feat(waiter): a table's earlier orders (platform-v2 step 4.7)

- GET /api/tables/{id}/history: the table's closed / cancelled orders, newest first,
  for the waiters of its zone (ordering.orders.open), never older than the history
  module's history_days (free tier 31, D-6), module.disabled when history is off;
  each order with times, total (cancelled items left out), people and items.
- Waiter: a History tab on the table (and "This table's earlier orders" on a free
  table); orders expand to their items; the last answer is kept on the phone and
  shown offline, marked as saved; the plan's day limit is shown.
- Tests: test_table_history.py (4), waiter e2e +2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 04:45:31 +02:00
co-authored by Claude Opus 5.5
parent f8d91e7fae
commit 9e1912bce0
7 changed files with 306 additions and 2 deletions
+89
View File
@@ -0,0 +1,89 @@
"""A table's earlier orders on the waiter's phone (plan step 4.7).
GET /api/tables/{id}/history: the table's finished orders (closed / cancelled), newest
first, for the waiters of its zone, never older than the `history` module allows (free
tier 31 days, D-6), and refused when the module is off.
"""
from datetime import timedelta
import pytest
from core.timeutil import utcnow
from tests.test_entitlements import install
@pytest.fixture
def floor(db, make_user, login):
from models.product import Category, Product
from models.table import Table, TableGroup
from models.user import WaiterZone
terrace, inside = TableGroup(name="Terrace"), TableGroup(name="Inside")
db.add_all([terrace, inside])
db.flush()
t1, t3 = Table(number=1, group_id=terrace.id), Table(number=3, group_id=inside.id)
cat = Category(name="Drinks")
db.add_all([t1, t3, cat])
db.flush()
coffee = Product(name="Freddo", base_price=3.0, primary_category_id=cat.id)
db.add(coffee)
maria = make_user(role="waiter", nickname="Maria")
db.add(WaiterZone(waiter_id=maria.id, group_id=terrace.id))
db.commit()
return {"t1": t1, "t3": t3, "coffee": coffee, "maria": maria, "h": login(maria, "waiter")}
def order(db, f, table, status, opened_ago, items=((2, "paid"),), closed=True):
from models.order import Order, OrderItem, OrderWaiter
o = Order(table_id=table.id, opened_by=f["maria"].id, status=status, order_type="here",
opened_at=utcnow() - opened_ago, closed_at=(utcnow() - opened_ago + timedelta(minutes=40)) if closed else None)
db.add(o)
db.flush()
db.add(OrderWaiter(order_id=o.id, waiter_id=f["maria"].id))
for qty, st in items:
db.add(OrderItem(order_id=o.id, product_id=f["coffee"].id, product_name="Freddo", added_by=f["maria"].id,
quantity=qty, unit_price=3.0, status=st))
db.commit()
return o
def test_the_tables_earlier_orders_newest_first(client, db, floor):
f = floor
lunch = order(db, f, f["t1"], "closed", timedelta(hours=5), items=((2, "paid"), (1, "cancelled")))
coffee = order(db, f, f["t1"], "closed", timedelta(hours=2))
voided = order(db, f, f["t1"], "cancelled", timedelta(hours=1), items=((1, "cancelled"),))
order(db, f, f["t1"], "open", timedelta(minutes=10), closed=False) # the open one isn't history
order(db, f, f["t3"], "closed", timedelta(hours=1)) # another table
r = client.get(f"/api/tables/{f['t1'].id}/history", headers=f["h"])
assert r.status_code == 200, r.text
data = r.json()
assert [o["id"] for o in data["orders"]] == [voided.id, coffee.id, lunch.id]
first = data["orders"][2]
assert first["total"] == 6.0 and first["people"] == ["Maria"] and first["status"] == "closed"
assert [(i["name"], i["quantity"], i["status"]) for i in first["items"]] == [("Freddo", 2, "paid"), ("Freddo", 1, "cancelled")]
assert data["orders"][0]["total"] == 0.0 and data["orders"][0]["status"] == "cancelled"
def test_only_the_waiters_of_the_tables_zone(client, db, floor):
r = client.get(f"/api/tables/{floor['t3'].id}/history", headers=floor["h"])
assert r.status_code == 403 and r.json()["error"]["code"] == "pos.table_not_yours"
def test_free_tier_shows_only_its_days(client, db, floor):
f = floor
old = order(db, f, f["t1"], "closed", timedelta(days=40))
recent = order(db, f, f["t1"], "closed", timedelta(days=3))
assert [o["id"] for o in client.get(f"/api/tables/{f['t1'].id}/history", headers=f["h"]).json()["orders"]] == [recent.id, old.id]
install({"ordering": {}, "history": {"limits": {"history_days": 31}}})
data = client.get(f"/api/tables/{f['t1'].id}/history", headers=f["h"]).json()
assert [o["id"] for o in data["orders"]] == [recent.id] and data["history_days"] == 31
def test_history_off_means_no_history(client, db, floor, owner_h):
assert client.put("/api/platform/modules/history", headers=owner_h, json={"enabled": False}).status_code == 200
r = client.get(f"/api/tables/{floor['t1'].id}/history", headers=floor["h"])
assert r.status_code == 403 and r.json()["error"]["code"] == "module.disabled"
@pytest.fixture
def owner_h(make_user, login):
return login(make_user(role="owner"))