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:
@@ -2,7 +2,7 @@ import os
|
||||
import uuid
|
||||
import bcrypt
|
||||
from core.i18n import normalize_language
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, UploadFile, File
|
||||
from fastapi import APIRouter, Depends, status, UploadFile, File
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from pydantic import BaseModel
|
||||
@@ -29,14 +29,14 @@ AVATAR_DIR = "/app/data/avatars"
|
||||
def _user_or_404(user_id: int, db: Session) -> User:
|
||||
u = db.query(User).filter(User.id == user_id).first()
|
||||
if not u:
|
||||
raise HTTPException(status_code=404, detail="Staff member not found")
|
||||
raise XeniaError("staff.not_found")
|
||||
return u
|
||||
|
||||
|
||||
def _guard_superadmin_target(target: User, acting_user: User):
|
||||
"""Prevent any modification of a superadmin by a non-superadmin."""
|
||||
if target.role == "superadmin" and acting_user.role != "superadmin":
|
||||
raise HTTPException(status_code=403, detail="Cannot modify a superadmin account")
|
||||
raise XeniaError("staff.superadmin_protected")
|
||||
|
||||
|
||||
def _guard_grant(acting_user: User, keys) -> None:
|
||||
@@ -146,11 +146,11 @@ def list_staff(db: Session = Depends(get_db), user: User = Depends(require_staff
|
||||
@router.post("/", response_model=UserOut, status_code=status.HTTP_201_CREATED)
|
||||
def create_staff(body: UserCreate, db: Session = Depends(get_db), user: User = Depends(require_staff_manager)):
|
||||
if body.role not in VALID_ROLES:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid role. Valid roles: {sorted(VALID_ROLES)}")
|
||||
raise XeniaError("staff.role_invalid", role=body.role)
|
||||
if body.role == "superadmin" and user.role != "superadmin":
|
||||
raise HTTPException(status_code=403, detail="Only a superadmin can create another superadmin")
|
||||
raise XeniaError("staff.superadmin_only")
|
||||
if db.query(User).filter(User.username == body.username).first():
|
||||
raise HTTPException(status_code=400, detail="Username already exists")
|
||||
raise XeniaError("staff.username_taken", status=400)
|
||||
|
||||
pin_hash = bcrypt.hashpw(body.pin.encode(), bcrypt.gensalt()).decode()
|
||||
new_user = User(
|
||||
@@ -186,9 +186,9 @@ def update_staff(waiter_id: int, body: UserUpdate, db: Session = Depends(get_db)
|
||||
_guard_superadmin_target(target, user)
|
||||
|
||||
if body.role is not None and body.role not in VALID_ROLES:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid role. Valid roles: {sorted(VALID_ROLES)}")
|
||||
raise XeniaError("staff.role_invalid", role=body.role)
|
||||
if body.role == "superadmin" and user.role != "superadmin":
|
||||
raise HTTPException(status_code=403, detail="Only a superadmin can assign the superadmin role")
|
||||
raise XeniaError("staff.superadmin_only")
|
||||
|
||||
if body.language is not None:
|
||||
body.language = normalize_language(body.language) or ""
|
||||
@@ -238,7 +238,7 @@ def delete_staff(waiter_id: int, db: Session = Depends(get_db), user: User = Dep
|
||||
async def upload_avatar(waiter_id: int, file: UploadFile = File(...), db: Session = Depends(get_db), user: User = Depends(require_staff_manager)):
|
||||
target = _user_or_404(waiter_id, db)
|
||||
if not file.content_type.startswith("image/"):
|
||||
raise HTTPException(status_code=400, detail="File must be an image")
|
||||
raise XeniaError("staff.avatar_type")
|
||||
|
||||
if target.avatar_url:
|
||||
old_path = os.path.join(AVATAR_DIR, os.path.basename(target.avatar_url))
|
||||
@@ -305,7 +305,7 @@ def assign_assistant(waiter_id: int, assistant_id: int, db: Session = Depends(ge
|
||||
AssistantAssignment.assistant_waiter_id == assistant_id,
|
||||
).first()
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail="Assignment already exists")
|
||||
raise XeniaError("staff.assignment_exists")
|
||||
assignment = AssistantAssignment(primary_waiter_id=waiter_id, assistant_waiter_id=assistant_id)
|
||||
db.add(assignment)
|
||||
db.commit()
|
||||
@@ -319,6 +319,6 @@ def remove_assistant(waiter_id: int, db: Session = Depends(get_db), user: User =
|
||||
AssistantAssignment.primary_waiter_id == waiter_id
|
||||
).first()
|
||||
if not assignment:
|
||||
raise HTTPException(status_code=404, detail="Assignment not found")
|
||||
raise XeniaError("staff.assignment_not_found")
|
||||
db.delete(assignment)
|
||||
db.commit()
|
||||
|
||||
Reference in New Issue
Block a user