feat(cloud): the online ordering app (takeaway, delivery) with hours, time slots and rate limits (platform-v2 3S, S.4)

- connect_frontend/order-app at /order/{site}: menu per service at that service's prices,
  extras, basket (kept per tab), checkout priced by the cloud (fee, area by list or
  postcode, minimum), name/phone/address/notes (remembered), as soon as possible or a time
  slot, tracking page with steps, lines and totals; en/el; tokens in index.css. Logic in
  src/logic.js, unit-tested; built and served by the connect image (npm test in the build).
- services/ordering.py: the venue's services, hours (past midnight), preparation, slot grid,
  days ahead, ASAP; takeaway/delivery orders checked (order.service_unavailable,
  order.closed, order.time_invalid) and need a phone; GET /api/orders/{site}/slots; the
  public menu carries the ordering summary.
- services/ratelimit.py: per-caller limits on quote, order and track (429 rate.limited).
- online_orders.requested_for (0003, unreleased) stored as UTC; every datetime sent with its
  offset (the database keeps no zone: a 12:00 Athens slot came back as 12:00 unzoned).
- Tracking returns what was ordered, the totals, the type and the time.
- Tests: test_ordering.py (5), checkout/fees updated; order-app unit (4).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 13:39:00 +02:00
co-authored by Claude Opus 5.5
parent 9e8ec4c4e6
commit b25bfcda3e
30 changed files with 4424 additions and 15 deletions
+95
View File
@@ -0,0 +1,95 @@
"""When online orders are taken: services, hours, time slots, rate limits (plan phase 3S, S.4)."""
import json
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
from services import ordering
from test_checkout import COLA, _code, _h, _menu, _order, _venue
ATH = ZoneInfo("Europe/Athens")
def snap(**cfg):
return {"timezone": "Europe/Athens", "ordering": {"services": ["takeaway", "delivery"], "hours": {}, "prep_minutes": 20,
"slot_minutes": 15, "days_ahead": 0, "asap": True, **cfg}}
def at(y, mo, d, h, mi):
return datetime(y, mo, d, h, mi, tzinfo=ATH).astimezone(timezone.utc)
# Sunday 15 Nov 2026 is weekday 6; Monday 16 Nov is 0
EVENINGS = {"6": [["18:00", "23:00"]], "5": [["20:00", "02:00"]]}
def test_open_hours_including_past_midnight():
cfg = ordering.config(snap(hours=EVENINGS))
local = lambda *a: datetime(*a, tzinfo=ATH) # noqa: E731
assert ordering.is_open(cfg, local(2026, 11, 15, 18, 0))
assert not ordering.is_open(cfg, local(2026, 11, 15, 23, 0))
assert not ordering.is_open(cfg, local(2026, 11, 15, 12, 0))
assert ordering.is_open(cfg, local(2026, 11, 15, 1, 30)) # Saturday's 20:00–02:00, after midnight
assert not ordering.is_open(cfg, local(2026, 11, 16, 19, 0)) # Monday has no hours: closed
assert ordering.is_open(ordering.config(snap()), local(2026, 11, 16, 4, 0)) # no hours = any time
def test_slots_start_after_preparation_on_the_grid_and_stay_in_hours():
s = snap(hours=EVENINGS)
got = [t.strftime("%H:%M") for t in ordering.slots(s, at(2026, 11, 15, 21, 7))]
assert got == ["21:30", "21:45", "22:00", "22:15", "22:30", "22:45"] # 21:07 + 20 → 21:27 → 21:30
assert ordering.slots(s, at(2026, 11, 15, 22, 50)) == [] # nothing left today
ahead = ordering.slots(snap(hours=EVENINGS, days_ahead=1, slot_minutes=30), at(2026, 11, 15, 22, 50))
assert ahead == [] # Monday closed
morning = [t.strftime("%H:%M") for t in ordering.slots(s, at(2026, 11, 15, 10, 0))]
assert morning[0] == "18:00" and morning[-1] == "22:45"
def test_check_asap_and_requested_times():
s = snap(hours=EVENINGS)
evening, noon = at(2026, 11, 15, 21, 7), at(2026, 11, 15, 12, 0)
assert ordering.check(s, "delivery", None, evening) is None
assert ordering.check(s, "delivery", None, noon) == ("order.closed", {}) # closed now: no ASAP
assert ordering.check(s, "takeaway", at(2026, 11, 15, 18, 30), noon) is None # but a later slot is fine
assert ordering.check(s, "takeaway", at(2026, 11, 15, 18, 31), noon)[0] == "order.time_invalid" # off the grid
assert ordering.check(s, "takeaway", at(2026, 11, 15, 21, 15), evening)[0] == "order.time_invalid" # too soon
assert ordering.check(snap(asap=False), "delivery", None, evening) == ("order.closed", {})
assert ordering.check(snap(services=["takeaway"]), "delivery", None, evening) == ("order.service_unavailable", {"service": "delivery"})
assert ordering.check(s, "takeaway", datetime(2026, 11, 15, 18, 30), noon)[0] == "order.time_invalid" # no zone
def test_the_endpoints_enforce_it(client, admin_h):
site, numeric = _venue(client, admin_h)
# the venue turns delivery off and opens only on Mondays 12:00–13:00
menu = client.get(f"/api/menu/{site['site_id']}").json()
snapshot = {"categories": menu["categories"], "timezone": "Europe/Athens",
"ordering": {"services": ["takeaway"], "hours": {"0": [["12:00", "13:00"]]}, "prep_minutes": 0,
"slot_minutes": 15, "days_ahead": 7, "asap": True}}
client.post("/api/menu/sync", headers=_h(site), json={"site_id": numeric, "snapshot_json": json.dumps(snapshot)})
s = client.get(f"/api/orders/{site['site_id']}/slots").json()
assert s["services"] == ["takeaway"] and len(s["slots"]) >= 4
first = datetime.fromisoformat(s["slots"][0])
assert first.astimezone(ATH).weekday() == 0 and first.astimezone(ATH).strftime("%H:%M") >= "12:00"
assert client.get(f"/api/menu/{site['site_id']}").json()["ordering"]["services"] == ["takeaway"]
assert _code(_order(client, site, "delivery", customer_address="x")) == "order.service_unavailable"
r = _order(client, site, "takeaway", requested_for=s["slots"][0])
assert r.status_code == 201, r.text
stored = next(o for o in client.get(f"/api/orders/pending/{numeric}", headers=_h(site)).json() if o["public_ref"] == r.json()["public_ref"])
assert datetime.fromisoformat(stored["requested_for"].replace("Z", "+00:00")) == first
track = client.get(f"/api/orders/track/{r.json()['tracking_token']}").json()
assert track["requested_for"] and track["order_type"] == "takeaway"
# a takeaway or delivery order needs a phone number
assert _code(_order(client, site, "takeaway", customer_phone="", requested_for=s["slots"][0])) == "order.details_missing"
def test_rate_limits(client, admin_h):
from services import ratelimit
site, _ = _venue(client, admin_h)
statuses = [_order(client, site).status_code for _ in range(11)]
assert statuses[:10] == [201] * 10 and statuses[10] == 429
assert _code(_order(client, site)) == "rate.limited"
# another address is counted apart (behind the proxy)
ok = client.post(f"/api/orders/{site['site_id']}", headers={"X-Forwarded-For": "203.0.113.9"},
json={"order_type": "takeaway", "customer_name": "A", "customer_phone": "1", "items": [{"product_id": COLA, "quantity": 1}]})
assert ok.status_code == 201
assert ratelimit.hit("t", "k", 2, 60, now=0) and ratelimit.hit("t", "k", 2, 60, now=1) and not ratelimit.hit("t", "k", 2, 60, now=2)
assert ratelimit.hit("t", "k", 2, 60, now=61) # the window moves on