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.
open_dispute.py · 6.0 KB · 142 lines
| 1 | """action_open_dispute -- staff record a customer's claim about a shop sale. |
| 2 | |
| 3 | POST {kind, summary, detail?, order_id?, shipment_id?, payment_id?, |
| 4 | customer_email?} |
| 5 | |
| 6 | Why an action and not the record API. disputes.order_id, shipment_id and |
| 7 | payment_id declare `relation.requires: update`, so over the API a row may |
| 8 | point only at an order its writer may change: a customer's dispute cannot |
| 9 | name somebody else's order or payment. That is right for customers and |
| 10 | wrong for the counter, where the person taking the call is a cashier who |
| 11 | may READ every sale and change none, and the sale is the shop's -- so the |
| 12 | API answered a cashier's dispute about a shop order with 403. Giving staff |
| 13 | update on orders would let them reprice and void bills; loosening the |
| 14 | relation would let a customer point a dispute at anybody's order. Neither. |
| 15 | This action is the grant (docs/package-authoring.md, "An action that is |
| 16 | the grant"), and checks for itself what the relation check stood for: |
| 17 | |
| 18 | - **Role.** Staff only (object_corrections.staff_refusal, from the execute |
| 19 | rules this package ships for cashier, shift_lead, manager and owner), |
| 20 | whatever the server's enforcement setting. |
| 21 | - **Scope.** The order and the payment are read AS THE CALLER (staff roles |
| 22 | read every sale; a customer would see only their own, but customers do |
| 23 | not get this far). A payment named beside an order must be a payment on |
| 24 | that order's bill, owned by the order's account. A shipment, which staff |
| 25 | roles do not read, is read under this action's authority only when it |
| 26 | is the named order's own shipment, and a deny rule naming the caller |
| 27 | still wins; without an order, the caller has to be able to read it. A |
| 28 | refused record answers exactly like a missing one, without the id. |
| 29 | - **State.** The row is opened `open`, owned by and opened_by the caller, |
| 30 | with no resolution: closing it, and paying anything out, is |
| 31 | action_resolve_dispute's door and its own checks. |
| 32 | |
| 33 | Nothing about orders, payments or shipments is written here. |
| 34 | """ |
| 35 | |
| 36 | import os |
| 37 | |
| 38 | import object_access |
| 39 | import object_collections |
| 40 | import object_corrections |
| 41 | import object_ids |
| 42 | import object_records |
| 43 | |
| 44 | ACTOR = "action_open_dispute" |
| 45 | KINDS = ("shipment", "delivery", "payment", "product", "other") |
| 46 | |
| 47 | |
| 48 | def _base_dir(): |
| 49 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 50 | |
| 51 | |
| 52 | def _text(value): |
| 53 | return str(value if value is not None else "").strip() |
| 54 | |
| 55 | |
| 56 | def _missing(what): |
| 57 | return {"status": 404, "error": f"No such {what}."} |
| 58 | |
| 59 | |
| 60 | def _mismatch(what): |
| 61 | return {"status": 409, "code": "order_mismatch", |
| 62 | "error": f"{what}; nothing was recorded."} |
| 63 | |
| 64 | |
| 65 | def _on_the_orders_bill(order, payment): |
| 66 | invoice_id = _text(order.get("invoice_id")) |
| 67 | return (bool(invoice_id) |
| 68 | and _text(payment.get("invoice_id")) == invoice_id |
| 69 | and _text(payment.get("owner_id")) == _text(order.get("owner_id"))) |
| 70 | |
| 71 | |
| 72 | def _shipment(base, access, shipment_id, order): |
| 73 | """(row, refusal). Staff roles do not read shipments, so a shipment is |
| 74 | taken under this action's authority only as the named order's own.""" |
| 75 | if not order: |
| 76 | row = access.get("shipments", shipment_id) |
| 77 | return (row, None) if row is not None else (None, _missing("shipment")) |
| 78 | try: |
| 79 | # access: role-only object (execute granted to staff roles); a shipment is taken only as the order the caller read through access.get, and denied_by_rule below still refuses |
| 80 | row = object_records.get_collection_record("shipments", shipment_id, base_dir=base) |
| 81 | except (object_records.RecordNotFoundError, object_records.InvalidRecordIdError, |
| 82 | object_collections.CollectionNotFoundError): |
| 83 | row = None |
| 84 | if row is None or object_corrections.denied_by_rule(access, "read", "shipments", row): |
| 85 | return None, _missing("shipment") |
| 86 | if _text(row.get("order_id")) != _text(order.get("id")): |
| 87 | return None, _mismatch("That shipment is not a shipment of this order") |
| 88 | return row, None |
| 89 | |
| 90 | |
| 91 | def POST(request): |
| 92 | base = _base_dir() |
| 93 | access = object_access.Access(request.get("_identity"), base_dir=base) |
| 94 | staff = object_corrections.staff_refusal(access, "action_open_dispute", |
| 95 | doing="open a dispute here") |
| 96 | if staff: |
| 97 | return staff |
| 98 | |
| 99 | kind = _text(request.get("kind")) |
| 100 | if kind not in KINDS: |
| 101 | return {"status": 400, |
| 102 | "error": f"kind is one of {', '.join(KINDS)}."} |
| 103 | summary = _text(request.get("summary")) |
| 104 | if not summary or len(summary) > 300: |
| 105 | return {"status": 400, |
| 106 | "error": "summary is required: one line, at most 300 characters."} |
| 107 | email = _text(request.get("customer_email")) |
| 108 | if len(email) > 254: |
| 109 | return {"status": 400, "error": "customer_email is at most 254 characters."} |
| 110 | |
| 111 | order_id = _text(request.get("order_id")) |
| 112 | payment_id = _text(request.get("payment_id")) |
| 113 | shipment_id = _text(request.get("shipment_id")) |
| 114 | |
| 115 | order = {} |
| 116 | if order_id: |
| 117 | order = access.get("orders", order_id) |
| 118 | if order is None: |
| 119 | return _missing("order") |
| 120 | if payment_id: |
| 121 | payment = access.get("payments", payment_id) |
| 122 | if payment is None: |
| 123 | return _missing("payment") |
| 124 | if order and not _on_the_orders_bill(order, payment): |
| 125 | return _mismatch("That payment is not a payment on this order") |
| 126 | if shipment_id: |
| 127 | _, refusal = _shipment(base, access, shipment_id, order) |
| 128 | if refusal: |
| 129 | return refusal |
| 130 | |
| 131 | dispute_id = object_ids.new_uuid4() |
| 132 | who = access.user_id |
| 133 | # access: role-only object (execute granted to staff roles); the new row is the caller's own and points only at records checked above |
| 134 | object_records.create_collection_record( |
| 135 | "disputes", |
| 136 | {"id": dispute_id, "kind": kind, "order_id": order_id, |
| 137 | "shipment_id": shipment_id, "payment_id": payment_id, |
| 138 | "opened_by": who, "customer_email": email, "summary": summary, |
| 139 | "detail": _text(request.get("detail")), "status": "open", |
| 140 | "owner_id": who}, |
| 141 | base_dir=base, actor=f"{ACTOR}:{who}") |
| 142 | return {"ok": True, "dispute_id": dispute_id, "status": "open"} |