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.
pickup_slots.py · 3.1 KB · 74 lines
| 1 | """Pre-write hook for pickup_slots: a window cannot already hold more than |
| 2 | it can. |
| 3 | |
| 4 | One gate, the one the schema has no way to say: a comparison between two |
| 5 | fields. `capacity` and `orders_taken` are integers, and the schema declares |
| 6 | each `validation.min` 0 -- so neither can be negative, for every writer, |
| 7 | which is what this hook used to check by hand. (Zero capacity is legal and |
| 8 | means something useful -- "this window exists and is full", a different |
| 9 | sentence from is_open=false's "we are not open then".) |
| 10 | |
| 11 | **orders_taken may not exceed capacity, and the refusal carries both |
| 12 | numbers.** A gate that only says "no" leaves whoever is fixing the slot |
| 13 | guessing which of the two figures is wrong -- exactly the objection |
| 14 | hook_shipment_lines' three-number refusal answers on the shipping side. |
| 15 | |
| 16 | **The race this does NOT stop, and must not be read as stopping.** Two |
| 17 | shoppers can pass action_checkout's capacity gate for the last place in |
| 18 | one window; the second increment then pushes orders_taken past capacity |
| 19 | for real. Checkout writes through object_records directly, which is a |
| 20 | trusted server-side path and bypasses hooks by design |
| 21 | (docs/validation-and-logic.md), so that write lands. THAT IS DELIBERATE |
| 22 | AND IT IS THE POINT: the oversell is a fact about what happened, and the |
| 23 | shop needs to see it on the row it happened to so somebody can ring the |
| 24 | customer. What this hook refuses is a NUMBER SOMEBODY TYPED -- an |
| 25 | operator editing a slot in a form, or an API client posting one -- and a |
| 26 | typo is not a fact. The same split hook_shipment_lines and |
| 27 | action_create_shipment already carry, named in both files so neither is |
| 28 | removed on the assumption the other covers it. |
| 29 | """ |
| 30 | |
| 31 | |
| 32 | def _text(value): |
| 33 | return str(value if value is not None else "").strip() |
| 34 | |
| 35 | |
| 36 | def _int(value, default=None): |
| 37 | """The integer a field holds, or `default` when it holds nothing and |
| 38 | None when it holds something that is not one. |
| 39 | |
| 40 | None means "schema validation owns this error": a hook that invented |
| 41 | its own message for a non-integer would produce two different |
| 42 | refusals for one mistake depending on which gate saw it first. |
| 43 | """ |
| 44 | text = _text(value) |
| 45 | if not text: |
| 46 | return default |
| 47 | try: |
| 48 | return int(text) |
| 49 | except ValueError: |
| 50 | return None |
| 51 | |
| 52 | |
| 53 | def BEFORE_WRITE(request): |
| 54 | action = _text(request.get("action")) |
| 55 | if action not in ("create", "update"): |
| 56 | return None |
| 57 | record = request.get("record") or {} |
| 58 | |
| 59 | capacity = _int(record.get("capacity"), 0) |
| 60 | taken = _int(record.get("orders_taken"), 0) |
| 61 | if capacity is None or taken is None: |
| 62 | return None # schema validation owns the type error |
| 63 | |
| 64 | if taken > capacity: |
| 65 | starts_at = _text(record.get("starts_at")) |
| 66 | when = f" starting {starts_at}" if starts_at else "" |
| 67 | return {"error": (f"That slot{when} would hold more orders than it " |
| 68 | f"can: capacity {capacity}, orders taken {taken}. " |
| 69 | f"Raise the capacity if the shop really can serve " |
| 70 | f"{taken} in that window -- do not raise the count, " |
| 71 | f"which is a record of orders that exist."), |
| 72 | "status": 409} |
| 73 | |
| 74 | return None |