feat: call a waiter from a menu tablet at a table (platform-v2 step 5.7b)

- backend: menu tablet device config `table` (ref, validated against active
  tables) and `call_waiter`; GET /api/menu-tablet/menu gives the table and
  whether it can call; POST /api/menu-tablet/call sends a `table_call` staff
  message ("Table 5: the guests call a waiter.", venue language) to the table's
  waiters (its open order's, else the zone's), at most once a minute per tablet
  (menu_tablet.call_too_soon with the seconds left; menu_tablet.no_table).
- terminal: "Call a waiter" only on a tablet at a table; "A waiter is on the
  way to table 5", then a countdown before it can call again; a failed call
  says to ask at the counter.
- waiter: the call shows with its own look and "Open the table".
- manager: tables as the choices of a device setting.
- tests: backend +2 (535 → 533 passed total), e2e menu_tablet.mjs +3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 00:04:39 +02:00
co-authored by Claude Opus 5.5
parent cc81e0a31f
commit f8d0d29d8f
19 changed files with 254 additions and 12 deletions
+66
View File
@@ -9,6 +9,9 @@ products and their categories: no ordering, no tables, no sign-in.
of the tablet's sales point and whether it is available; the
tablet's settings (device config) and the venue's name.
POST /api/menu-tablet/call (a tablet set at a table, 5.7b) call a waiter: a message to the
table's waiters (its order's, else the zone's), once a minute at most.
How it looks is the tablet's device config (modules/menu_tablet/manifest.py): view, photos,
descriptions, grouping, whose prices, sold-out style, search, one category or all, languages,
idle reset, light / dark. Any menu change sends `menu_tablet.changed` to every tablet (main.py).
@@ -17,14 +20,21 @@ from __future__ import annotations
import json
import threading
import time
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from core import sales_points
from core.access.deps import require_device
from core.errors import XeniaError
from database import get_db
router = APIRouter()
CALL_EVERY_S = 60
_calls: dict[int, float] = {} # tablet id → its last call (monotonic)
_lock = threading.Lock()
def _config(dev) -> dict:
@@ -107,11 +117,67 @@ def menu(db: Session = Depends(get_db), dev=Depends(require_device("menu_tablet"
shown = [by_id[i] for i in by_id if i in used]
prices = dict(zip([p.id for p in shown], unit_prices(db, shown, point))) if point and shown else {}
name_row = db.query(PosSettings).filter(PosSettings.key == "venue.name").first()
table = _table(db, cfg)
return {
"venue": {"name": (name_row.value if name_row else "") or None},
"table": {"id": table.id, "name": _label(table), "can_call": bool(cfg.get("call_waiter"))} if table else None,
"config": cfg,
"categories": out_cats,
"products": {str(p.id): {"id": p.id, "name": p.name, "description": p.description or None, "tags": _tags(p),
"price": round(prices[p.id], 2) if p.id in prices else None, "available": bool(p.is_available),
"image": media_store.image_of(p.media)} for p in shown},
}
# ── calling a waiter from a tablet at a table (5.7b) ──────────────────────────
def _table(db, cfg):
from models.table import Table
t = db.get(Table, cfg["table"]) if cfg.get("table") else None
return t if t is not None and t.is_active else None
def _label(table) -> str:
from services.table_qr import label
return label(table)
def _waiters(db, table) -> list[int]:
"""The table's waiters: its open order's, else the waiters whose zone covers the table."""
from models.order import Order
from services.qr_orders import waiters_for
order = db.query(Order).filter(Order.table_id == table.id, Order.status.in_(("open", "partially_paid", "paid"))).first()
class _NoOrder:
waiters = []
return waiters_for(db, order or _NoOrder(), table)
@router.post("/call")
def call_waiter(db: Session = Depends(get_db), dev=Depends(require_device("menu_tablet"))):
"""A guest at the table asks for a waiter. The message reaches the table's waiters' phones."""
from core.access.queries import holders_filter
from core.i18n import t, venue_language
from models.user import User
from routers.kds import _save_and_broadcast_message
cfg = _config(dev)
table = _table(db, cfg)
if table is None or not cfg.get("call_waiter"):
raise XeniaError("menu_tablet.no_table")
now = time.monotonic()
with _lock:
last = _calls.get(dev.id)
if last is not None and now - last < CALL_EVERY_S:
raise XeniaError("menu_tablet.call_too_soon", seconds=int(CALL_EVERY_S - (now - last)) + 1)
_calls[dev.id] = now
sender = db.query(User).filter(holders_filter("core.dashboard.access")).first()
lang = venue_language()
waiters = _waiters(db, table)
_save_and_broadcast_message(db, sender.id if sender else 1, t("tablet.call_message", lang, table=_label(table)), waiters,
"table_call", t("tablet.sender", lang), table_ids=[table.id])
return {"table": {"id": table.id, "name": _label(table)}, "waiters": len(waiters), "again_in": CALL_EVERY_S}
def reset_calls() -> None:
with _lock:
_calls.clear()