feat: foundations for the manager conversion (platform-v2 step 6.1)
- Backend: every HTTPException in routers/ is an error code now (260 raises, ~105 new codes, texts in en + el); XeniaError(detail=...) keeps the three v1-shaped answers old pages read; bad / unknown tokens answer auth.session_ended; test_errors refuses new HTTPExceptions. - Manager: v2.json is the list of pages on the v2 rules: scripts/lint.mjs lints them, tests/v2-guards.test.mjs refuses Greek and colour literals, `strict` paths make set-state-in-effect an error. - src/live/useLiveQuery.js (react-query + live events + refresh on reconnect); Signed in now uses it. - Dead manager files removed (3.1k lines unreachable from main.jsx). - Tests: backend 544 passed, manager unit 33, run-all (no Android) passed (manager's live bell check passed on a rerun). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from core.timeutil import utcnow
|
||||
from datetime import datetime, timezone
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Optional
|
||||
|
||||
@@ -11,6 +11,7 @@ from models.order import Order, OrderItem
|
||||
from models.user import User
|
||||
from schemas.tabs import TabOut, TabEntryOut, TabPaymentOut, TabPayRequest, TabForgiveRequest, TabCloseRequest
|
||||
from routers.deps import require_manager
|
||||
from core.errors import XeniaError
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -94,7 +95,7 @@ def list_tabs(
|
||||
def get_tab(tab_id: int, db: Session = Depends(get_db), user: User = Depends(require_manager)):
|
||||
tab = db.query(Tab).filter(Tab.id == tab_id).first()
|
||||
if not tab:
|
||||
raise HTTPException(status_code=404, detail="Tab not found")
|
||||
raise XeniaError("tab.not_found")
|
||||
return _tab_out(tab)
|
||||
|
||||
|
||||
@@ -102,10 +103,10 @@ def get_tab(tab_id: int, db: Session = Depends(get_db), user: User = Depends(req
|
||||
def open_tab(customer_id: int, db: Session = Depends(get_db), user: User = Depends(require_manager)):
|
||||
customer = db.query(Customer).filter(Customer.id == customer_id, Customer.is_active == True).first()
|
||||
if not customer:
|
||||
raise HTTPException(status_code=404, detail="Customer not found")
|
||||
raise XeniaError("customer.not_found")
|
||||
existing = db.query(Tab).filter(Tab.customer_id == customer_id, Tab.status == "open").first()
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail="Customer already has an open tab")
|
||||
raise XeniaError("tab.already_open")
|
||||
tab = Tab(customer_id=customer_id)
|
||||
db.add(tab)
|
||||
db.commit()
|
||||
@@ -125,9 +126,9 @@ def add_tab_entry(
|
||||
):
|
||||
tab = db.query(Tab).filter(Tab.id == tab_id, Tab.status == "open").first()
|
||||
if not tab:
|
||||
raise HTTPException(status_code=404, detail="Open tab not found")
|
||||
raise XeniaError("tab.not_found")
|
||||
if amount <= 0:
|
||||
raise HTTPException(status_code=400, detail="Amount must be positive")
|
||||
raise XeniaError("validation.positive", field='amount')
|
||||
entry = TabEntry(
|
||||
tab_id=tab_id, order_id=order_id, order_item_id=order_item_id,
|
||||
amount=amount, description=description, created_by_id=user.id,
|
||||
@@ -147,14 +148,14 @@ def pay_tab(
|
||||
):
|
||||
tab = db.query(Tab).filter(Tab.id == tab_id, Tab.status == "open").first()
|
||||
if not tab:
|
||||
raise HTTPException(status_code=404, detail="Open tab not found")
|
||||
raise XeniaError("tab.not_found")
|
||||
if body.amount <= 0:
|
||||
raise HTTPException(status_code=400, detail="Amount must be positive")
|
||||
raise XeniaError("validation.positive", field='amount')
|
||||
total_charged = sum(e.amount for e in tab.entries)
|
||||
total_paid = sum(p.amount for p in tab.payments)
|
||||
balance = total_charged - total_paid
|
||||
if body.amount > balance + 0.005:
|
||||
raise HTTPException(status_code=400, detail=f"Payment exceeds balance ({balance:.2f})")
|
||||
raise XeniaError("tab.over_balance", balance=f'{balance:.2f}')
|
||||
payment = TabPayment(
|
||||
tab_id=tab_id, amount=body.amount,
|
||||
payment_method=body.payment_method, notes=body.notes,
|
||||
@@ -175,12 +176,12 @@ def close_tab(
|
||||
):
|
||||
tab = db.query(Tab).filter(Tab.id == tab_id, Tab.status == "open").first()
|
||||
if not tab:
|
||||
raise HTTPException(status_code=404, detail="Open tab not found")
|
||||
raise XeniaError("tab.not_found")
|
||||
total_charged = sum(e.amount for e in tab.entries)
|
||||
total_paid = sum(p.amount for p in tab.payments)
|
||||
balance = round(total_charged - total_paid, 2)
|
||||
if balance > 0.005:
|
||||
raise HTTPException(status_code=400, detail=f"Tab still has a balance of €{balance:.2f} — pay it off first or use /forgive")
|
||||
raise XeniaError("tab.has_balance", balance=f'{balance:.2f}')
|
||||
tab.status = "closed"
|
||||
tab.closed_at = utcnow()
|
||||
tab.closed_by_id = user.id
|
||||
@@ -200,7 +201,7 @@ def forgive_tab(
|
||||
):
|
||||
tab = db.query(Tab).filter(Tab.id == tab_id, Tab.status == "open").first()
|
||||
if not tab:
|
||||
raise HTTPException(status_code=404, detail="Open tab not found")
|
||||
raise XeniaError("tab.not_found")
|
||||
tab.status = "forgiven"
|
||||
tab.closed_at = utcnow()
|
||||
tab.closed_by_id = user.id
|
||||
|
||||
Reference in New Issue
Block a user