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>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import json
|
||||
import secrets
|
||||
from datetime import timezone
|
||||
from datetime import date, timezone
|
||||
from fastapi import APIRouter, Depends, HTTPException, Header, Request, status
|
||||
from passlib.context import CryptContext
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -38,6 +38,15 @@ _TRANSITIONS: dict[str, set[str]] = {
|
||||
"ready": {"delivered"},
|
||||
"out_for_delivery": {"delivered"},
|
||||
}
|
||||
# reservations (S.6): confirmed or declined; then the guests arrive, or the venue cancels
|
||||
_RES_TRANSITIONS: dict[str, set[str]] = {
|
||||
"pending_acceptance": {"accepted", "rejected"},
|
||||
"accepted": {"delivered", "cancelled"},
|
||||
}
|
||||
|
||||
|
||||
def _transitions(order_type: str) -> dict[str, set[str]]:
|
||||
return _RES_TRANSITIONS if order_type in ordering.RES_TYPES else _TRANSITIONS
|
||||
|
||||
|
||||
def _require_site(
|
||||
@@ -77,6 +86,17 @@ def order_slots(site_slug: str, db: Session = Depends(get_db)):
|
||||
return ordering.summary(menu)
|
||||
|
||||
|
||||
@router.get("/{site_slug}/reservation-slots")
|
||||
def reservation_slots(site_slug: str, day: str, db: Session = Depends(get_db)):
|
||||
"""The reservation times of one venue day (S.6): ?day=YYYY-MM-DD → {day, slots: [iso]}."""
|
||||
_site, menu = _site_and_menu(db, site_slug)
|
||||
try:
|
||||
wanted = date.fromisoformat(day)
|
||||
except ValueError:
|
||||
raise HTTPException(status_code=422, detail={"code": "order.time_invalid", "params": {}})
|
||||
return {"day": wanted.isoformat(), "slots": [t.isoformat() for t in ordering.res_slots(menu, wanted)]}
|
||||
|
||||
|
||||
@router.post("/{site_slug}/quote")
|
||||
def quote_order(site_slug: str, body: QuoteRequest, request: Request, db: Session = Depends(get_db)):
|
||||
"""The cart priced by the cloud from the venue's menu, for the customer apps (phase 3S)."""
|
||||
@@ -94,17 +114,28 @@ def create_order(site_slug: str, body: OnlineOrderCreate, request: Request, db:
|
||||
_limit(request, "order", ORDER_LIMIT)
|
||||
site, menu = _site_and_menu(db, site_slug)
|
||||
items = [i.model_dump() for i in body.items]
|
||||
priced = _priced(menu, body.order_type, items, body.area_id, body.postcode)
|
||||
if body.order_type in ("takeaway", "delivery"): # the online ordering app (S.4)
|
||||
reservation = body.order_type in ordering.RES_TYPES # S.6: a table, maybe with food
|
||||
if body.order_type == "reservation":
|
||||
if items:
|
||||
raise HTTPException(status_code=422, detail={"code": "order.items_not_allowed", "params": {}})
|
||||
priced = None
|
||||
else:
|
||||
priced = _priced(menu, body.order_type, items, body.area_id, body.postcode)
|
||||
if reservation:
|
||||
problem = ordering.res_check(menu, body.order_type, body.requested_for, body.party_size)
|
||||
elif body.order_type in ("takeaway", "delivery"): # the online ordering app (S.4)
|
||||
problem = ordering.check(menu, body.order_type, body.requested_for)
|
||||
if problem:
|
||||
raise HTTPException(status_code=409, detail={"code": problem[0], "params": problem[1]})
|
||||
if not (body.customer_phone or "").strip():
|
||||
raise HTTPException(status_code=422, detail={"code": "order.details_missing", "params": {"fields": ["customer_phone"]}})
|
||||
try:
|
||||
check_sent(priced, items, body.total)
|
||||
except CheckoutError as e:
|
||||
raise HTTPException(status_code=e.status, detail=e.detail())
|
||||
else:
|
||||
problem = None
|
||||
if problem:
|
||||
raise HTTPException(status_code=409, detail={"code": problem[0], "params": problem[1]})
|
||||
if (reservation or body.order_type in ("takeaway", "delivery")) and not (body.customer_phone or "").strip():
|
||||
raise HTTPException(status_code=422, detail={"code": "order.details_missing", "params": {"fields": ["customer_phone"]}})
|
||||
if priced is not None:
|
||||
try:
|
||||
check_sent(priced, items, body.total)
|
||||
except CheckoutError as e:
|
||||
raise HTTPException(status_code=e.status, detail=e.detail())
|
||||
if body.order_type == "delivery" and not ((body.customer_address or "").strip() and (body.customer_phone or "").strip()):
|
||||
raise HTTPException(status_code=422, detail={"code": "order.details_missing", "params": {"fields": ["customer_phone", "customer_address"]}})
|
||||
|
||||
@@ -121,16 +152,17 @@ def create_order(site_slug: str, body: OnlineOrderCreate, request: Request, db:
|
||||
customer_address=body.customer_address,
|
||||
customer_notes=body.customer_notes,
|
||||
table_label=(body.customer_table or "").strip() or None,
|
||||
delivery_area=priced.area_name,
|
||||
delivery_area_id=priced.area_id,
|
||||
delivery_area=priced.area_name if priced else None,
|
||||
delivery_area_id=priced.area_id if priced else None,
|
||||
# stored as UTC: SQLite keeps no time zone (read back naive = UTC, schemas mark it)
|
||||
requested_for=(body.requested_for.astimezone(timezone.utc).replace(tzinfo=None)
|
||||
if body.order_type in ("takeaway", "delivery") and body.requested_for else None),
|
||||
sales_point=priced.sales_point,
|
||||
items_json=json.dumps([line.out() for line in priced.lines]),
|
||||
subtotal=priced.subtotal,
|
||||
delivery_fee=priced.delivery_fee,
|
||||
total=priced.total,
|
||||
if (reservation or body.order_type in ("takeaway", "delivery")) and body.requested_for else None),
|
||||
party_size=body.party_size if reservation else None,
|
||||
sales_point=priced.sales_point if priced else None,
|
||||
items_json=json.dumps([line.out() for line in priced.lines] if priced else []),
|
||||
subtotal=priced.subtotal if priced else 0.0,
|
||||
delivery_fee=priced.delivery_fee if priced else 0.0,
|
||||
total=priced.total if priced else 0.0,
|
||||
status="pending_acceptance",
|
||||
)
|
||||
order.tracking_token = secrets.token_urlsafe(12)
|
||||
@@ -155,6 +187,7 @@ def track_order(token: str, request: Request, db: Session = Depends(get_db)):
|
||||
lines = []
|
||||
return OrderStatusOut(public_ref=order.public_ref, status=order.status, rejection_reason=order.rejection_reason,
|
||||
order_type=order.order_type, requested_for=order.requested_for, created_at=order.created_at,
|
||||
party_size=order.party_size,
|
||||
lines=[{"name": x.get("name"), "quantity": x.get("quantity"), "unit_price": x.get("unit_price"),
|
||||
"options": [o.get("name") for o in x.get("options") or []]} for x in lines],
|
||||
subtotal=order.subtotal, delivery_fee=order.delivery_fee, total=order.total)
|
||||
@@ -224,7 +257,7 @@ def update_order_status(
|
||||
if not order:
|
||||
raise HTTPException(status_code=404, detail="Order not found")
|
||||
|
||||
allowed = _TRANSITIONS.get(order.status, set())
|
||||
allowed = _transitions(order.order_type).get(order.status, set())
|
||||
if body.status not in allowed:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
|
||||
Reference in New Issue
Block a user