- Counter POS gets an "Online" mode (module on + connect_orders.manage): New / In
progress / Finished, a chime on arrival (and every 30 s while one waits), badge,
toast in any mode; the order panel offers only the next step: accept / reject with
a reason, preparing, ready or out for delivery, then take the payment (shared
PaymentDialog, drawer, shift) and mark delivered/served.
- connect_orders router on v2 conventions: require(connect_orders.manage), error
codes online.not_found / online.status_changed, the cloud's step order enforced,
audit rows, GET /api/connect/orders/board. POS device scope gains connect_orders.*;
barista and barman get the key by default.
- Fixes: the KDS showed online orders before acceptance; accepting printed no
kitchen tickets; a pull whose synced mark failed stored the order twice
(store_online_orders skips known cloud ids); pulled orders lacked the business day
and delivery type; rejected orders kept active items; delivered sets closed_at/by.
- Online orders belong to holders of connect_orders.manage (_can_access_order), so
the counter pays them with POST /api/pos/orders/{id}/pay.
- Tests: test_pos_online.py (5), terminal unit +1, terminal.mjs +17 with a fake
cloud (pull, synced, status mirror); fixed a timing race in a 3.3c check.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
171 lines
9.4 KiB
Python
171 lines
9.4 KiB
Python
"""Counter POS: online orders (plan step 3.3d). An order pulled from the cloud waits for the
|
|
counter, which accepts (kitchen tickets, KDS) or rejects it, moves it along, takes the payment
|
|
and finishes it; every change is broadcast and mirrored to the cloud."""
|
|
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)
|
|
|
|
|
|
@pytest.fixture
|
|
def counter(client, db, pair, make_user):
|
|
"""A counter screen with a barista (the role accepts online orders by default) on shift."""
|
|
from core.timeutil import business_date_of, utcnow
|
|
from models.business_day import BusinessDay
|
|
from models.shift import WaiterShift
|
|
token, r = pair("pos_counter", "Counter")
|
|
assert r.status_code == 201, r.text
|
|
barista = make_user(role="barista", nickname="Eleni")
|
|
jwt = terminal_login(client, token, barista)
|
|
day = db.query(BusinessDay).filter(BusinessDay.status == "open").first()
|
|
if day is None:
|
|
day = BusinessDay(status="open", opened_at=utcnow(), business_date=business_date_of(utcnow()),
|
|
opened_by_id=make_user(role="owner").id)
|
|
db.add(day)
|
|
db.flush()
|
|
shift = WaiterShift(waiter_id=barista.id, business_day_id=day.id, starting_cash=20)
|
|
db.add(shift)
|
|
db.commit()
|
|
return {"h": {"X-Xenia-Device": token, "Authorization": f"Bearer {jwt}"}, "user": barista, "id": r.json()["id"],
|
|
"shift": shift, "day": day}
|
|
|
|
|
|
@pytest.fixture
|
|
def cloud(monkeypatch):
|
|
"""What reaches the cloud (status mirroring) and what the kitchen printers get."""
|
|
import routers.connect_orders as co
|
|
import services.printer_service as ps
|
|
got = {"mirror": [], "printed": [], "events": []}
|
|
|
|
async def mirror(cloud_id, status, reason=None):
|
|
got["mirror"].append((cloud_id, status, reason))
|
|
monkeypatch.setattr(co, "_mirror_status_to_cloud", mirror)
|
|
monkeypatch.setattr(ps, "route_and_print_sync", lambda order_id, ids, db, **kw: (got["printed"].append((order_id, sorted(ids))), [])[1])
|
|
real = co.broadcast_sync
|
|
monkeypatch.setattr(co, "broadcast_sync", lambda kind, data, **kw: (got["events"].append((kind, data)), real(kind, data, **kw)))
|
|
return got
|
|
|
|
|
|
def pulled(db, menu, cloud_id=501, order_type="delivery", **extra):
|
|
"""Store an order exactly as the cloud pull does (services/cloud_sync.py)."""
|
|
from services.cloud_sync import store_online_orders
|
|
order = {
|
|
"id": cloud_id, "public_ref": f"ORD-{cloud_id:04d}", "order_type": order_type,
|
|
"customer_name": "Nikos", "customer_phone": "6900000000", "customer_address": "Main square 3",
|
|
"customer_notes": "Ring twice", "status": "pending_acceptance",
|
|
"items_json": json.dumps([
|
|
{"product_id": menu["burger"].id, "name": "Burger", "quantity": 2, "unit_price": 9.5, "options": []},
|
|
{"product_id": menu["coffee"].id, "name": "Freddo", "quantity": 1, "unit_price": 3.0, "options": []},
|
|
]),
|
|
"subtotal": 22.0, "delivery_fee": 0.0, "total": 22.0, **extra,
|
|
}
|
|
return store_online_orders(db, [order])[cloud_id]
|
|
|
|
|
|
def board(client, counter):
|
|
r = client.get("/api/connect/orders/board", headers=counter["h"])
|
|
assert r.status_code == 200, r.text
|
|
return r.json()
|
|
|
|
|
|
def kds_ids(client, owner_h):
|
|
return {o["id"] for o in client.get("/api/kds/orders", headers=owner_h).json()["orders"]}
|
|
|
|
|
|
def test_a_pulled_order_waits_for_the_counter(client, db, counter, menu, owner_h):
|
|
from models.order import Order
|
|
oid = pulled(db, menu)
|
|
order = db.get(Order, oid)
|
|
assert order.online_status == "pending_acceptance" and order.order_type == "delivery"
|
|
assert order.business_day_id == counter["day"].id
|
|
assert [(i.product_name, i.quantity, i.unit_price) for i in order.items] == [("Burger", 2, 9.5), ("Freddo", 1, 3.0)]
|
|
# a second pull of the same order (its "synced" mark got lost) doesn't store it twice
|
|
assert pulled(db, menu) == -oid
|
|
assert db.query(Order).filter(Order.online_order_cloud_id == 501).count() == 1
|
|
b = board(client, counter)
|
|
assert [o["id"] for o in b["incoming"]] == [oid] and b["in_progress"] == []
|
|
o = b["incoming"][0]
|
|
assert o["ref"] == "ORD-0501" and o["type"] == "delivery" and o["total"] == 22.0 and o["unpaid"] == 22.0
|
|
assert o["customer"] == {"name": "Nikos", "phone": "6900000000", "address": "Main square 3", "notes": "Ring twice"}
|
|
assert [i["product_name"] for i in o["items"]] == ["Burger", "Freddo"]
|
|
# the kitchen doesn't see it yet: it may still be rejected
|
|
assert oid not in kds_ids(client, owner_h)
|
|
|
|
|
|
def test_online_orders_need_the_key(client, pair, make_user, menu, db):
|
|
token, _ = pair("pos_counter", "Other till")
|
|
jwt = terminal_login(client, token, make_user(role="waiter")) # no connect_orders.manage
|
|
h = {"X-Xenia-Device": token, "Authorization": f"Bearer {jwt}"}
|
|
oid = pulled(db, menu)
|
|
assert err(client.get("/api/connect/orders/board", headers=h)) == "access.denied"
|
|
assert err(client.post(f"/api/connect/orders/{oid}/accept", headers=h)) == "access.denied"
|
|
|
|
|
|
def test_accept_sends_it_to_the_kitchen(client, db, counter, menu, owner_h, cloud):
|
|
oid = pulled(db, menu)
|
|
r = client.post(f"/api/connect/orders/{oid}/accept", headers=counter["h"])
|
|
assert r.status_code == 200, r.text
|
|
assert r.json()["online_status"] == "accepted"
|
|
items = sorted(i["id"] for i in r.json()["items"])
|
|
assert cloud["printed"] == [(oid, items)] # kitchen tickets
|
|
assert oid in kds_ids(client, owner_h) # and the KDS
|
|
assert cloud["mirror"] == [(501, "accepted", None)] # the customer's page follows
|
|
assert ("online_order_updated", {"order_id": oid, "status": "accepted"}) in cloud["events"]
|
|
assert ("order_updated", {"order_id": oid, "table_id": None, "status": "open", "action": "items_added", "item_ids": items}) in cloud["events"]
|
|
b = board(client, counter)
|
|
assert b["incoming"] == [] and [o["id"] for o in b["in_progress"]] == [oid]
|
|
# accepted on another screen meanwhile → refused, nothing printed again
|
|
again = client.post(f"/api/connect/orders/{oid}/accept", headers=counter["h"])
|
|
assert again.status_code == 409 and err(again) == "online.status_changed"
|
|
assert len(cloud["printed"]) == 1
|
|
|
|
|
|
def test_delivery_paid_at_the_counter_and_finished(client, db, counter, menu, owner_h, cloud, drawer_printer):
|
|
from models.order import Order
|
|
from models.printer import Printer
|
|
printer = db.query(Printer).filter(Printer.name == "Counter receipt").one() # drawer_printer's
|
|
assert client.patch(f"/api/devices/{counter['id']}", headers=owner_h,
|
|
json={"config": {"receipt_printer_id": printer.id}}).status_code == 200
|
|
oid = pulled(db, menu)
|
|
h = counter["h"]
|
|
step = lambda s: client.post(f"/api/connect/orders/{oid}/status", headers=h, json={"status": s}) # noqa: E731
|
|
assert err(step("preparing")) == "online.status_changed" # not accepted yet
|
|
client.post(f"/api/connect/orders/{oid}/accept", headers=h)
|
|
assert err(step("ready")) == "online.status_changed" # one step at a time
|
|
assert step("preparing").json()["online_status"] == "preparing"
|
|
assert step("out_for_delivery").json()["online_status"] == "out_for_delivery"
|
|
assert step("nonsense").status_code == 422
|
|
# the driver brings the cash: the counter's ordinary payment (drawer, the barista's shift)
|
|
o = board(client, counter)["in_progress"][0]
|
|
ids = [i["id"] for i in o["items"]]
|
|
paid = client.post(f"/api/pos/orders/{oid}/pay", headers=h, json={"payments": [{"method": "cash", "item_ids": ids}], "cash_given": 30})
|
|
assert paid.status_code == 200, paid.text
|
|
assert paid.json()["total"] == 22.0 and paid.json()["change"] == 8.0 and paid.json()["drawer"] == "opened"
|
|
db.expire_all()
|
|
assert all(i.paid_in_shift_id == counter["shift"].id for i in db.get(Order, oid).items)
|
|
assert len(drawer_printer) == 1 # the drawer kick
|
|
done = step("delivered")
|
|
assert done.status_code == 200 and done.json()["online_status"] == "delivered" and done.json()["status"] == "closed"
|
|
assert oid not in kds_ids(client, owner_h)
|
|
b = board(client, counter)
|
|
assert b["in_progress"] == [] and b["finished"][0]["id"] == oid and b["finished"][0]["unpaid"] == 0
|
|
assert [m[1] for m in cloud["mirror"]] == ["accepted", "preparing", "out_for_delivery", "delivered"]
|
|
|
|
|
|
def test_reject_tells_the_customer_why(client, db, counter, menu, owner_h, cloud):
|
|
from models.order import Order
|
|
oid = pulled(db, menu, order_type="dine_in")
|
|
assert db.get(Order, oid).order_type == "here"
|
|
r = client.post(f"/api/connect/orders/{oid}/reject", headers=counter["h"], json={"reason": "Kitchen closed"})
|
|
assert r.status_code == 200, r.text
|
|
assert r.json()["online_status"] == "rejected" and r.json()["status"] == "cancelled"
|
|
assert {i["status"] for i in r.json()["items"]} == {"cancelled"}
|
|
assert cloud["mirror"] == [(501, "rejected", "Kitchen closed")] and cloud["printed"] == []
|
|
assert oid not in kds_ids(client, owner_h)
|
|
b = board(client, counter)
|
|
assert b["incoming"] == [] and b["finished"][0]["status"] == "rejected" and b["finished"][0]["total"] == 0
|
|
assert err(client.post(f"/api/connect/orders/{oid}/accept", headers=counter["h"])) == "online.status_changed"
|
|
assert err(client.post("/api/connect/orders/99999/accept", headers=counter["h"])) == "online.not_found"
|