askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
goodwill.py · 1.8 KB · 46 lines
| 1 | """hook_goodwill -- a goodwill record cites a reason the shop lists, and |
| 2 | says what the customer was told. |
| 3 | |
| 4 | A gate, so it only refuses or stamps (docs/logic-decisions.md #6): the |
| 5 | reason code must be a correction_reasons code that applies to goodwill |
| 6 | (data a manager extends, so a typo is refused with the list rather than |
| 7 | stored as a reason nobody can count); customer_wording is filled from the |
| 8 | reason's wording when the person left it blank, so every goodwill row says |
| 9 | what the customer heard. Who may give goodwill at all is the permission |
| 10 | rules' (shift lead, manager, owner create), and the approver is the row's |
| 11 | owner, which the server stamps. |
| 12 | |
| 13 | Fails closed: reason codes that cannot be read refuse the write (503) |
| 14 | rather than letting an unchecked code through. |
| 15 | """ |
| 16 | |
| 17 | from __future__ import annotations |
| 18 | |
| 19 | import os |
| 20 | |
| 21 | import object_collections |
| 22 | import object_corrections |
| 23 | import object_records |
| 24 | |
| 25 | |
| 26 | def _base_dir(): |
| 27 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 28 | |
| 29 | |
| 30 | def BEFORE_WRITE(request): |
| 31 | action = object_corrections.text(request.get("action")) |
| 32 | if action != "create": |
| 33 | return None |
| 34 | record = dict(request.get("record") or {}) |
| 35 | try: |
| 36 | reasons = object_records.read_collection_records("correction_reasons", base_dir=_base_dir()) |
| 37 | except object_collections.CollectionNotFoundError: |
| 38 | reasons = [] |
| 39 | except Exception: |
| 40 | return {"status": 503, "error": "Reason codes could not be read; nothing was recorded."} |
| 41 | reason, why = object_corrections.find_reason(reasons, record.get("reason_code"), "goodwill") |
| 42 | if reason is None: |
| 43 | return {"status": 422, "error": why} |
| 44 | if not object_corrections.text(record.get("customer_wording")): |
| 45 | record["customer_wording"] = object_corrections.text(reason.get("customer_wording")) |
| 46 | return {"record": record} |