2026-06-29 00:10:10 +02:00
|
|
|
"""User management routes."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
2026-06-29 17:43:56 +02:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, Response, status
|
2026-08-24 10:55:22 +02:00
|
|
|
from pydantic import BaseModel, Field
|
2026-07-25 21:03:46 +02:00
|
|
|
from sqlalchemy import select
|
2026-08-16 01:17:18 +02:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
from app.core.audit import log_audit
|
2026-08-06 12:05:09 +02:00
|
|
|
from app.core.auth import get_redis, invalidate_all_user_sessions
|
2026-06-29 00:10:10 +02:00
|
|
|
from app.core.db import get_db
|
2026-08-16 01:17:18 +02:00
|
|
|
from app.core.notifications import post_system_message
|
2026-07-15 21:59:45 +02:00
|
|
|
from app.core.permissions import invalidate_permission_cache
|
2026-07-24 21:46:27 +02:00
|
|
|
from app.deps import get_current_user, require_permission
|
2026-08-16 01:17:18 +02:00
|
|
|
from app.models.user import User
|
|
|
|
|
from app.schemas.user import PaginatedUsers, UserCreate, UserResponse, UserUpdate
|
2026-07-29 02:37:51 +02:00
|
|
|
from app.services.owner_transfer_service import transfer_ownership
|
2026-08-16 01:17:18 +02:00
|
|
|
from app.services.user_service import _UNSET, user_service
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api/v1/users", tags=["users"])
|
|
|
|
|
|
|
|
|
|
|
2026-08-24 10:55:22 +02:00
|
|
|
class MenuOrderRequest(BaseModel):
|
|
|
|
|
"""Update the current user's menu order preference."""
|
|
|
|
|
|
|
|
|
|
menu_order: list[str] = Field(..., min_length=0)
|
|
|
|
|
|
|
|
|
|
|
2026-07-03 19:49:48 +00:00
|
|
|
def _parse_role_id(raw: str | None) -> uuid.UUID | None:
|
|
|
|
|
"""Convert a string body value into a UUID or None.
|
|
|
|
|
|
2026-07-03 20:43:55 +00:00
|
|
|
Empty string and None are both treated as "clear role_id".
|
2026-07-03 19:49:48 +00:00
|
|
|
"""
|
|
|
|
|
if raw is None or raw == "":
|
|
|
|
|
return None
|
|
|
|
|
try:
|
|
|
|
|
return uuid.UUID(raw)
|
|
|
|
|
except (ValueError, AttributeError):
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
400,
|
|
|
|
|
detail={"detail": "Invalid role_id", "code": "invalid_role_id"},
|
|
|
|
|
) from None
|
|
|
|
|
|
|
|
|
|
|
2026-07-23 22:44:54 +02:00
|
|
|
@router.get("", response_model=PaginatedUsers)
|
2026-06-29 00:10:10 +02:00
|
|
|
async def list_users(
|
|
|
|
|
page: int = Query(1, ge=1),
|
|
|
|
|
page_size: int = Query(25, ge=1, le=100),
|
|
|
|
|
search: str | None = Query(None),
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("users:read")),
|
2026-06-29 00:10:10 +02:00
|
|
|
):
|
|
|
|
|
"""List users (admin only, paginated)."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
return await user_service.list_users(db, tenant_id, page, page_size, search)
|
|
|
|
|
|
|
|
|
|
|
2026-07-23 22:44:54 +02:00
|
|
|
@router.post("", status_code=status.HTTP_201_CREATED, response_model=UserResponse)
|
2026-06-29 00:10:10 +02:00
|
|
|
async def create_user(
|
|
|
|
|
body: UserCreate,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("users:write")),
|
2026-06-29 00:10:10 +02:00
|
|
|
):
|
|
|
|
|
"""Create a new user (admin only)."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
user_id = uuid.UUID(current_user["user_id"])
|
2026-07-03 19:49:48 +00:00
|
|
|
role_id = _parse_role_id(body.role_id)
|
2026-06-29 00:10:10 +02:00
|
|
|
|
2026-08-03 22:32:03 +02:00
|
|
|
# Mass-Assignment protection: only system admin can create admin users
|
|
|
|
|
role = body.role
|
|
|
|
|
if role == "admin" and not current_user.get("is_system_admin"):
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
|
|
|
detail={"detail": "Only system admin can create admin users", "code": "role_escalation_forbidden"},
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-31 00:58:05 +02:00
|
|
|
try:
|
|
|
|
|
user = await user_service.create_user(
|
|
|
|
|
db,
|
|
|
|
|
tenant_id,
|
|
|
|
|
body.email,
|
|
|
|
|
body.name,
|
|
|
|
|
body.password,
|
2026-08-03 22:32:03 +02:00
|
|
|
role,
|
2026-07-31 00:58:05 +02:00
|
|
|
role_id,
|
|
|
|
|
body.is_active,
|
|
|
|
|
)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
|
if isinstance(exc, IntegrityError):
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
|
|
|
detail={"detail": "User with this email already exists", "code": "duplicate_email"},
|
|
|
|
|
) from exc
|
|
|
|
|
raise
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
# Audit log
|
|
|
|
|
await log_audit(
|
2026-06-29 17:43:56 +02:00
|
|
|
db,
|
|
|
|
|
tenant_id,
|
|
|
|
|
user_id,
|
|
|
|
|
"create",
|
|
|
|
|
"user",
|
|
|
|
|
user.id,
|
2026-07-03 19:49:48 +00:00
|
|
|
changes={"email": body.email, "name": body.name, "role": body.role, "role_id": body.role_id},
|
2026-06-29 00:10:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Notification
|
2026-08-16 01:17:18 +02:00
|
|
|
await post_system_message(
|
2026-06-29 17:43:56 +02:00
|
|
|
db,
|
|
|
|
|
tenant_id,
|
|
|
|
|
user.id,
|
|
|
|
|
"info",
|
2026-06-29 00:10:10 +02:00
|
|
|
"Account created",
|
|
|
|
|
f"Your account has been created by {current_user['name']}.",
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-25 03:22:55 +02:00
|
|
|
# Publish user.created event
|
|
|
|
|
from app.core.event_bus import get_event_bus
|
|
|
|
|
event_bus = get_event_bus()
|
|
|
|
|
await event_bus.publish('user.created', {
|
|
|
|
|
'user_id': str(user.id),
|
|
|
|
|
'tenant_id': str(tenant_id),
|
|
|
|
|
'email': body.email,
|
|
|
|
|
'name': body.name,
|
|
|
|
|
'role': body.role,
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-29 00:10:10 +02:00
|
|
|
return {
|
|
|
|
|
"id": str(user.id),
|
|
|
|
|
"email": user.email,
|
|
|
|
|
"name": user.name,
|
2026-07-25 21:03:46 +02:00
|
|
|
"role": body.role,
|
|
|
|
|
"role_id": str(role_id) if role_id else None,
|
2026-06-29 00:10:10 +02:00
|
|
|
"is_active": user.is_active,
|
2026-07-25 21:03:46 +02:00
|
|
|
"tenant_id": str(tenant_id),
|
2026-06-29 00:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-07-23 22:44:54 +02:00
|
|
|
@router.get("/{user_id}", response_model=UserResponse)
|
2026-06-29 00:10:10 +02:00
|
|
|
async def get_user(
|
|
|
|
|
user_id: str,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("users:read")),
|
2026-06-29 00:10:10 +02:00
|
|
|
):
|
|
|
|
|
"""Get a single user."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
try:
|
|
|
|
|
uid = uuid.UUID(user_id)
|
|
|
|
|
except ValueError:
|
2026-06-29 17:43:56 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
400, detail={"detail": "Invalid user_id", "code": "invalid_id"}
|
|
|
|
|
) from None
|
2026-06-29 00:10:10 +02:00
|
|
|
|
2026-07-25 21:03:46 +02:00
|
|
|
result = await user_service.get_user(db, tenant_id, uid)
|
|
|
|
|
if result is None:
|
2026-06-29 00:10:10 +02:00
|
|
|
raise HTTPException(404, detail={"detail": "User not found", "code": "not_found"})
|
|
|
|
|
|
2026-07-25 21:03:46 +02:00
|
|
|
user, user_tenant = result
|
2026-06-29 00:10:10 +02:00
|
|
|
return {
|
|
|
|
|
"id": str(user.id),
|
|
|
|
|
"email": user.email,
|
|
|
|
|
"name": user.name,
|
2026-07-25 21:03:46 +02:00
|
|
|
"role": user_tenant.role,
|
|
|
|
|
"role_id": str(user_tenant.role_id) if user_tenant.role_id else None,
|
2026-06-29 00:10:10 +02:00
|
|
|
"is_active": user.is_active,
|
2026-07-25 21:03:46 +02:00
|
|
|
"tenant_id": str(user_tenant.tenant_id),
|
2026-06-29 00:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.patch("/{user_id}")
|
|
|
|
|
async def update_user(
|
|
|
|
|
user_id: str,
|
|
|
|
|
body: UserUpdate,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("users:write")),
|
2026-06-29 00:10:10 +02:00
|
|
|
):
|
2026-07-03 20:43:55 +00:00
|
|
|
"""Update a user (admin only).
|
|
|
|
|
|
|
|
|
|
Uses ``model_fields_set`` to detect whether ``role_id`` was explicitly
|
|
|
|
|
present in the request body (even if sent as ``null``). This allows
|
|
|
|
|
the caller to clear the FK by sending ``role_id: null``.
|
2026-07-15 21:59:45 +02:00
|
|
|
|
|
|
|
|
Self-modification prevention: a user cannot change their own role,
|
|
|
|
|
is_active status, or system_admin flag.
|
2026-07-03 20:43:55 +00:00
|
|
|
"""
|
2026-06-29 00:10:10 +02:00
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
acting_user_id = uuid.UUID(current_user["user_id"])
|
|
|
|
|
try:
|
|
|
|
|
uid = uuid.UUID(user_id)
|
|
|
|
|
except ValueError:
|
2026-06-29 17:43:56 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
400, detail={"detail": "Invalid user_id", "code": "invalid_id"}
|
|
|
|
|
) from None
|
2026-06-29 00:10:10 +02:00
|
|
|
|
2026-07-15 21:59:45 +02:00
|
|
|
# Self-modification prevention: cannot change own role or active status
|
|
|
|
|
if uid == acting_user_id:
|
|
|
|
|
if body.role is not None or body.is_active is not None or "role_id" in body.model_fields_set:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
|
|
|
detail={"detail": "Cannot modify your own role or active status", "code": "self_modification_forbidden"},
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-03 22:32:03 +02:00
|
|
|
# Mass-Assignment protection: only system admin can change roles to admin
|
|
|
|
|
if body.role == "admin" and not current_user.get("is_system_admin"):
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
|
|
|
detail={"detail": "Only system admin can assign admin role", "code": "role_escalation_forbidden"},
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-06 12:05:09 +02:00
|
|
|
# Only system admin can change is_system_admin flag
|
|
|
|
|
if body.is_system_admin is not None and not current_user.get("is_system_admin"):
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
|
|
|
detail={"detail": "Only system admin can change system admin flag", "code": "admin_flag_forbidden"},
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-03 20:43:55 +00:00
|
|
|
# Determine if role_id was explicitly sent (Pydantic v2)
|
|
|
|
|
role_id_sent = "role_id" in body.model_fields_set
|
|
|
|
|
|
2026-06-29 00:10:10 +02:00
|
|
|
changes: dict[str, Any] = {}
|
|
|
|
|
if body.name is not None:
|
|
|
|
|
changes["name"] = body.name
|
|
|
|
|
if body.role is not None:
|
|
|
|
|
changes["role"] = body.role
|
2026-07-03 20:43:55 +00:00
|
|
|
if role_id_sent:
|
2026-07-03 19:49:48 +00:00
|
|
|
changes["role_id"] = body.role_id
|
2026-06-29 00:10:10 +02:00
|
|
|
if body.is_active is not None:
|
|
|
|
|
changes["is_active"] = body.is_active
|
2026-08-06 12:05:09 +02:00
|
|
|
if body.is_system_admin is not None:
|
|
|
|
|
changes["is_system_admin"] = body.is_system_admin
|
2026-06-29 00:10:10 +02:00
|
|
|
|
2026-07-03 20:43:55 +00:00
|
|
|
# Pass _UNSET sentinel when role_id was not in the request body
|
|
|
|
|
# so the service leaves the existing value untouched.
|
|
|
|
|
role_id: uuid.UUID | None | Any
|
|
|
|
|
if role_id_sent:
|
|
|
|
|
role_id = _parse_role_id(body.role_id)
|
|
|
|
|
else:
|
|
|
|
|
role_id = _UNSET
|
2026-07-03 19:49:48 +00:00
|
|
|
|
2026-07-24 17:17:53 +02:00
|
|
|
# Handle profile fields
|
|
|
|
|
if body.first_name is not None:
|
|
|
|
|
changes["first_name"] = body.first_name
|
|
|
|
|
if body.last_name is not None:
|
|
|
|
|
changes["last_name"] = body.last_name
|
|
|
|
|
if body.email is not None:
|
|
|
|
|
changes["email"] = body.email
|
|
|
|
|
if body.avatar_url is not None:
|
|
|
|
|
changes["avatar_url"] = body.avatar_url
|
|
|
|
|
if body.new_password is not None:
|
|
|
|
|
changes["password_changed"] = True
|
|
|
|
|
|
|
|
|
|
try:
|
2026-07-25 21:03:46 +02:00
|
|
|
result = await user_service.update_user(
|
2026-07-24 17:17:53 +02:00
|
|
|
db,
|
|
|
|
|
tenant_id,
|
|
|
|
|
uid,
|
|
|
|
|
body.name,
|
|
|
|
|
body.role,
|
|
|
|
|
role_id,
|
|
|
|
|
body.is_active,
|
|
|
|
|
body.first_name,
|
|
|
|
|
body.last_name,
|
|
|
|
|
body.avatar_url,
|
|
|
|
|
body.email,
|
|
|
|
|
body.current_password,
|
|
|
|
|
body.new_password,
|
2026-08-06 12:05:09 +02:00
|
|
|
body.is_system_admin,
|
2026-07-24 17:17:53 +02:00
|
|
|
)
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise HTTPException(400, detail={"detail": str(exc), "code": "invalid_password"}) from None
|
2026-07-25 21:03:46 +02:00
|
|
|
if result is None:
|
2026-06-29 00:10:10 +02:00
|
|
|
raise HTTPException(404, detail={"detail": "User not found", "code": "not_found"})
|
|
|
|
|
|
2026-07-25 21:03:46 +02:00
|
|
|
user, user_tenant = result
|
2026-07-29 02:37:51 +02:00
|
|
|
|
|
|
|
|
# Auto-transfer ownership when user is deactivated
|
|
|
|
|
if body.is_active is False:
|
|
|
|
|
await transfer_ownership(
|
|
|
|
|
db,
|
|
|
|
|
tenant_id,
|
|
|
|
|
from_user_id=uid,
|
|
|
|
|
to_user_id=acting_user_id,
|
|
|
|
|
)
|
|
|
|
|
|
2026-06-29 00:10:10 +02:00
|
|
|
await log_audit(db, tenant_id, acting_user_id, "update", "user", uid, changes=changes)
|
|
|
|
|
|
2026-07-15 21:59:45 +02:00
|
|
|
# Invalidate permission cache for the updated user
|
|
|
|
|
redis = get_redis()
|
|
|
|
|
await invalidate_permission_cache(redis, uid, tenant_id)
|
|
|
|
|
|
2026-08-06 12:05:09 +02:00
|
|
|
# If is_system_admin was changed, invalidate ALL sessions for this user
|
|
|
|
|
# so the stale admin flag doesn't persist in Redis until TTL (8h)
|
|
|
|
|
if body.is_system_admin is not None:
|
|
|
|
|
try:
|
|
|
|
|
await invalidate_all_user_sessions(redis, uid)
|
|
|
|
|
except Exception:
|
|
|
|
|
pass # Best-effort — don't fail the update if Redis is down
|
|
|
|
|
|
2026-06-29 00:10:10 +02:00
|
|
|
return {
|
|
|
|
|
"id": str(user.id),
|
|
|
|
|
"email": user.email,
|
|
|
|
|
"name": user.name,
|
2026-07-24 17:17:53 +02:00
|
|
|
"first_name": user.first_name,
|
|
|
|
|
"last_name": user.last_name,
|
|
|
|
|
"avatar_url": user.avatar_url,
|
2026-07-25 21:03:46 +02:00
|
|
|
"role": user_tenant.role,
|
|
|
|
|
"role_id": str(user_tenant.role_id) if user_tenant.role_id else None,
|
2026-06-29 00:10:10 +02:00
|
|
|
"is_active": user.is_active,
|
2026-07-25 21:03:46 +02:00
|
|
|
"tenant_id": str(user_tenant.tenant_id),
|
2026-06-29 00:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/{user_id}")
|
|
|
|
|
async def delete_user(
|
|
|
|
|
user_id: str,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("users:write")),
|
2026-06-29 00:10:10 +02:00
|
|
|
):
|
|
|
|
|
"""Delete a user (admin only)."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
acting_user_id = uuid.UUID(current_user["user_id"])
|
|
|
|
|
try:
|
|
|
|
|
uid = uuid.UUID(user_id)
|
|
|
|
|
except ValueError:
|
2026-06-29 17:43:56 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
400, detail={"detail": "Invalid user_id", "code": "invalid_id"}
|
|
|
|
|
) from None
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
# Get user snapshot for audit before deletion
|
2026-07-25 21:03:46 +02:00
|
|
|
result = await user_service.get_user(db, tenant_id, uid)
|
|
|
|
|
if result is None:
|
2026-06-29 00:10:10 +02:00
|
|
|
raise HTTPException(404, detail={"detail": "User not found", "code": "not_found"})
|
2026-07-25 21:03:46 +02:00
|
|
|
user, user_tenant = result
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
success = await user_service.delete_user(db, tenant_id, uid)
|
|
|
|
|
if not success:
|
|
|
|
|
raise HTTPException(404, detail={"detail": "User not found", "code": "not_found"})
|
|
|
|
|
|
|
|
|
|
await log_audit(
|
2026-06-29 17:43:56 +02:00
|
|
|
db,
|
|
|
|
|
tenant_id,
|
|
|
|
|
acting_user_id,
|
|
|
|
|
"delete",
|
|
|
|
|
"user",
|
|
|
|
|
uid,
|
2026-06-29 00:10:10 +02:00
|
|
|
changes={"email": user.email, "name": user.name},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
2026-07-24 21:46:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/me/menu-order")
|
|
|
|
|
async def get_menu_order(
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
current_user: dict = Depends(get_current_user),
|
|
|
|
|
):
|
|
|
|
|
"""Get the current user's menu order preference."""
|
|
|
|
|
user_id = uuid.UUID(current_user["user_id"])
|
|
|
|
|
|
|
|
|
|
result = await db.execute(
|
2026-07-25 21:03:46 +02:00
|
|
|
select(User).where(User.id == user_id)
|
2026-07-24 21:46:27 +02:00
|
|
|
)
|
|
|
|
|
user = result.scalar_one_or_none()
|
|
|
|
|
if user is None:
|
|
|
|
|
raise HTTPException(404, detail={"detail": "User not found", "code": "not_found"})
|
|
|
|
|
|
|
|
|
|
menu_order = user.preferences.get("menu_order", [])
|
|
|
|
|
return {"menu_order": menu_order}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.put("/me/menu-order")
|
|
|
|
|
async def update_menu_order(
|
2026-08-24 10:55:22 +02:00
|
|
|
body: MenuOrderRequest,
|
2026-07-24 21:46:27 +02:00
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
current_user: dict = Depends(get_current_user),
|
|
|
|
|
):
|
|
|
|
|
"""Update the current user's menu order preference."""
|
|
|
|
|
user_id = uuid.UUID(current_user["user_id"])
|
|
|
|
|
|
2026-08-24 10:55:22 +02:00
|
|
|
menu_order = body.menu_order
|
2026-07-24 21:46:27 +02:00
|
|
|
|
|
|
|
|
result = await db.execute(
|
2026-07-25 21:03:46 +02:00
|
|
|
select(User).where(User.id == user_id)
|
2026-07-24 21:46:27 +02:00
|
|
|
)
|
|
|
|
|
user = result.scalar_one_or_none()
|
|
|
|
|
if user is None:
|
|
|
|
|
raise HTTPException(404, detail={"detail": "User not found", "code": "not_found"})
|
|
|
|
|
|
|
|
|
|
prefs = dict(user.preferences) if user.preferences else {}
|
|
|
|
|
prefs["menu_order"] = menu_order
|
|
|
|
|
user.preferences = prefs
|
|
|
|
|
await db.commit()
|
|
|
|
|
|
|
|
|
|
return {"menu_order": menu_order}
|