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.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
pickup_slots.py · 4.1 KB · 95 lines
| 1 | """Pre-write hook for pickup_slots: a window cannot hold minus one order, |
| 2 | and it cannot already hold more than it can. |
| 3 | |
| 4 | Two gates, both of them things the schema has no way to say. `capacity` |
| 5 | and `orders_taken` are ordinary integers as far as validation is |
| 6 | concerned, and integers are perfectly happy to be -3 or to be four in a |
| 7 | window that holds two. |
| 8 | |
| 9 | **Capacity may not be negative.** Zero is legal and means something |
| 10 | useful -- "this window exists and is full", which is a different sentence |
| 11 | from is_open=false's "we are not open then". Minus one is not a smaller |
| 12 | version of that; it is a number no surface can act on, and the picker |
| 13 | that reads it would quietly decide the window has places left. |
| 14 | |
| 15 | **orders_taken may not exceed capacity, and the refusal carries both |
| 16 | numbers.** A gate that only says "no" leaves whoever is fixing the slot |
| 17 | guessing which of the two figures is wrong -- exactly the objection |
| 18 | hook_shipment_lines' three-number refusal answers on the shipping side. |
| 19 | |
| 20 | **The race this does NOT stop, and must not be read as stopping.** Two |
| 21 | shoppers can pass action_checkout's capacity gate for the last place in |
| 22 | one window; the second increment then pushes orders_taken past capacity |
| 23 | for real. Checkout writes through object_records directly, which is a |
| 24 | trusted server-side path and bypasses hooks by design |
| 25 | (docs/validation-and-logic.md), so that write lands. THAT IS DELIBERATE |
| 26 | AND IT IS THE POINT: the oversell is a fact about what happened, and the |
| 27 | shop needs to see it on the row it happened to so somebody can ring the |
| 28 | customer. What this hook refuses is a NUMBER SOMEBODY TYPED -- an |
| 29 | operator editing a slot in a form, or an API client posting one -- and a |
| 30 | typo is not a fact. The same split hook_shipment_lines and |
| 31 | action_create_shipment already carry, named in both files so neither is |
| 32 | removed on the assumption the other covers it. |
| 33 | """ |
| 34 | |
| 35 | |
| 36 | def _text(value): |
| 37 | return str(value if value is not None else "").strip() |
| 38 | |
| 39 | |
| 40 | def _int(value, default=None): |
| 41 | """The integer a field holds, or `default` when it holds nothing and |
| 42 | None when it holds something that is not one. |
| 43 | |
| 44 | None means "schema validation owns this error": a hook that invented |
| 45 | its own message for a non-integer would produce two different |
| 46 | refusals for one mistake depending on which gate saw it first. |
| 47 | """ |
| 48 | text = _text(value) |
| 49 | if not text: |
| 50 | return default |
| 51 | try: |
| 52 | return int(text) |
| 53 | except ValueError: |
| 54 | return None |
| 55 | |
| 56 | |
| 57 | def BEFORE_WRITE(request): |
| 58 | action = _text(request.get("action")) |
| 59 | if action not in ("create", "update"): |
| 60 | return None |
| 61 | record = request.get("record") or {} |
| 62 | |
| 63 | capacity = _int(record.get("capacity"), 0) |
| 64 | if capacity is None: |
| 65 | return None |
| 66 | if capacity < 0: |
| 67 | return {"error": (f"A slot's capacity cannot be negative (got " |
| 68 | f"{capacity}). Zero is the way to say this window " |
| 69 | f"is full, and is_open=false is the way to say the " |
| 70 | f"shop is shut then -- a negative capacity is " |
| 71 | f"neither, and every surface that reads it would " |
| 72 | f"have to guess which one was meant."), |
| 73 | "status": 400} |
| 74 | |
| 75 | taken = _int(record.get("orders_taken"), 0) |
| 76 | if taken is None: |
| 77 | return None |
| 78 | if taken < 0: |
| 79 | return {"error": (f"A slot cannot have taken a negative number of " |
| 80 | f"orders (got {taken}). Nothing booked is what zero " |
| 81 | f"means; a correction is a new number, never a " |
| 82 | f"negative one."), |
| 83 | "status": 400} |
| 84 | |
| 85 | if taken > capacity: |
| 86 | starts_at = _text(record.get("starts_at")) |
| 87 | when = f" starting {starts_at}" if starts_at else "" |
| 88 | return {"error": (f"That slot{when} would hold more orders than it " |
| 89 | f"can: capacity {capacity}, orders taken {taken}. " |
| 90 | f"Raise the capacity if the shop really can serve " |
| 91 | f"{taken} in that window -- do not raise the count, " |
| 92 | f"which is a record of orders that exist."), |
| 93 | "status": 409} |
| 94 | |
| 95 | return None |