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
+63
View File
@@ -86,3 +86,66 @@ def test_only_menu_tablets(client, pair):
token, _ = pair("kds", "Kitchen")
assert err(client.get("/api/menu-tablet/menu", headers={"X-Xenia-Device": token})) == "device.out_of_scope"
assert err(client.get("/api/menu-tablet/menu")) == "device.not_paired"
# ── 5.7b: calling a waiter from a tablet at a table ─────────────────────────
@pytest.fixture
def table(db):
from models.table import Table
t = Table(number=5, is_active=True)
db.add(t)
db.commit()
return t
@pytest.fixture(autouse=True)
def _calls():
import routers.menu_tablet as mt
mt.reset_calls()
yield
mt.reset_calls()
def test_a_tablet_at_a_table_calls_a_waiter(client, owner_h, tablet, menu, table, make_user, db, monkeypatch):
"""Done when (5.7b): a tablet set to table 5 calls a waiter and the waiter gets the message."""
import routers.kds as kds
from models.user import WaiterZone
maria = make_user(role="waiter")
db.add(WaiterZone(waiter_id=maria.id, group_id=None))
db.commit()
got = []
monkeypatch.setattr(kds, "broadcast_sync", lambda kind, data, **kw: got.append((kind, data, kw)), raising=False)
assert client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()["table"] is None
assert err(client.post("/api/menu-tablet/call", headers=tablet["h"])) == "menu_tablet.no_table"
client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"table": table.id}})
assert client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()["table"] == {"id": table.id, "name": "5", "can_call": True}
r = client.post("/api/menu-tablet/call", headers=tablet["h"])
assert r.status_code == 200 and r.json()["waiters"] >= 1 and r.json()["again_in"] == 60
from models.message import StaffMessage
msg = db.query(StaffMessage).order_by(StaffMessage.id.desc()).first()
assert msg.message_type == "table_call" and msg.body == "Table 5: the guests call a waiter." and str(maria.id) in msg.target_waiter_ids
assert str(table.id) in msg.table_ids
# once a minute at most
again = client.post("/api/menu-tablet/call", headers=tablet["h"])
assert again.status_code == 429 and err(again) == "menu_tablet.call_too_soon" and again.json()["error"]["params"]["seconds"] <= 60
# the option off: no calls, the menu says so
client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"table": table.id, "call_waiter": False}})
assert client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()["table"]["can_call"] is False
assert err(client.post("/api/menu-tablet/call", headers=tablet["h"])) == "menu_tablet.no_table"
def test_the_tables_open_order_decides_who_hears_it(client, owner_h, tablet, table, make_user, db):
from models.order import Order, OrderWaiter
from models.message import StaffMessage
nikos, eleni = make_user(role="waiter"), make_user(role="waiter")
order = Order(table_id=table.id, opened_by=nikos.id, status="open", order_type="dine_in")
db.add(order)
db.flush()
db.add(OrderWaiter(order_id=order.id, waiter_id=nikos.id))
db.commit()
client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"table": table.id}})
assert client.post("/api/menu-tablet/call", headers=tablet["h"]).status_code == 200
msg = db.query(StaffMessage).order_by(StaffMessage.id.desc()).first()
assert msg.target_waiter_ids == f"[{nikos.id}]" and eleni.id
assert err(client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"table": 99999}})) == "validation.device_config"