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,5 +1,5 @@
|
||||
from core.timeutil import business_date_of, parse_api_datetime, utcnow
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Optional
|
||||
@@ -18,6 +18,7 @@ from schemas.business_day import BusinessDayOut, OpenBusinessDayRequest, CloseBu
|
||||
from routers.deps import get_current_user, require_manager
|
||||
from models.user import User
|
||||
from services.license_state import license_state
|
||||
from core.errors import XeniaError
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -44,29 +45,23 @@ def open_business_day(
|
||||
):
|
||||
existing = db.query(BusinessDay).filter(BusinessDay.status == "open").first()
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail="A business day is already open")
|
||||
raise XeniaError("business_day.already_open")
|
||||
|
||||
# Gate: admin lock (already enforced or pending)
|
||||
if license_state.get("locked") or license_state.get("lock_pending"):
|
||||
raise HTTPException(
|
||||
status_code=423,
|
||||
detail={
|
||||
raise XeniaError("license.locked", detail={
|
||||
"code": "SYSTEM_LOCKED",
|
||||
"message": "Το σύστημα έχει κλειδωθεί από διαχειριστή. Επικοινωνήστε με την υποστήριξη.",
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
# Gate: license expired and expiry grace period also over
|
||||
if not license_state.get("licensed", True):
|
||||
expires_at = license_state.get("expires_at", "")
|
||||
raise HTTPException(
|
||||
status_code=402,
|
||||
detail={
|
||||
raise XeniaError("license.expired", detail={
|
||||
"code": "LICENSE_EXPIRED",
|
||||
"message": "Η άδεια χρήσης έχει λήξει. Ανανεώστε την άδεια ή επικοινωνήστε με την υποστήριξη.",
|
||||
"expires_at": expires_at,
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
opened_at = utcnow()
|
||||
day = BusinessDay(opened_by_id=user.id, notes=body.notes,
|
||||
@@ -85,7 +80,7 @@ def close_business_day(
|
||||
):
|
||||
day = db.query(BusinessDay).filter(BusinessDay.status == "open").first()
|
||||
if not day:
|
||||
raise HTTPException(status_code=404, detail="No open business day")
|
||||
raise XeniaError("business_day.none_open")
|
||||
|
||||
open_orders = db.query(Order).filter(
|
||||
Order.business_day_id == day.id,
|
||||
@@ -102,14 +97,11 @@ def close_business_day(
|
||||
OrderItem.status == "active",
|
||||
).first() is not None
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
raise XeniaError("business_day.orders_open", detail={
|
||||
"message": f"{len(open_orders)} table(s) still open, {with_pending} with unpaid items.",
|
||||
"open_orders": len(open_orders),
|
||||
"partially_paid": with_pending,
|
||||
},
|
||||
)
|
||||
}, open_orders=len(open_orders), partially_paid=with_pending)
|
||||
|
||||
now = utcnow()
|
||||
|
||||
@@ -195,18 +187,18 @@ def patch_business_day(
|
||||
"""
|
||||
day = db.query(BusinessDay).filter(BusinessDay.id == day_id).first()
|
||||
if not day:
|
||||
raise HTTPException(status_code=404, detail="Business day not found")
|
||||
raise XeniaError("business_day.not_found")
|
||||
if day.status != "closed":
|
||||
raise HTTPException(status_code=400, detail="Can only edit a closed business day")
|
||||
raise XeniaError("business_day.not_closed")
|
||||
|
||||
try:
|
||||
new_closed = parse_api_datetime(body.closed_at)
|
||||
except (ValueError, TypeError):
|
||||
raise HTTPException(status_code=422, detail="Invalid datetime format")
|
||||
raise XeniaError("validation.datetime")
|
||||
|
||||
now = utcnow()
|
||||
if new_closed > now:
|
||||
raise HTTPException(status_code=400, detail="Close time cannot be in the future")
|
||||
raise XeniaError("business_day.close_in_future")
|
||||
|
||||
# Must not be earlier than the latest order closed_at in this day
|
||||
last_order_close = (
|
||||
@@ -218,10 +210,7 @@ def patch_business_day(
|
||||
if last_order_close.tzinfo is None:
|
||||
last_order_close = last_order_close.replace(tzinfo=timezone.utc)
|
||||
if new_closed < last_order_close:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Close time cannot be earlier than the last order's close time ({_dt(last_order_close)})"
|
||||
)
|
||||
raise XeniaError("business_day.close_before_last_order", time=_dt(last_order_close))
|
||||
|
||||
day.closed_at = new_closed
|
||||
db.commit()
|
||||
@@ -237,16 +226,13 @@ def delete_business_day(
|
||||
"""Permanently delete a business day. Only allowed when it has no shifts."""
|
||||
day = db.query(BusinessDay).filter(BusinessDay.id == day_id).first()
|
||||
if not day:
|
||||
raise HTTPException(status_code=404, detail="Business day not found")
|
||||
raise XeniaError("business_day.not_found")
|
||||
if day.status == "open":
|
||||
raise HTTPException(status_code=400, detail="Cannot delete an open business day — close it first")
|
||||
raise XeniaError("business_day.delete_open")
|
||||
|
||||
shift_count = db.query(WaiterShift).filter(WaiterShift.business_day_id == day_id).count()
|
||||
if shift_count > 0:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail=f"Δεν είναι δυνατή η διαγραφή: η εργάσιμη μέρα έχει {shift_count} βάρδια/ες. Διαγράψτε πρώτα όλες τις βάρδιες.",
|
||||
)
|
||||
raise XeniaError("business_day.has_shifts", count=shift_count)
|
||||
|
||||
db.query(Order).filter(Order.business_day_id == day_id).update(
|
||||
{"business_day_id": None}, synchronize_session=False
|
||||
@@ -266,11 +252,11 @@ def business_day_summary(
|
||||
if day_id:
|
||||
day = db.query(BusinessDay).filter(BusinessDay.id == day_id).first()
|
||||
if not day:
|
||||
raise HTTPException(status_code=404, detail="Business day not found")
|
||||
raise XeniaError("business_day.not_found")
|
||||
else:
|
||||
day = db.query(BusinessDay).filter(BusinessDay.status == "open").first()
|
||||
if not day:
|
||||
raise HTTPException(status_code=404, detail="No open business day")
|
||||
raise XeniaError("business_day.none_open")
|
||||
|
||||
orders = db.query(Order).filter(Order.business_day_id == day.id).all()
|
||||
order_ids = [o.id for o in orders]
|
||||
|
||||
Reference in New Issue
Block a user