feat(backend): KDS item claims across screens; zone screens only touch their own items (platform-v2 3.2c)
- order_items.claimed_by / claimed_at (Alembic 0015). POST/DELETE
/api/kds/orders/{id}/items/{item}/claim (kds.bump): someone else's claim is refused
(kds.item_claimed), letting go works for anyone with kds.bump; live kds_item_claimed.
In progress claims for whoever did it; done/served/pending/declined clear the claim.
- Fix: order-level status and decline from a zone screen changed every zone's items;
now only the screen's zone, and a zone screen sees the order status of its own items.
- Terminal: "I'm on it" / "Let go" on the selected card, the cook's name on claimed items.
- Tests: backend +6, terminal unit +1, terminal.mjs +6 with a second screen and cook.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+132
-21
@@ -23,6 +23,13 @@ GET /api/kds/context
|
||||
POST /api/kds/orders/{order_id}/retry-print
|
||||
Print the order's unprinted items again (the KDS can't reach /api/orders, 3.2a).
|
||||
|
||||
POST / DELETE /api/kds/orders/{order_id}/items/{item_id}/claim (kds.bump, plan step 3.2c)
|
||||
A cook claims an item ("I'm on it") or lets it go. An item claimed by someone else is
|
||||
refused (kds.item_claimed); letting go works for anyone holding kds.bump (a cook who left).
|
||||
Moving an item to "preparing" claims it for whoever did it when nobody has; any other
|
||||
status (done, served, back to pending, declined) clears the claim. Live: kds_item_claimed.
|
||||
Order-level status changes from a kitchen screen with a zone only touch that zone's items.
|
||||
|
||||
Kitchen screens (plan step 3.2a): a call from a paired device running the KDS uses that
|
||||
device's settings. GET /orders returns the screen's prep zone (every zone when it has none;
|
||||
a zone_id sent by the screen is ignored), /print defaults to it, and messages to waiters are signed
|
||||
@@ -49,6 +56,7 @@ from models.message import StaffMessage, StaffMessageAck
|
||||
from models.shift import WaiterShift
|
||||
from core.access.context import current_device
|
||||
from core.modules import device_config
|
||||
from core.access.deps import require
|
||||
from routers.deps import get_current_user, require_kds
|
||||
from services.sse_bus import broadcast_sync
|
||||
|
||||
@@ -76,6 +84,38 @@ def _device_zone_id(request: Request, db: Session) -> Optional[int]:
|
||||
return _screen_settings(dev, db)["prep_zone_id"] if dev else None
|
||||
|
||||
|
||||
def _real_user_id(user) -> Optional[int]:
|
||||
"""A claim belongs to a staff member; the support account (no DB row) can't hold one."""
|
||||
return user.id if isinstance(user, User) else None
|
||||
|
||||
|
||||
def _claim_follows_status(item: OrderItem, user) -> None:
|
||||
"""In progress = someone is on it: claim for `user` if nobody has; anything else ends it."""
|
||||
if item.kds_status == "preparing":
|
||||
if item.claimed_by is None and _real_user_id(user) is not None:
|
||||
item.claimed_by = user.id
|
||||
item.claimed_at = utcnow()
|
||||
elif item.claimed_by is not None:
|
||||
item.claimed_by = None
|
||||
item.claimed_at = None
|
||||
|
||||
|
||||
def _claimer_name(item: OrderItem) -> Optional[str]:
|
||||
u = item.claimed_by_user
|
||||
return (u.nickname or u.full_name or u.username) if u is not None else None
|
||||
|
||||
|
||||
def _iso(dt) -> Optional[str]:
|
||||
return dt.isoformat() + "Z" if dt is not None and dt.tzinfo is None else (dt.isoformat() if dt else None)
|
||||
|
||||
|
||||
def _in_screen_zone(item: OrderItem, zone_id: Optional[int]) -> bool:
|
||||
"""A kitchen screen with a zone only changes its own zone's items (order-level actions)."""
|
||||
if zone_id is None:
|
||||
return True
|
||||
return bool(item.product) and any(z.id == zone_id for z in item.product.prep_zones)
|
||||
|
||||
|
||||
def _sender(request: Request, db: Session, sent: Optional[str]) -> str:
|
||||
"""How waiters see who sent a KDS message. A kitchen screen signs with its zone's
|
||||
notification name (else the zone's name, else the screen's name); the manager's page
|
||||
@@ -179,6 +219,9 @@ def _build_order_payload(order: Order, tables_map: dict, db: Session) -> dict:
|
||||
"unit_type": unit_type or "piece",
|
||||
"kds_status": item.kds_status,
|
||||
"decline_note": item.decline_note,
|
||||
"claimed_by": item.claimed_by,
|
||||
"claimed_by_name": _claimer_name(item),
|
||||
"claimed_at": _iso(item.claimed_at),
|
||||
"added_at": item.added_at.isoformat() if item.added_at else None,
|
||||
"_added_at_raw": item.added_at, # stripped before response
|
||||
"prep_zone_ids": prep_zone_ids,
|
||||
@@ -223,22 +266,26 @@ def _build_order_payload(order: Order, tables_map: dict, db: Session) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _status_of(statuses: set) -> Optional[str]:
|
||||
"""An order's KDS status from its items' statuses (None when there are none)."""
|
||||
if not statuses:
|
||||
return None
|
||||
if statuses <= {"served"}:
|
||||
return "served"
|
||||
if statuses <= {"done", "served"}:
|
||||
return "done"
|
||||
if statuses <= {"declined"}:
|
||||
return "declined"
|
||||
if "preparing" in statuses or "done" in statuses:
|
||||
return "preparing"
|
||||
return "pending"
|
||||
|
||||
|
||||
def _sync_order_kds_status(order: Order):
|
||||
"""Derive and set order.kds_status from all non-cancelled items. Call before db.commit()."""
|
||||
relevant = [i for i in order.items if i.status != "cancelled"]
|
||||
if not relevant:
|
||||
new_status = _status_of({i.kds_status for i in order.items if i.status != "cancelled"})
|
||||
if new_status is None:
|
||||
return
|
||||
statuses = {i.kds_status for i in relevant}
|
||||
if statuses <= {"served"}:
|
||||
new_status = "served"
|
||||
elif statuses <= {"done", "served"}:
|
||||
new_status = "done"
|
||||
elif statuses <= {"declined"}:
|
||||
new_status = "declined"
|
||||
elif "preparing" in statuses or "done" in statuses:
|
||||
new_status = "preparing"
|
||||
else:
|
||||
new_status = "pending"
|
||||
if new_status != order.kds_status:
|
||||
order.kds_status = new_status
|
||||
order.kds_status_changed_at = utcnow()
|
||||
@@ -287,6 +334,9 @@ def kds_orders(
|
||||
p["items"] = [it for it in p["items"] if zone_id in it.get("prep_zone_ids", [])]
|
||||
if not p["items"]:
|
||||
continue
|
||||
# for this zone the order is as far as ITS items are (3.2c): the Grill is done
|
||||
# with an order whose drinks are still to do at the bar
|
||||
p["kds_status"] = _status_of({it["kds_status"] for it in p["items"]}) or p["kds_status"]
|
||||
payloads.append(p)
|
||||
return {"orders": payloads}
|
||||
|
||||
@@ -303,9 +353,13 @@ class OrderTypeBody(BaseModel):
|
||||
def update_order_kds_status(
|
||||
order_id: int,
|
||||
body: KdsStatusBody,
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Set the order's KDS status; pending/preparing/done also set its open items. On a
|
||||
kitchen screen with a prep zone only that zone's items change (3.2c), and the order's
|
||||
status then follows all its items."""
|
||||
if body.status not in VALID_KDS_STATUSES:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid status '{body.status}'")
|
||||
|
||||
@@ -313,28 +367,32 @@ def update_order_kds_status(
|
||||
if not order:
|
||||
raise HTTPException(status_code=404, detail="Order not found")
|
||||
|
||||
if body.status != order.kds_status:
|
||||
zone_id = _device_zone_id(request, db)
|
||||
if body.status != order.kds_status and zone_id is None:
|
||||
order.kds_status = body.status
|
||||
order.kds_status_changed_at = utcnow()
|
||||
|
||||
# Cascade order-level status to non-cancelled, non-served items only
|
||||
if body.status in ("pending", "preparing", "done"):
|
||||
for item in order.items:
|
||||
if item.status != "cancelled" and item.kds_status != "served":
|
||||
if item.status != "cancelled" and item.kds_status != "served" and _in_screen_zone(item, zone_id):
|
||||
item.kds_status = body.status
|
||||
# auto_ready_to_served: if cascading to 'done', immediately upgrade qualifying items
|
||||
if body.status == "done" and item.product:
|
||||
if any(bool(z.auto_ready_to_served) for z in item.product.prep_zones):
|
||||
item.kds_status = "served"
|
||||
_claim_follows_status(item, user)
|
||||
if zone_id is not None:
|
||||
_sync_order_kds_status(order)
|
||||
|
||||
db.commit()
|
||||
|
||||
broadcast_sync("kds_order_updated", {
|
||||
"order_id": order_id,
|
||||
"kds_status": body.status,
|
||||
"kds_status": order.kds_status,
|
||||
})
|
||||
|
||||
return {"order_id": order_id, "kds_status": body.status}
|
||||
return {"order_id": order_id, "kds_status": order.kds_status}
|
||||
|
||||
|
||||
@router.put("/orders/{order_id}/items/{item_id}/kds_status")
|
||||
@@ -363,6 +421,7 @@ def update_item_kds_status(
|
||||
auto_serve = any(bool(z.auto_ready_to_served) for z in item.product.prep_zones)
|
||||
if auto_serve:
|
||||
item.kds_status = "served"
|
||||
_claim_follows_status(item, user)
|
||||
|
||||
if item.order:
|
||||
_sync_order_kds_status(item.order)
|
||||
@@ -519,6 +578,7 @@ class DeclineBody(BaseModel):
|
||||
def decline_order(
|
||||
order_id: int,
|
||||
body: DeclineBody,
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(get_current_user),
|
||||
):
|
||||
@@ -527,20 +587,25 @@ def decline_order(
|
||||
if not order:
|
||||
raise HTTPException(status_code=404, detail="Order not found")
|
||||
|
||||
zone_id = _device_zone_id(request, db) # a zone screen declines only its own items (3.2c)
|
||||
for item in order.items:
|
||||
if item.status != "cancelled":
|
||||
if item.status != "cancelled" and _in_screen_zone(item, zone_id):
|
||||
item.kds_status = "declined"
|
||||
item.decline_note = body.decline_note
|
||||
_claim_follows_status(item, user)
|
||||
|
||||
order.kds_status = "declined"
|
||||
if zone_id is None:
|
||||
order.kds_status = "declined"
|
||||
else:
|
||||
_sync_order_kds_status(order)
|
||||
db.commit()
|
||||
|
||||
broadcast_sync("kds_order_updated", {
|
||||
"order_id": order_id,
|
||||
"kds_status": "declined",
|
||||
"kds_status": order.kds_status,
|
||||
"decline_note": body.decline_note,
|
||||
})
|
||||
return {"order_id": order_id, "kds_status": "declined"}
|
||||
return {"order_id": order_id, "kds_status": order.kds_status}
|
||||
|
||||
|
||||
@router.put("/orders/{order_id}/items/{item_id}/decline")
|
||||
@@ -561,6 +626,7 @@ def decline_item(
|
||||
|
||||
item.kds_status = "declined"
|
||||
item.decline_note = body.decline_note
|
||||
_claim_follows_status(item, user)
|
||||
|
||||
if item.order:
|
||||
_sync_order_kds_status(item.order)
|
||||
@@ -841,3 +907,48 @@ def kds_retry_print(
|
||||
if not unprinted_ids:
|
||||
return {"print_results": []}
|
||||
return {"print_results": route_and_print_sync(order_id, unprinted_ids, db)}
|
||||
|
||||
|
||||
|
||||
# ─────────────────────────── claims (3.2c) ─────────────────────────────────
|
||||
|
||||
def _claim_item(db: Session, order_id: int, item_id: int) -> OrderItem:
|
||||
item = db.query(OrderItem).filter(OrderItem.id == item_id, OrderItem.order_id == order_id).first()
|
||||
if not item:
|
||||
raise XeniaError("kds.item_not_found", item_id=item_id)
|
||||
return item
|
||||
|
||||
|
||||
def _claim_out(item: OrderItem) -> dict:
|
||||
return {"order_id": item.order_id, "item_id": item.id, "claimed_by": item.claimed_by,
|
||||
"claimed_by_name": _claimer_name(item), "claimed_at": _iso(item.claimed_at)}
|
||||
|
||||
|
||||
@router.post("/orders/{order_id}/items/{item_id}/claim")
|
||||
def claim_item(order_id: int, item_id: int, db: Session = Depends(get_db), user=Depends(require("kds.bump"))):
|
||||
""""I'm on it": the item is mine. Someone else's claim is refused (kds.item_claimed)."""
|
||||
item = _claim_item(db, order_id, item_id)
|
||||
uid = _real_user_id(user)
|
||||
if uid is None:
|
||||
raise XeniaError("kds.claim_needs_staff")
|
||||
if item.claimed_by is not None and item.claimed_by != uid:
|
||||
raise XeniaError("kds.item_claimed", name=_claimer_name(item) or "")
|
||||
if item.claimed_by != uid:
|
||||
item.claimed_by = uid
|
||||
item.claimed_at = utcnow()
|
||||
db.commit()
|
||||
db.refresh(item)
|
||||
broadcast_sync("kds_item_claimed", _claim_out(item))
|
||||
return _claim_out(item)
|
||||
|
||||
|
||||
@router.delete("/orders/{order_id}/items/{item_id}/claim")
|
||||
def release_item(order_id: int, item_id: int, db: Session = Depends(get_db), user=Depends(require("kds.bump"))):
|
||||
"""Let the item go (anyone holding kds.bump: also for a cook who left)."""
|
||||
item = _claim_item(db, order_id, item_id)
|
||||
if item.claimed_by is not None:
|
||||
item.claimed_by = None
|
||||
item.claimed_at = None
|
||||
db.commit()
|
||||
broadcast_sync("kds_item_claimed", _claim_out(item))
|
||||
return _claim_out(item)
|
||||
|
||||
Reference in New Issue
Block a user