- 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>
202 lines
7.1 KiB
Python
202 lines
7.1 KiB
Python
from core.timeutil import utcnow, venue_day_window
|
|
from fastapi import APIRouter, BackgroundTasks, Depends, status, Query
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
from datetime import datetime, timezone
|
|
|
|
from database import get_db
|
|
from models.reservation import Reservation
|
|
from models.table import Table
|
|
from schemas.reservation import (
|
|
ReservationCreate, ReservationUpdate, ReservationStatusUpdate, ReservationOut,
|
|
)
|
|
from routers.deps import get_current_user, require_manager
|
|
from models.user import User
|
|
from core.errors import XeniaError
|
|
|
|
router = APIRouter()
|
|
|
|
VALID_STATUSES = {"pending", "arrived", "cancelled", "no_show"}
|
|
|
|
|
|
def _enrich(r: Reservation) -> ReservationOut:
|
|
out = ReservationOut.model_validate(r)
|
|
if r.table:
|
|
out.table_label = r.table.label or f"T{r.table.number}"
|
|
if r.created_by:
|
|
out.created_by_name = (
|
|
r.created_by.full_name or r.created_by.nickname or r.created_by.username
|
|
)
|
|
return out
|
|
|
|
|
|
@router.post("/", response_model=ReservationOut, status_code=status.HTTP_201_CREATED)
|
|
def create_reservation(
|
|
body: ReservationCreate,
|
|
db: Session = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
if body.table_id:
|
|
if not db.query(Table).filter(Table.id == body.table_id, Table.is_active == True).first():
|
|
raise XeniaError("table.not_found", status=400)
|
|
|
|
r = Reservation(
|
|
guest_name=body.guest_name,
|
|
party_size=body.party_size,
|
|
phone=body.phone,
|
|
email=body.email,
|
|
note=body.note,
|
|
reserved_for=body.reserved_for,
|
|
source=body.source,
|
|
online_customer_id=body.online_customer_id,
|
|
created_by_user_id=user.id,
|
|
status="pending",
|
|
)
|
|
if body.table_id:
|
|
from services.online_reservations import keep_table
|
|
keep_table(db, r, body.table_id) # not kept for another booking close by (6.9)
|
|
db.add(r)
|
|
db.commit()
|
|
db.refresh(r)
|
|
from services.ws_bus import broadcast_sync
|
|
broadcast_sync("reservation_updated", {"reservation_id": r.id, "status": r.status, "table_id": r.table_id})
|
|
if r.table_id:
|
|
broadcast_sync("table_list_changed", {})
|
|
return _enrich(r)
|
|
|
|
|
|
@router.get("/", response_model=List[ReservationOut])
|
|
def list_reservations(
|
|
tab: Optional[str] = Query(None), # "upcoming" | "history" — if omitted returns all
|
|
date: Optional[str] = Query(None), # ISO date string YYYY-MM-DD to filter by day
|
|
db: Session = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
q = db.query(Reservation)
|
|
|
|
if date:
|
|
try:
|
|
day = datetime.fromisoformat(date).date()
|
|
except ValueError:
|
|
raise XeniaError("validation.datetime", status=400)
|
|
# The venue's calendar day, as a UTC window (D-11; 23 h / 25 h on DST days)
|
|
start, end = venue_day_window(day)
|
|
q = q.filter(Reservation.reserved_for >= start, Reservation.reserved_for < end)
|
|
|
|
if tab == "upcoming":
|
|
# requested (online, to confirm, S.6) + pending + no_show (actionable), soonest first
|
|
q = q.filter(Reservation.status.in_(["requested", "pending", "no_show"]))
|
|
q = q.order_by(Reservation.reserved_for.asc())
|
|
elif tab == "history":
|
|
# arrived, cancelled, plus no_show/pending that are clearly in the past
|
|
now = utcnow()
|
|
q = q.filter(
|
|
(Reservation.status.in_(["arrived", "cancelled", "declined"])) |
|
|
(
|
|
Reservation.status.in_(["no_show", "pending"]) &
|
|
(Reservation.reserved_for < now)
|
|
)
|
|
)
|
|
q = q.order_by(Reservation.reserved_for.desc())
|
|
else:
|
|
q = q.order_by(Reservation.reserved_for.asc())
|
|
|
|
reservations = q.all()
|
|
return [_enrich(r) for r in reservations]
|
|
|
|
|
|
@router.get("/{reservation_id}", response_model=ReservationOut)
|
|
def get_reservation(
|
|
reservation_id: int,
|
|
db: Session = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
r = db.query(Reservation).filter(Reservation.id == reservation_id).first()
|
|
if not r:
|
|
raise XeniaError("reservation.not_found", reservation_id=reservation_id)
|
|
return _enrich(r)
|
|
|
|
|
|
@router.patch("/{reservation_id}", response_model=ReservationOut)
|
|
def update_reservation(
|
|
reservation_id: int,
|
|
body: ReservationUpdate,
|
|
db: Session = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
r = db.query(Reservation).filter(Reservation.id == reservation_id).first()
|
|
if not r:
|
|
raise XeniaError("reservation.not_found", reservation_id=reservation_id)
|
|
|
|
updates = body.model_dump(exclude_none=True)
|
|
# the table: set, changed or taken back (null) — kept for one booking at a time (6.9)
|
|
if "table_id" in body.model_fields_set:
|
|
from services.online_reservations import keep_table
|
|
keep_table(db, r, body.table_id)
|
|
updates.pop("table_id", None)
|
|
|
|
if "status" in updates and updates["status"] not in VALID_STATUSES:
|
|
raise XeniaError("validation.choice", field='status', value=updates['status'])
|
|
|
|
for field, value in updates.items():
|
|
setattr(r, field, value)
|
|
|
|
r.updated_at = utcnow()
|
|
db.commit()
|
|
db.refresh(r)
|
|
from services.ws_bus import broadcast_sync
|
|
broadcast_sync("reservation_updated", {"reservation_id": r.id, "status": r.status, "table_id": r.table_id})
|
|
broadcast_sync("table_list_changed", {})
|
|
return _enrich(r)
|
|
|
|
|
|
@router.patch("/{reservation_id}/status", response_model=ReservationOut)
|
|
def update_reservation_status(
|
|
reservation_id: int,
|
|
body: ReservationStatusUpdate,
|
|
background: BackgroundTasks,
|
|
db: Session = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
if body.status not in VALID_STATUSES:
|
|
raise XeniaError("validation.choice", field='status', value=body.status)
|
|
|
|
r = db.query(Reservation).filter(Reservation.id == reservation_id).first()
|
|
if not r:
|
|
raise XeniaError("reservation.not_found", reservation_id=reservation_id)
|
|
|
|
if r.online_order_cloud_id and r.status in ("requested", "pending") and body.status in ("arrived", "cancelled", "no_show"):
|
|
# an online booking (phase 3S, S.6): the same steps as the counter's, and the customer's page follows
|
|
from routers.connect_orders import _booking_changed
|
|
from services import online_reservations as svc
|
|
if body.status == "arrived":
|
|
fired = svc.arrive(db, r, r.table_id)
|
|
db.commit()
|
|
if fired:
|
|
svc.send_to_kitchen(db, r.order_id, fired)
|
|
else:
|
|
svc.cancel(db, r, body.status, None)
|
|
db.commit()
|
|
db.refresh(r)
|
|
_booking_changed(background, r)
|
|
return _enrich(r)
|
|
|
|
r.status = body.status
|
|
r.updated_at = utcnow()
|
|
db.commit()
|
|
db.refresh(r)
|
|
return _enrich(r)
|
|
|
|
|
|
@router.delete("/{reservation_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_reservation(
|
|
reservation_id: int,
|
|
db: Session = Depends(get_db),
|
|
user: User = Depends(require_manager),
|
|
):
|
|
r = db.query(Reservation).filter(Reservation.id == reservation_id).first()
|
|
if not r:
|
|
raise XeniaError("reservation.not_found", reservation_id=reservation_id)
|
|
db.delete(r)
|
|
db.commit()
|