Plan phase 3S, step S.7. - tables.qr_token (migration 0021): a random token per active table; services/table_qr.py (tokens, rotate, the snapshot's `qr` with the tokens' sha256 only, the code's link from the cloud's apps_url); setting connect_menu.table_ordering (read-only by default). - /api/connect/qr (connect_menu.manage): the codes, ordering on/off, a new code per table or for all; table and QR changes push the menu at once (MENU_PATHS). - Pull: a QR order with a table id goes onto the table's open order or a new one (services/qr_orders.py): items at the cloud's prices, kitchen tickets, KDS, order_updated (the waiters' phones and the counter), a `qr_order` message to the order's waiters or the table's zone waiters; audit QR_ORDER (a repeated pull adds nothing); the cloud is told "accepted" (the guest's page: in the kitchen). No usable table or no open day → the counter gets it as before. Waiter app: the message's look. - Manager: Online → Table QR codes (/online/qr): ordering switch, the menu link, a QR card per table (print all), new code per table / for all. - Heartbeat: apps_url kept in the license state. - Tests: tests/test_qr_table.py (5); e2e qr_table.mjs (a real cloud and venue: table 5's code → two dishes on table 5's order, KDS, live event, the guest's page; a forged code refused; a new code kills the old one), in run-all. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
"""The tables' QR codes (plan phase 3S, step S.7). Key: connect_menu.manage.
|
|
|
|
GET /api/connect/qr {ordering, menu_url, tables: [{id, name, group, url}]}
|
|
PUT /api/connect/qr/ordering {on}: the QR menu takes orders at the table, or is read-only
|
|
POST /api/connect/qr/tables/{id}/rotate a new code for one table (the printed one stops working)
|
|
POST /api/connect/qr/rotate new codes for every table
|
|
|
|
Each change pushes the menu soon (main.py middleware, MENU_PATHS), so the cloud knows the new
|
|
codes within seconds. services/table_qr.py has the rules.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from core.access.deps import require
|
|
from core.errors import XeniaError
|
|
from database import get_db
|
|
from services import table_qr
|
|
|
|
router = APIRouter()
|
|
KEY = "connect_menu.manage"
|
|
|
|
|
|
class OrderingBody(BaseModel):
|
|
on: bool
|
|
|
|
|
|
def _out(db) -> dict:
|
|
from models.table import Table, TableGroup
|
|
table_qr.ensure_tokens(db)
|
|
db.commit()
|
|
groups = {g.id: g.name for g in db.query(TableGroup).all()}
|
|
tables = db.query(Table).filter(Table.is_active.is_(True)).order_by(Table.group_id, Table.number).all()
|
|
return {
|
|
"ordering": table_qr.ordering_on(db),
|
|
"menu_url": table_qr.menu_url(),
|
|
"tables": [{"id": t.id, "name": table_qr.label(t), "number": t.number, "group": groups.get(t.group_id),
|
|
"url": table_qr.menu_url(t.qr_token)} for t in tables],
|
|
}
|
|
|
|
|
|
@router.get("")
|
|
def qr_codes(db: Session = Depends(get_db), user=Depends(require(KEY))):
|
|
return _out(db)
|
|
|
|
|
|
@router.put("/ordering")
|
|
def set_ordering(body: OrderingBody, db: Session = Depends(get_db), user=Depends(require(KEY))):
|
|
table_qr.set_ordering(db, body.on)
|
|
db.commit()
|
|
return _out(db)
|
|
|
|
|
|
@router.post("/tables/{table_id}/rotate")
|
|
def rotate_one(table_id: int, db: Session = Depends(get_db), user=Depends(require(KEY))):
|
|
from models.table import Table
|
|
table = db.get(Table, table_id)
|
|
if table is None or not table.is_active:
|
|
raise XeniaError("reservation.table_unknown", table_id=table_id)
|
|
table_qr.rotate(db, table)
|
|
db.commit()
|
|
return _out(db)
|
|
|
|
|
|
@router.post("/rotate")
|
|
def rotate_all(db: Session = Depends(get_db), user=Depends(require(KEY))):
|
|
from models.table import Table
|
|
for t in db.query(Table).filter(Table.is_active.is_(True)).all():
|
|
table_qr.rotate(db, t)
|
|
db.commit()
|
|
return _out(db)
|