"""The QR menu at the table (plan phase 3S, step S.7): each table's QR code carries a random token (the cloud gets only its hash); an order placed with it goes straight onto the table's order, to the kitchen, and the table's waiters get a message; a new code replaces the old.""" import json import pytest from tests.test_live import err, owner_h, pair, terminal_login # noqa: F401 (fixtures) from tests.test_pos import drawer_printer, menu, till # noqa: F401 (fixtures) from tests.test_pos_online import cloud, counter, kds_ids # noqa: F401 (fixtures) @pytest.fixture def floor(db): from models.table import Table, TableGroup terrace, hall = TableGroup(name="Terrace"), TableGroup(name="Hall") db.add_all([terrace, hall]) db.flush() t5 = Table(number=5, label="T5", group_id=terrace.id, is_active=True) t9 = Table(number=9, group_id=hall.id, is_active=True) old = Table(number=11, group_id=hall.id, is_active=False) db.add_all([t5, t9, old]) db.commit() return {"t5": t5, "t9": t9, "old": old, "terrace": terrace, "hall": hall} def pull_at_table(db, menu, table_id, cloud_id=801, qty=2): from services.cloud_sync import store_online_orders at_table = [] order = { "id": cloud_id, "public_ref": f"ORD-{cloud_id:04d}", "order_type": "dine_in", "sales_point": "qr_table", "customer_name": "Table T5", "table_id": table_id, "table_label": "T5", "status": "pending_acceptance", "items_json": json.dumps([{"product_id": menu["coffee"].id, "name": "Freddo", "quantity": qty, "unit_price": 3.0, "options": []}]), "subtotal": 3.0 * qty, "delivery_fee": 0.0, "total": 3.0 * qty, } return store_online_orders(db, [order], None, at_table)[cloud_id], at_table def test_qr_codes_per_table_and_what_the_cloud_gets(client, db, owner_h, floor, monkeypatch): from services import cloud_sync, table_qr monkeypatch.setitem(cloud_sync.license_state, "apps_url", "https://menu.example.com") monkeypatch.setattr(cloud_sync.settings, "SITE_ID", "taverna-1") r = client.get("/api/connect/qr", headers=owner_h) assert r.status_code == 200, r.text data = r.json() assert data["ordering"] is False and data["menu_url"] == "https://menu.example.com/menu/taverna-1" assert [(t["name"], t["group"]) for t in data["tables"]] == [("T5", "Terrace"), ("9", "Hall")] # the inactive one has none db.refresh(floor["t5"]) token = floor["t5"].qr_token assert len(token) >= 20 and data["tables"][0]["url"] == f"https://menu.example.com/menu/taverna-1?t={token}" # the snapshot has the hashes only snap, _ = cloud_sync.menu_snapshot(db) assert snap["qr"]["ordering"] is False assert {"id": floor["t5"].id, "label": "T5", "token_hash": table_qr.token_hash(token)} in snap["qr"]["tables"] assert token not in json.dumps(snap) # ordering at the table on; a new code for table 5 assert client.put("/api/connect/qr/ordering", headers=owner_h, json={"on": True}).json()["ordering"] is True rotated = client.post(f"/api/connect/qr/tables/{floor['t5'].id}/rotate", headers=owner_h).json() db.refresh(floor["t5"]) assert floor["t5"].qr_token != token and rotated["tables"][0]["url"].endswith(floor["t5"].qr_token) assert cloud_sync.menu_snapshot(db)[0]["qr"]["ordering"] is True assert err(client.post(f"/api/connect/qr/tables/{floor['old'].id}/rotate", headers=owner_h)) == "reservation.table_unknown" def test_qr_codes_need_the_key(client, pair, make_user): token, _ = pair("pos_counter", "Till 2") jwt = terminal_login(client, token, make_user(role="waiter")) assert err(client.get("/api/connect/qr", headers={"X-Xenia-Device": token, "Authorization": f"Bearer {jwt}"})) in ("access.denied", "device.out_of_scope") def test_an_order_at_the_table_goes_on_its_order(client, db, counter, menu, owner_h, cloud, floor, make_user, monkeypatch): from models.message import StaffMessage from models.order import Order, OrderItem from models.user import WaiterZone import routers.kds as kds sent = [] real = kds.broadcast_sync monkeypatch.setattr(kds, "broadcast_sync", lambda kind, data, **kw: (sent.append((kind, data, kw)), real(kind, data, **kw))) import services.qr_orders as qo # noqa: F401 terrace_waiter, hall_waiter = make_user(role="waiter", nickname="Nikos"), make_user(role="waiter", nickname="Maria") db.add_all([WaiterZone(waiter_id=terrace_waiter.id, group_id=floor["terrace"].id), WaiterZone(waiter_id=hall_waiter.id, group_id=floor["hall"].id)]) # table 5 already has an order (a waiter's) open_order = Order(table_id=floor["t5"].id, opened_by=terrace_waiter.id, status="open", business_day_id=counter["day"].id) db.add(open_order) db.flush() db.add(OrderItem(order_id=open_order.id, product_id=menu["burger"].id, added_by=terrace_waiter.id, quantity=1, unit_price=10.0, status="active", printed=True)) db.commit() local, at_table = pull_at_table(db, menu, floor["t5"].id) assert local == open_order.id and at_table == [801] db.expire_all() order = db.get(Order, open_order.id) assert [(i.product_name, i.quantity, i.unit_price) for i in order.items] == [("Burger", 1, 10.0), ("Freddo", 2, 3.0)] added = [i.id for i in order.items if i.product_name == "Freddo"] assert cloud["printed"] == [(order.id, added)] # to the kitchen, like a waiter's assert order.id in kds_ids(client, owner_h) msg = db.query(StaffMessage).filter(StaffMessage.message_type == "qr_order").one() assert json.loads(msg.target_waiter_ids) == [terrace_waiter.id] and "T5" in msg.body and "2" in msg.body assert any(k == "message_sent" and kw.get("user_ids") == [terrace_waiter.id] for k, _, kw in sent) # a repeated pull doesn't add it twice again, at_table = pull_at_table(db, menu, floor["t5"].id) assert again == -order.id and at_table == [801] assert db.query(OrderItem).filter(OrderItem.order_id == order.id).count() == 2 def test_a_free_table_gets_a_new_order_and_its_zone_waiters_hear(db, counter, menu, cloud, floor, make_user): from models.message import StaffMessage from models.order import Order from models.user import WaiterZone hall_waiter, everywhere = make_user(role="waiter"), make_user(role="waiter") db.add_all([WaiterZone(waiter_id=hall_waiter.id, group_id=floor["hall"].id), WaiterZone(waiter_id=everywhere.id, group_id=None)]) db.commit() local, _ = pull_at_table(db, menu, floor["t9"].id, cloud_id=802, qty=1) order = db.get(Order, local) assert (order.table_id, order.source, order.sales_point, order.status) == (floor["t9"].id, "pos", "in_store", "open") assert [i.unit_price for i in order.items] == [3.0] assert [i.source for i in order.items] == ["qr"] # marked on the phones (4.8) msg = db.query(StaffMessage).filter(StaffMessage.message_type == "qr_order").one() assert sorted(json.loads(msg.target_waiter_ids)) == sorted([hall_waiter.id, everywhere.id]) def test_no_usable_table_it_waits_at_the_counter(client, db, counter, menu, cloud, floor): from models.order import Order local, at_table = pull_at_table(db, menu, floor["old"].id, cloud_id=803) order = db.get(Order, local) assert at_table == [] and order.table_id is None and order.source == "online" and order.online_status == "pending_acceptance" assert "T5" in (order.online_customer_notes or "") assert cloud["printed"] == []