Plan phase 3S, step S.7.
- services/qr.py: the snapshot's `qr` {ordering, tables: [{id, label, token_hash}]} (sha256
of each table's token; the cloud never holds the tokens). A table order (dine_in) needs
the venue's ordering on, the site's menu_mode "order" and a valid table token: otherwise
order.service_unavailable / 403 order.table_invalid (a forged, old or typed-in table is
refused). The order keeps the table's id and label (online_orders.table_id, migration 0006).
- Public menu: ?t=<token> → `table` {id, label} or `table_invalid`; `table_ordering`; `qr`
never leaves the cloud.
- Heartbeat returns `apps_url` (new setting PUBLIC_APPS_URL: where /menu/ and /order/ are
served) so sites can print table QR codes that open the right address.
- Menu app: the table from its QR code (kept for the visit), a "Table 5" banner, ordering
only at a real table (otherwise read-only, with a notice for an invalid code); checkout
without a typed table number; prices are the QR menu's (sales point qr_table — it showed
in-store prices since S.1 removed digital_price) and only products sold there; the
confirmation page for table orders (in the kitchen); the delivery/takeaway leftovers
removed (they have their own app since S.4); test ids for e2e.
- Tests: tests/test_qr.py (3); test_checkout's typed-table case moved there.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
156 lines
6.5 KiB
Python
156 lines
6.5 KiB
Python
import os
|
|
import uuid
|
|
from fastapi import APIRouter, Depends, HTTPException, Header, UploadFile, File, Form, status
|
|
from passlib.context import CryptContext
|
|
from sqlalchemy.orm import Session
|
|
|
|
from database import get_db
|
|
from models.site import Site
|
|
from models.menu_snapshot import MenuSnapshot
|
|
from models.product_image import ProductImage
|
|
from schemas.menu import MenuSyncRequest, MenuSyncResponse
|
|
|
|
router = APIRouter()
|
|
_pwd = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
PRODUCT_IMAGE_DIR = "/app/data/product_images"
|
|
|
|
|
|
def _require_site(
|
|
x_site_id: str = Header(..., alias="X-Site-ID"),
|
|
x_site_key: str = Header(..., alias="X-Site-Key"),
|
|
db: Session = Depends(get_db),
|
|
) -> Site:
|
|
site = db.query(Site).filter(Site.site_id == x_site_id).first()
|
|
if not site or not _pwd.verify(x_site_key, site.secret_key_hash):
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid site credentials")
|
|
return site
|
|
|
|
|
|
# ── Public ────────────────────────────────────────────────────────────────────
|
|
|
|
@router.get("/{site_slug}")
|
|
def get_menu(site_slug: str, t: str | None = None, db: Session = Depends(get_db)):
|
|
"""Return the latest menu snapshot for a site. Used by the public menu SPA. `t`: a table's
|
|
QR token (S.7) → `table` {id, label}, or `table_invalid` when it isn't one of the venue's."""
|
|
site = db.query(Site).filter(Site.site_id == site_slug).first()
|
|
if not site:
|
|
raise HTTPException(status_code=404, detail="Site not found")
|
|
|
|
snapshot = db.query(MenuSnapshot).filter(MenuSnapshot.site_id == site.id).first()
|
|
if not snapshot:
|
|
raise HTTPException(status_code=404, detail="No menu published yet")
|
|
|
|
import json
|
|
data = json.loads(snapshot.snapshot_json)
|
|
from services.checkout import delivery_areas
|
|
data["delivery_areas"] = delivery_areas(data) # S.3: what the customer picks from
|
|
data.pop("fees", None) # the rules and postcodes stay on the cloud
|
|
from services.ordering import summary
|
|
data["ordering"] = summary(data) # S.4: services, open now, slots
|
|
data["menu_mode"] = site.menu_mode
|
|
from services import qr
|
|
data["table_ordering"] = qr.ordering_on(data, site) # S.7: ordering at the table (with a table's QR)
|
|
if t is not None:
|
|
table = qr.table_for(data, t)
|
|
data["table"] = table
|
|
data["table_invalid"] = table is None
|
|
data.pop("qr", None) # the tables' token hashes stay on the cloud
|
|
data["restaurant"] = {
|
|
"name": site.menu_display_name,
|
|
"tagline": {"en": site.menu_tagline_en, "gr": site.menu_tagline_gr},
|
|
"blurb": {"en": site.menu_blurb_en, "gr": site.menu_blurb_gr},
|
|
"hours": site.menu_hours,
|
|
"headerImageUrl": site.menu_header_image_url,
|
|
}
|
|
|
|
cloud_images = {
|
|
img.product_id: img.image_url
|
|
for img in db.query(ProductImage).filter(ProductImage.site_id == site.id).all()
|
|
}
|
|
for cat in data.get("categories", []):
|
|
for product in cat.get("products", []):
|
|
# image_url as pushed by local_backend points at the restaurant's own
|
|
# LAN/local server and isn't reachable from the internet — always
|
|
# replace it with the cloud-hosted copy (or None) unless a manual
|
|
# digital_image_url override is set.
|
|
if not product.get("digital_image_url"):
|
|
product["image_url"] = cloud_images.get(product.get("id"))
|
|
|
|
return data
|
|
|
|
|
|
# ── Internal (site API key) ───────────────────────────────────────────────────
|
|
|
|
@router.post("/sync", response_model=MenuSyncResponse)
|
|
def sync_menu(body: MenuSyncRequest, site: Site = Depends(_require_site), db: Session = Depends(get_db)):
|
|
"""Upsert the menu snapshot for this site. Called by local_backend on each sync. Only the
|
|
authenticated site's own (before S.2 the body's site_id was trusted: one site could
|
|
overwrite another's menu)."""
|
|
if body.site_id != site.id:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Not this site's menu")
|
|
import json
|
|
try:
|
|
json.loads(body.snapshot_json)
|
|
except ValueError:
|
|
raise HTTPException(status_code=422, detail="snapshot_json is not JSON")
|
|
snapshot = db.query(MenuSnapshot).filter(MenuSnapshot.site_id == site.id).first()
|
|
if snapshot:
|
|
snapshot.snapshot_json = body.snapshot_json
|
|
else:
|
|
snapshot = MenuSnapshot(site_id=site.id, snapshot_json=body.snapshot_json)
|
|
db.add(snapshot)
|
|
db.commit()
|
|
return MenuSyncResponse(ok=True)
|
|
|
|
|
|
@router.post("/sync-image")
|
|
async def sync_product_image(
|
|
product_id: int = Form(...),
|
|
file: UploadFile = File(...),
|
|
site: Site = Depends(_require_site),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""Upload/replace the cloud-hosted copy of a product's image. Called by
|
|
local_backend during menu sync for products whose image changed."""
|
|
if not file.content_type or not file.content_type.startswith("image/"):
|
|
raise HTTPException(status_code=400, detail="File must be an image")
|
|
|
|
contents = await file.read()
|
|
|
|
import hashlib
|
|
image_hash = hashlib.sha256(contents).hexdigest()
|
|
|
|
record = (
|
|
db.query(ProductImage)
|
|
.filter(ProductImage.site_id == site.id, ProductImage.product_id == product_id)
|
|
.first()
|
|
)
|
|
|
|
os.makedirs(PRODUCT_IMAGE_DIR, exist_ok=True)
|
|
|
|
if record and record.image_hash == image_hash:
|
|
return {"ok": True, "image_url": record.image_url, "unchanged": True}
|
|
|
|
if record:
|
|
old_path = os.path.join(PRODUCT_IMAGE_DIR, os.path.basename(record.image_url))
|
|
if os.path.exists(old_path):
|
|
os.remove(old_path)
|
|
|
|
ext = file.filename.rsplit(".", 1)[-1].lower() if file.filename and "." in file.filename else "jpg"
|
|
filename = f"{site.id}_{product_id}_{uuid.uuid4().hex[:8]}.{ext}"
|
|
filepath = os.path.join(PRODUCT_IMAGE_DIR, filename)
|
|
with open(filepath, "wb") as f:
|
|
f.write(contents)
|
|
|
|
image_url = f"/static/product_images/{filename}"
|
|
if record:
|
|
record.image_url = image_url
|
|
record.image_hash = image_hash
|
|
else:
|
|
record = ProductImage(site_id=site.id, product_id=product_id, image_url=image_url, image_hash=image_hash)
|
|
db.add(record)
|
|
db.commit()
|
|
|
|
return {"ok": True, "image_url": image_url, "unchanged": False}
|