Files
bonaminandClaude Opus 5.5 3459c80611 feat(cloud): table bookings online, with food ordered ahead
Plan phase 3S, step S.6.
- New order types "reservation" (a table only, no items) and "preorder" (a table with food,
  priced at the online_preorder sales point); online_orders.party_size (migration 0005).
- services/ordering.py: the venue's booking settings (ordering.reservations in the snapshot):
  hours (none = any time), time grid, notice, days ahead (≤ 90), party size; res_slots per
  venue day, res_days, res_check (order.service_unavailable, order.party_invalid,
  order.time_invalid). The public menu's ordering summary gains `reservations`
  {enabled, with_food, party_max, days}; GET /api/orders/{site}/reservation-slots?day=.
- Bookings need a phone; a plain booking refuses items (order.items_not_allowed).
- Status steps for bookings: pending_acceptance → accepted | rejected; accepted → delivered
  (arrived) | cancelled (by the venue); ordinary orders unchanged.
- Order app: "Book a table" (people, day, time, optional food at the pre-order prices),
  booking summary at checkout, a booking tracking page (waiting, confirmed, welcome;
  declined / cancelled with the reason). en/el.
- Tests: tests/test_reservations.py (3), order-app logic test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:31:21 +02:00

115 lines
7.4 KiB
Python

"""Reservations online (plan phase 3S, step S.6): a table only, or a table with food ready on
arrival; times on the venue's grid, inside its hours, with notice, up to N days ahead; party
size; the venue confirms or declines, then the guests arrive or the venue cancels."""
import json
from datetime import date, datetime, timezone
from zoneinfo import ZoneInfo
from services import ordering
from test_checkout import COLA, ORDERING, _code, _h, _venue
ATH = ZoneInfo("Europe/Athens")
DINNER = {"0": [["19:00", "23:00"]], "6": [["13:00", "16:00"], ["19:00", "23:00"]]} # Mon, Sun
def snap(**res):
return {"timezone": "Europe/Athens", "ordering": {**ORDERING, "reservations": {
"enabled": True, "with_food": True, "hours": DINNER, "slot_minutes": 30, "days_ahead": 14,
"notice_minutes": 120, "party_max": 8, **res}}}
def at(y, mo, d, h, mi):
return datetime(y, mo, d, h, mi, tzinfo=ATH).astimezone(timezone.utc)
def test_times_days_and_checks():
s, now = snap(), at(2026, 11, 15, 18, 10) # Sunday 18:10
sunday = [t.strftime("%H:%M") for t in ordering.res_slots(s, date(2026, 11, 15), now)]
assert sunday == ["20:30", "21:00", "21:30", "22:00", "22:30"] # 2 h notice; lunch is over
monday = [t.strftime("%H:%M") for t in ordering.res_slots(s, date(2026, 11, 16), now)]
assert monday[0] == "19:00" and monday[-1] == "22:30"
assert ordering.res_slots(s, date(2026, 11, 17), now) == [] # Tuesday: no hours = closed
assert ordering.res_slots(s, date(2026, 11, 30), now) == [] # beyond 14 days
days = ordering.res_days(s, now)
assert days[:3] == ["2026-11-15", "2026-11-16", "2026-11-22"] and "2026-11-17" not in days
ok = at(2026, 11, 16, 20, 0)
assert ordering.res_check(s, "reservation", ok, 4, now) is None
assert ordering.res_check(s, "preorder", ok, 4, now) is None
assert ordering.res_check(s, "reservation", ok, 9, now) == ("order.party_invalid", {"max": 8})
assert ordering.res_check(s, "reservation", ok, 0, now)[0] == "order.party_invalid"
assert ordering.res_check(s, "reservation", ok, None, now)[0] == "order.party_invalid"
assert ordering.res_check(s, "reservation", at(2026, 11, 16, 20, 10), 2, now)[0] == "order.time_invalid" # off grid
assert ordering.res_check(s, "reservation", at(2026, 11, 15, 19, 0), 2, now)[0] == "order.time_invalid" # too soon
assert ordering.res_check(s, "reservation", None, 2, now)[0] == "order.time_invalid"
assert ordering.res_check(snap(with_food=False), "preorder", ok, 2, now) == ("order.service_unavailable", {"service": "preorder"})
assert ordering.res_check(snap(enabled=False), "reservation", ok, 2, now)[0] == "order.service_unavailable"
assert ordering.res_summary(snap(enabled=False), now) == {"enabled": False, "with_food": False, "party_max": 8, "days": []}
def _push(client, site, numeric, **res):
menu = client.get(f"/api/menu/{site['site_id']}").json()
hours = {str(d): [["00:00", "23:59"]] for d in range(7)}
snapshot = {"categories": menu["categories"], "timezone": "Europe/Athens",
"ordering": {**ORDERING, "reservations": {"enabled": True, "with_food": True, "hours": hours, "slot_minutes": 30,
"days_ahead": 3, "notice_minutes": 0, "party_max": 6, **res}}}
for cat in snapshot["categories"]:
for p in cat["products"]:
if p["id"] == COLA:
p["sold_at"] = [*p["sold_at"], "online_preorder"]
p["prices"] = {**p["prices"], "online_preorder": 2.6}
r = client.post("/api/menu/sync", headers=_h(site), json={"site_id": numeric, "snapshot_json": json.dumps(snapshot)})
assert r.status_code in (200, 204), r.text
def test_book_a_table_with_and_without_food(client, admin_h):
site, numeric = _venue(client, admin_h)
_push(client, site, numeric)
menu = client.get(f"/api/menu/{site['site_id']}").json()
res = menu["ordering"]["reservations"]
assert res["enabled"] and res["with_food"] and res["party_max"] == 6 and len(res["days"]) == 4
day = res["days"][1]
slots = client.get(f"/api/orders/{site['site_id']}/reservation-slots", params={"day": day}).json()["slots"]
assert len(slots) == 48
when = slots[40] # 20:00 tomorrow
book = lambda **x: client.post(f"/api/orders/{site['site_id']}", json={ # noqa: E731
"order_type": "reservation", "customer_name": "Eleni", "customer_phone": "6977777777", "party_size": 4,
"requested_for": when, "customer_notes": "By the window", **x})
plain = book()
assert plain.status_code == 201, plain.text
assert _code(book(party_size=7)) == "order.party_invalid"
assert _code(book(customer_phone="")) == "order.details_missing"
assert _code(book(items=[{"product_id": COLA, "quantity": 1}])) == "order.items_not_allowed"
assert _code(book(requested_for=None)) == "order.time_invalid"
food = book(order_type="preorder", items=[{"product_id": COLA, "quantity": 2}], total=5.2)
assert food.status_code == 201, food.text
assert food.json()["total"] == 5.2 # the preorder price
assert _code(book(order_type="preorder", items=[])) == "order.empty"
pending = {o["public_ref"]: o for o in client.get(f"/api/orders/pending/{numeric}", headers=_h(site)).json()}
p = pending[plain.json()["public_ref"]]
assert (p["order_type"], p["party_size"], p["total"], json.loads(p["items_json"]), p["sales_point"]) == ("reservation", 4, 0.0, [], None)
assert datetime.fromisoformat(p["requested_for"].replace("Z", "+00:00")) == datetime.fromisoformat(when)
f = pending[food.json()["public_ref"]]
assert (f["order_type"], f["sales_point"], f["party_size"]) == ("preorder", "online_preorder", 4)
# the venue confirms, then the guests arrive (or it cancels); the customer's page follows
st = lambda oid, s: client.patch(f"/api/orders/{oid}/status", headers=_h(site), json={"status": s}) # noqa: E731
assert st(p["id"], "preparing").status_code == 422
assert st(p["id"], "accepted").status_code == 204
assert st(p["id"], "delivered").status_code == 204
assert st(f["id"], "accepted").status_code == 204 and st(f["id"], "cancelled").status_code == 204
track = client.get(f"/api/orders/track/{food.json()['tracking_token']}").json()
assert (track["status"], track["order_type"], track["party_size"], track["total"]) == ("cancelled", "preorder", 4, 5.2)
# an ordinary order can't be "cancelled" this way
order = client.post(f"/api/orders/{site['site_id']}", json={"order_type": "takeaway", "customer_name": "M",
"customer_phone": "6900000000", "items": [{"product_id": COLA, "quantity": 1}]})
oid = next(o["id"] for o in client.get(f"/api/orders/pending/{numeric}", headers=_h(site)).json() if o["public_ref"] == order.json()["public_ref"])
st(oid, "accepted")
assert st(oid, "cancelled").status_code == 422
def test_reservations_off_by_default(client, admin_h):
site, _ = _venue(client, admin_h)
assert client.get(f"/api/menu/{site['site_id']}").json()["ordering"]["reservations"]["enabled"] is False
r = client.post(f"/api/orders/{site['site_id']}", json={"order_type": "reservation", "customer_name": "E", "customer_phone": "690000000",
"party_size": 2, "requested_for": datetime.now(timezone.utc).isoformat()})
assert _code(r) == "order.service_unavailable"