- manager: new pages/orders/ (Tables page cards + quick actions, order page with
lines, multi-select pay/discount/cancel/tab, staff + customer, price changes,
history, the menu + cart beside it); a free table opens a new order on first
Send; v1 TablesPage, OrderDetailPage, DesktopOrderingTab, ManagerOrderDrawer
and PaymentMethodModal removed
- ordering by line id like the phones: GET /api/orders/menu (the counter's
product shape, in-store prices); the product dialog, cart rules and quote hook
move to packages/ordering (@xenia/ordering), shared by counter, menu tablet,
manager
- close follows orders.close_with_unpaid (pay first, or write off after saying
so) on the order page and the overview's quick view; paying asks cash or card
- a confirmed booking can keep its table ahead (manager, phones, counter):
POST /api/connect/bookings/{id}/table, reservations create/PATCH; refused with
reservation.table_kept within 2 h of another booking
- tests: test_manager_ordering.py, online bookings +2, manager orders.test.mjs,
@xenia/ordering, e2e manager.mjs (take, change, pay-first close, RESERVED)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
242 lines
14 KiB
Python
242 lines
14 KiB
Python
"""Table bookings from the online ordering app (plan phase 3S, step S.6): a booking arrives as a
|
||
reservation to confirm or decline; food ordered ahead reaches the kitchen at its fire time (the
|
||
booking time − the venue's fire minutes), not at once; the guests are seated and their food
|
||
moves to the table; every step is mirrored to the cloud."""
|
||
import json
|
||
from datetime import timedelta
|
||
|
||
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)
|
||
|
||
|
||
def book(db, menu, cloud_id=701, *, minutes=120, food=True, party=4):
|
||
"""A booking as the cloud hands it over."""
|
||
from core.timeutil import utcnow
|
||
from services.cloud_sync import store_online_orders
|
||
booked = []
|
||
lines = [{"product_id": menu["burger"].id, "name": "Burger", "quantity": 2, "unit_price": 10.0, "options": []},
|
||
{"product_id": menu["coffee"].id, "name": "Freddo", "quantity": 1, "unit_price": 3.0, "options": []}] if food else []
|
||
order = {
|
||
"id": cloud_id, "public_ref": f"ORD-{cloud_id:04d}", "order_type": "preorder" if food else "reservation",
|
||
"sales_point": "online_preorder" if food else None, "customer_name": "Eleni", "customer_phone": "6977777777",
|
||
"customer_notes": "By the window", "party_size": party, "status": "pending_acceptance",
|
||
"requested_for": (utcnow() + timedelta(minutes=minutes)).isoformat(), "created_at": utcnow().isoformat(),
|
||
"items_json": json.dumps(lines), "subtotal": 23.0 if food else 0.0, "delivery_fee": 0.0, "total": 23.0 if food else 0.0,
|
||
}
|
||
stored = store_online_orders(db, [order], booked)
|
||
return stored[cloud_id], booked
|
||
|
||
|
||
@pytest.fixture
|
||
def fire_in(db):
|
||
"""The venue's fire minutes (Online ordering → Reservations)."""
|
||
from models.settings import PosSettings
|
||
|
||
def _set(minutes):
|
||
value = json.dumps({"reservations": {"enabled": True, "with_food": True, "fire_minutes": minutes}})
|
||
row = db.get(PosSettings, "connect_orders.ordering")
|
||
if row is None:
|
||
db.add(PosSettings(key="connect_orders.ordering", value=value))
|
||
else:
|
||
row.value = value
|
||
db.commit()
|
||
return _set
|
||
|
||
|
||
def bookings(client, counter):
|
||
r = client.get("/api/connect/bookings", headers=counter["h"])
|
||
assert r.status_code == 200, r.text
|
||
return r.json()
|
||
|
||
|
||
def test_a_booking_with_food_reaches_the_kitchen_at_its_fire_time(client, db, counter, menu, owner_h, cloud, fire_in):
|
||
from core.timeutil import utcnow
|
||
from models.order import Order
|
||
from models.reservation import Reservation
|
||
from services import online_reservations as svc
|
||
fire_in(30)
|
||
rid, booked = book(db, menu)
|
||
assert booked == [rid]
|
||
r = db.get(Reservation, rid)
|
||
assert (r.status, r.source, r.party_size, r.online_ref, r.total, r.order_id) == ("requested", "online", 4, "ORD-0701", 23.0, None)
|
||
assert book(db, menu)[0] == -rid # a repeated pull stores it once
|
||
b = bookings(client, counter)
|
||
assert [x["id"] for x in b["requested"]] == [rid] and b["upcoming"] == []
|
||
x = b["requested"][0]
|
||
assert [(line["name"], line["quantity"]) for line in x["lines"]] == [("Burger", 2), ("Freddo", 1)] and x["price_diff"] is None
|
||
# confirmed two hours ahead: the kitchen doesn't get anything yet
|
||
c = client.post(f"/api/connect/bookings/{rid}/confirm", headers=counter["h"])
|
||
assert c.status_code == 200, c.text
|
||
assert c.json()["status"] == "pending" and c.json()["order_id"] is None
|
||
db.refresh(r)
|
||
assert abs((r.fire_at - (r.reserved_for - timedelta(minutes=30))).total_seconds()) < 1
|
||
assert cloud["mirror"] == [(701, "accepted", None)] and cloud["printed"] == []
|
||
assert svc.fire_due(db) == []
|
||
assert err(client.post(f"/api/connect/bookings/{rid}/confirm", headers=counter["h"])) == "reservation.status_changed"
|
||
# fire time: the order is made, printed, and on the KDS
|
||
fired = svc.fire_due(db, now=r.fire_at + timedelta(seconds=1))
|
||
db.refresh(r)
|
||
assert fired == [r.order_id]
|
||
order = db.get(Order, r.order_id)
|
||
assert (order.source, order.online_status, order.online_order_ref, order.sales_point, order.customer_count) == \
|
||
("online", None, "ORD-0701", "online_preorder", 4)
|
||
assert [(i.product_name, i.quantity, i.unit_price) for i in order.items] == [("Burger", 2, 10.0), ("Freddo", 1, 3.0)]
|
||
assert cloud["printed"] == [(order.id, sorted(i.id for i in order.items))]
|
||
assert order.id in kds_ids(client, owner_h)
|
||
assert svc.fire_due(db, now=utcnow() + timedelta(days=1)) == [] # once
|
||
# not on the counter's online board (it's a booking's food, not an order to answer)
|
||
board = client.get("/api/connect/orders/board", headers=counter["h"]).json()
|
||
assert order.id not in [o["id"] for lst in board.values() for o in lst]
|
||
|
||
|
||
def test_seated_the_food_moves_to_the_table(client, db, counter, menu, cloud, fire_in, owner_h):
|
||
from models.order import Order
|
||
from models.reservation import Reservation
|
||
from models.table import Table
|
||
fire_in(15)
|
||
t5 = Table(number=5, label="T5", is_active=True)
|
||
t6 = Table(number=6, label="T6", is_active=True)
|
||
db.add_all([t5, t6])
|
||
db.commit()
|
||
busy = Order(table_id=t6.id, opened_by=counter["user"].id, status="open", business_day_id=counter["day"].id)
|
||
db.add(busy)
|
||
db.commit()
|
||
rid, _ = book(db, menu, cloud_id=702)
|
||
h = counter["h"]
|
||
assert err(client.post(f"/api/connect/bookings/{rid}/arrive", headers=h, json={})) == "reservation.status_changed" # not confirmed
|
||
client.post(f"/api/connect/bookings/{rid}/confirm", headers=h)
|
||
assert err(client.post(f"/api/connect/bookings/{rid}/arrive", headers=h, json={"table_id": t6.id})) == "reservation.table_busy"
|
||
assert err(client.post(f"/api/connect/bookings/{rid}/arrive", headers=h, json={"table_id": 9999})) == "reservation.table_unknown"
|
||
# early: the food goes to the kitchen now, on table 5
|
||
a = client.post(f"/api/connect/bookings/{rid}/arrive", headers=h, json={"table_id": t5.id})
|
||
assert a.status_code == 200, a.text
|
||
assert a.json()["status"] == "arrived" and a.json()["table_label"] == "T5" and a.json()["order_id"]
|
||
order = db.get(Order, a.json()["order_id"])
|
||
assert order.table_id == t5.id and len(cloud["printed"]) == 1
|
||
assert [m[1] for m in cloud["mirror"]] == ["accepted", "delivered"]
|
||
assert db.get(Reservation, rid).status == "arrived"
|
||
|
||
|
||
def test_fired_before_arrival_then_seated(client, db, counter, menu, cloud, fire_in):
|
||
from models.order import Order
|
||
from models.table import Table
|
||
from services import online_reservations as svc
|
||
fire_in(60)
|
||
t7 = Table(number=7, label="T7", is_active=True)
|
||
db.add(t7)
|
||
db.commit()
|
||
rid, _ = book(db, menu, cloud_id=703, minutes=30) # due in 30 min, fire 60 before → at once
|
||
c = client.post(f"/api/connect/bookings/{rid}/confirm", headers=counter["h"]).json()
|
||
assert c["order_id"] and len(cloud["printed"]) == 1 # confirmed late: fired right away
|
||
order_id = c["order_id"]
|
||
assert db.get(Order, order_id).table_id is None
|
||
a = client.post(f"/api/connect/bookings/{rid}/arrive", headers=counter["h"], json={"table_id": t7.id}).json()
|
||
db.expire_all()
|
||
assert a["order_id"] == order_id and db.get(Order, order_id).table_id == t7.id and len(cloud["printed"]) == 1
|
||
assert svc.fire_due(db) == []
|
||
|
||
|
||
def test_no_business_day_the_food_waits(client, db, counter, menu, cloud, fire_in):
|
||
from models.business_day import BusinessDay
|
||
from models.reservation import Reservation
|
||
from services import online_reservations as svc
|
||
fire_in(0)
|
||
rid, _ = book(db, menu, cloud_id=704, minutes=1)
|
||
client.post(f"/api/connect/bookings/{rid}/confirm", headers=counter["h"])
|
||
db.expire_all()
|
||
r = db.get(Reservation, rid)
|
||
counter["day"].status = "closed"
|
||
db.commit()
|
||
later = r.fire_at + timedelta(minutes=5)
|
||
assert svc.fire_due(db, now=later) == [] and db.get(Reservation, rid).order_id is None
|
||
db.add(BusinessDay(status="open", opened_at=later, business_date=counter["day"].business_date, opened_by_id=counter["user"].id))
|
||
db.commit()
|
||
assert len(svc.fire_due(db, now=later)) == 1
|
||
|
||
|
||
def test_a_table_only_decline_and_cancel(client, db, counter, menu, cloud):
|
||
from models.reservation import Reservation
|
||
rid, _ = book(db, menu, cloud_id=705, food=False, party=2)
|
||
r = db.get(Reservation, rid)
|
||
assert r.items_json is None and r.total is None
|
||
d = client.post(f"/api/connect/bookings/{rid}/decline", headers=counter["h"], json={"reason": "Fully booked"})
|
||
assert d.json()["status"] == "declined" and d.json()["response_note"] == "Fully booked"
|
||
assert cloud["mirror"] == [(705, "rejected", "Fully booked")]
|
||
assert bookings(client, counter)["requested"] == []
|
||
rid2, _ = book(db, menu, cloud_id=706, food=False)
|
||
client.post(f"/api/connect/bookings/{rid2}/confirm", headers=counter["h"])
|
||
assert [x["id"] for x in bookings(client, counter)["upcoming"]] == [rid2]
|
||
x = client.post(f"/api/connect/bookings/{rid2}/cancel", headers=counter["h"], json={"status": "no_show"}).json()
|
||
assert x["status"] == "no_show" and cloud["mirror"][-1] == (706, "cancelled", None)
|
||
assert x["order_id"] is None
|
||
|
||
|
||
def test_the_manager_card_steps_are_mirrored(client, db, owner_h, menu, cloud, counter):
|
||
rid, _ = book(db, menu, cloud_id=707, food=False)
|
||
assert rid in [r["id"] for r in client.get("/api/reservations/?tab=upcoming", headers=owner_h).json()]
|
||
client.post(f"/api/connect/bookings/{rid}/confirm", headers=owner_h)
|
||
r = client.patch(f"/api/reservations/{rid}/status", headers=owner_h, json={"status": "arrived"})
|
||
assert r.status_code == 200, r.text
|
||
assert r.json()["status"] == "arrived" and cloud["mirror"][-1] == (707, "delivered", None)
|
||
|
||
|
||
def test_bookings_need_the_key(client, pair, make_user):
|
||
token, _ = pair("pos_counter", "Other till")
|
||
jwt = terminal_login(client, token, make_user(role="waiter"))
|
||
h = {"X-Xenia-Device": token, "Authorization": f"Bearer {jwt}"}
|
||
assert err(client.get("/api/connect/bookings", headers=h)) == "access.denied"
|
||
|
||
|
||
def test_reservation_settings(client, owner_h):
|
||
body = client.get("/api/connect/fees/ordering", headers=owner_h).json()
|
||
assert body["reservations"] == {"enabled": False, "with_food": False, "hours": {}, "slot_minutes": 30, "days_ahead": 30,
|
||
"notice_minutes": 60, "party_max": 10, "fire_minutes": 20}
|
||
body["reservations"].update(enabled=True, with_food=True, hours={"4": [["19:00", "23:30"]]}, party_max=8, fire_minutes=25)
|
||
r = client.put("/api/connect/fees/ordering", headers=owner_h, json=body)
|
||
assert r.status_code == 200, r.text
|
||
assert r.json()["reservations"]["hours"] == {"4": [["19:00", "23:30"]]} and r.json()["reservations"]["fire_minutes"] == 25
|
||
body["reservations"]["party_max"] = 0
|
||
assert client.put("/api/connect/fees/ordering", headers=owner_h, json=body).status_code == 422
|
||
|
||
|
||
def test_a_confirmed_booking_keeps_its_table_ahead(client, db, counter, menu, cloud, owner_h):
|
||
"""Plan step 6.9 (from 4.8): a confirmed booking can be given a table ahead, so the table shows
|
||
RESERVED and the pickers see it; one table isn't kept for two bookings close together."""
|
||
from models.table import Table
|
||
t5 = Table(number=5, seat_count=4)
|
||
db.add(t5)
|
||
db.commit()
|
||
rid, _ = book(db, menu, cloud_id=711, food=False, minutes=90)
|
||
assert err(client.post(f"/api/connect/bookings/{rid}/table", headers=counter["h"], json={"table_id": t5.id})) == "reservation.status_changed"
|
||
client.post(f"/api/connect/bookings/{rid}/confirm", headers=counter["h"])
|
||
kept = client.post(f"/api/connect/bookings/{rid}/table", headers=counter["h"], json={"table_id": t5.id})
|
||
assert kept.status_code == 200 and kept.json()["table_label"] == "T5"
|
||
table = next(t for t in client.get("/api/tables/", headers=owner_h).json() if t["id"] == t5.id)
|
||
assert table["upcoming_reservation"]["guest_name"] == "Eleni" # RESERVED
|
||
live = next(t for t in client.get("/api/floor-plans/live", headers=owner_h).json()["tables"] if t["id"] == t5.id)
|
||
assert live["state"] == "reserved"
|
||
rid2, _ = book(db, menu, cloud_id=712, food=False, minutes=150)
|
||
client.post(f"/api/connect/bookings/{rid2}/confirm", headers=counter["h"])
|
||
assert err(client.post(f"/api/connect/bookings/{rid2}/table", headers=counter["h"], json={"table_id": t5.id})) == "reservation.table_kept"
|
||
assert client.post(f"/api/connect/bookings/{rid}/table", headers=counter["h"], json={"table_id": None}).json()["table_id"] is None
|
||
assert client.post(f"/api/connect/bookings/{rid2}/table", headers=counter["h"], json={"table_id": t5.id}).status_code == 200
|
||
|
||
|
||
def test_the_managers_booking_dialog_keeps_and_frees_a_table(client, db, owner_h):
|
||
from core.timeutil import utcnow
|
||
from datetime import timedelta
|
||
from models.table import Table
|
||
t7 = Table(number=7)
|
||
db.add(t7)
|
||
db.commit()
|
||
when = (utcnow() + timedelta(hours=1)).isoformat()
|
||
a = client.post("/api/reservations/", headers=owner_h, json={"guest_name": "Nikos", "party_size": 2, "reserved_for": when, "table_id": t7.id})
|
||
assert a.status_code == 201, a.text
|
||
b = client.post("/api/reservations/", headers=owner_h, json={"guest_name": "Maria", "party_size": 2, "reserved_for": when, "table_id": t7.id})
|
||
assert err(b) == "reservation.table_kept"
|
||
freed = client.patch(f"/api/reservations/{a.json()['id']}", headers=owner_h, json={"table_id": None})
|
||
assert freed.status_code == 200 and freed.json()["table_id"] is None
|