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.
line_freeze.py · 2.8 KB · 73 lines
| 1 | """system_line_freeze -- an order line whose bill is issued or paid is not |
| 2 | rewritten in place. |
| 3 | |
| 4 | The locked check `settled_line` on order_lines (lock `ledger`, the lock |
| 5 | payments, refunds and invoice lines take). The rule and its reasons live in |
| 6 | object_corrections; this object reads the order, the invoices billing it and |
| 7 | the payments against them, and asks. See object_corrections' docstring for |
| 8 | what Corner Cup found (a paid soup edited into a salad, an unpaid line |
| 9 | deleted while the invoice kept billing it). |
| 10 | |
| 11 | Fails closed: invoices or payments that are installed and cannot be read |
| 12 | answer 503 ("try again"), never "nothing has been paid". Not installed is a |
| 13 | fact: an order nobody invoices on this box is not frozen by an invoice. |
| 14 | """ |
| 15 | |
| 16 | import os |
| 17 | |
| 18 | import object_collections |
| 19 | import object_corrections |
| 20 | import object_records |
| 21 | |
| 22 | |
| 23 | def _base_dir(): |
| 24 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 25 | |
| 26 | |
| 27 | def _rows(collection, base): |
| 28 | try: |
| 29 | return object_records.read_collection_records(collection, base_dir=base) |
| 30 | except object_collections.CollectionNotFoundError: |
| 31 | return [] |
| 32 | |
| 33 | |
| 34 | def _order(order_id, base): |
| 35 | if not order_id: |
| 36 | return None |
| 37 | try: |
| 38 | return object_records.get_collection_record("orders", order_id, base_dir=base) |
| 39 | except (object_records.RecordNotFoundError, object_records.InvalidRecordIdError, |
| 40 | object_collections.CollectionNotFoundError): |
| 41 | return None |
| 42 | |
| 43 | |
| 44 | def LOCKED_CHECK(request): |
| 45 | action = request.get("action") |
| 46 | record = request.get("record") or {} |
| 47 | existing = request.get("existing") |
| 48 | base = request.get("base_dir") or _base_dir() |
| 49 | text = object_corrections.text |
| 50 | order_ids = {text(record.get("order_id"))} |
| 51 | if existing: |
| 52 | order_ids.add(text(existing.get("order_id"))) |
| 53 | try: |
| 54 | invoices = _rows("invoices", base) |
| 55 | payments = _rows("payments", base) |
| 56 | worst = None |
| 57 | for order_id in sorted(i for i in order_ids if i): |
| 58 | bills = object_corrections.bills_for_order(_order(order_id, base), invoices) |
| 59 | received = object_corrections.received_cents( |
| 60 | [b.get("id") for b in bills], payments) |
| 61 | refusal = object_corrections.line_refusal( |
| 62 | kind="order", action=action, existing=existing, record=record, |
| 63 | issued_bill=any(object_corrections.issued(b) for b in bills), |
| 64 | received=received) |
| 65 | worst = worst or refusal |
| 66 | except Exception as exc: # noqa: BLE001 -- unknown is not "unpaid" |
| 67 | return {"status": 503, |
| 68 | "error": (f"Whether this order has been billed or paid cannot be read " |
| 69 | f"right now ({str(exc)[:120]}), so the line was not changed. " |
| 70 | f"Try again in a moment.")} |
| 71 | if worst: |
| 72 | return {"status": 409, "error": worst} |
| 73 | return None |