- 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>
152 lines
8.0 KiB
Python
152 lines
8.0 KiB
Python
"""The menu tablet (plan step 5.7a): the menu for a paired tablet, categories in the catalogue's
|
|
order with their products (a product in several categories shows in each), a sales point's live
|
|
prices, sold out crossed out or hidden, one category or all; menu changes reach the tablets."""
|
|
import pytest
|
|
|
|
from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures)
|
|
from tests.test_media import jpeg, media_dir, up # noqa: F401 (fixture + helpers)
|
|
|
|
|
|
@pytest.fixture
|
|
def menu(db):
|
|
from models.product import Category, Product, ProductCategory
|
|
grill, drinks = Category(name="Grill", sort_order=0), Category(name="Drinks", sort_order=1)
|
|
db.add_all([grill, drinks])
|
|
db.flush()
|
|
pita = Category(name="Pita", sort_order=0, parent_id=grill.id)
|
|
db.add(pita)
|
|
db.flush()
|
|
|
|
def prod(name, cats, price, **kw):
|
|
p = Product(name=name, base_price=price, primary_category_id=cats[0].id, **kw)
|
|
db.add(p)
|
|
db.flush()
|
|
for i, c in enumerate(cats):
|
|
db.add(ProductCategory(product_id=p.id, category_id=c.id, sort_order=kw.get("sort_order", 0)))
|
|
return p
|
|
out = {"steak": prod("Steak", [grill], 12.0, description="Pork, 300 g", tags='["gluten-free"]', sort_order=1),
|
|
"souvlaki": prod("Souvlaki", [grill, pita], 3.5, sort_order=0),
|
|
"beer": prod("Beer", [drinks], 4.5),
|
|
"wine": prod("Wine", [drinks], 5.0, sold_at='["online_delivery"]'),
|
|
"old": prod("Old", [grill], 1.0, lifecycle_status="archived")}
|
|
db.commit()
|
|
out["cats"] = {"grill": grill.id, "drinks": drinks.id, "pita": pita.id}
|
|
return out
|
|
|
|
|
|
@pytest.fixture
|
|
def tablet(pair):
|
|
token, r = pair("menu_tablet", "Entrance")
|
|
assert r.status_code == 201, r.text
|
|
return {"h": {"X-Xenia-Device": token}, "id": r.json()["id"]}
|
|
|
|
|
|
def names(data):
|
|
return [(c["name"], [data["products"][str(i)]["name"] for i in c["products"]]) for c in data["categories"]]
|
|
|
|
|
|
def test_the_menu_on_a_tablet(client, owner_h, tablet, menu):
|
|
photo = up(client, owner_h, jpeg(800, 600)).json()
|
|
client.put(f"/api/products/{menu['steak'].id}", headers=owner_h, json={"media_id": photo["id"]})
|
|
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
|
|
assert names(data) == [("Grill", ["Souvlaki", "Steak"]), ("Pita", ["Souvlaki"]), ("Drinks", ["Beer"])] # Wine: delivery only
|
|
assert [c["depth"] for c in data["categories"]] == [0, 1, 0]
|
|
steak = data["products"][str(menu["steak"].id)]
|
|
assert (steak["price"], steak["description"], steak["tags"], steak["image"]["card"]) == (12.0, "Pork, 300 g", ["gluten-free"], photo["urls"]["card"])
|
|
assert data["config"]["view"] == "expanded" and data["config"]["search"] is True
|
|
|
|
|
|
def test_the_tablets_settings_change_the_menu(client, owner_h, tablet, menu, db):
|
|
menu["beer"].is_available = False
|
|
db.commit()
|
|
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
|
|
assert data["products"][str(menu["beer"].id)]["available"] is False
|
|
cfg = {"sold_out": "hide", "category": menu["cats"]["grill"], "sales_point": "none", "view": "full"}
|
|
assert client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": cfg}).status_code == 200
|
|
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
|
|
assert names(data) == [("Grill", ["Souvlaki", "Steak"]), ("Pita", ["Souvlaki"])]
|
|
assert {p["price"] for p in data["products"].values()} == {None} and data["config"]["view"] == "full"
|
|
client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"sales_point": "online_delivery"}})
|
|
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
|
|
assert names(data)[0][0] == "Grill" and None not in {p["price"] for p in data["products"].values()} # prices again, still only Grill
|
|
assert err(client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"view": "huge"}})) == "validation.device_config"
|
|
|
|
|
|
def test_menu_changes_reach_the_tablets(client, owner_h, tablet, menu, db, monkeypatch):
|
|
import routers.menu_tablet as mt
|
|
sent = []
|
|
monkeypatch.setattr("services.ws_bus.broadcast_sync", lambda kind, data, **kw: sent.append((kind, kw.get("device_ids"))))
|
|
mt.menu_changed(db)
|
|
assert sent == [("menu_tablet.changed", [tablet["id"]])]
|
|
client.put(f"/api/products/{menu['steak'].id}", headers=owner_h, json={"base_price": 13.5})
|
|
assert client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()["products"][str(menu["steak"].id)]["price"] == 13.5
|
|
|
|
|
|
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"
|