refactor(api): introduce UserContext schema for API dependency injection

Replace direct User database model imports in API endpoints with UserContext
schema, following the architecture principle that API routes should not import
database models directly.

Changes:
- Create UserContext schema in models/schema/auth.py with from_user() factory
- Update app/api/deps.py to return UserContext from all auth dependencies
- Add _get_user_model() helper for functions needing User model access
- Update 58 API endpoint files to use UserContext instead of User
- Add noqa comments for 4 legitimate edge cases (enums, internal helpers)

Architecture validation: 0 errors (down from 61), 11 warnings remain

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-30 20:47:33 +01:00
parent 1ad30bd77e
commit cad862f469
60 changed files with 755 additions and 589 deletions

View File

@@ -29,8 +29,8 @@ from app.api.deps import (
)
from app.services.menu_service import MenuItemConfig, menu_service
from app.services.platform_service import platform_service
from models.database.admin_menu_config import FrontendType
from models.database.user import User
from models.database.admin_menu_config import FrontendType # noqa: API-007 - Enum for type safety
from models.schema.auth import UserContext
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/menu-config")
@@ -159,7 +159,7 @@ async def get_platform_menu_config(
FrontendType.ADMIN, description="Frontend type (admin or vendor)"
),
db: Session = Depends(get_db),
current_user: User = Depends(get_current_super_admin),
current_user: UserContext = Depends(get_current_super_admin),
):
"""
Get menu configuration for a platform.
@@ -188,7 +188,7 @@ async def update_platform_menu_visibility(
FrontendType.ADMIN, description="Frontend type (admin or vendor)"
),
db: Session = Depends(get_db),
current_user: User = Depends(get_current_super_admin),
current_user: UserContext = Depends(get_current_super_admin),
):
"""
Update visibility for a single menu item for a platform.
@@ -224,7 +224,7 @@ async def bulk_update_platform_menu_visibility(
FrontendType.ADMIN, description="Frontend type (admin or vendor)"
),
db: Session = Depends(get_db),
current_user: User = Depends(get_current_super_admin),
current_user: UserContext = Depends(get_current_super_admin),
):
"""
Update visibility for multiple menu items at once.
@@ -257,7 +257,7 @@ async def reset_platform_menu_config(
FrontendType.ADMIN, description="Frontend type (admin or vendor)"
),
db: Session = Depends(get_db),
current_user: User = Depends(get_current_super_admin),
current_user: UserContext = Depends(get_current_super_admin),
):
"""
Reset menu configuration for a platform to defaults.
@@ -287,7 +287,7 @@ async def reset_platform_menu_config(
@router.get("/user", response_model=MenuConfigResponse)
async def get_user_menu_config(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_super_admin),
current_user: UserContext = Depends(get_current_super_admin),
):
"""
Get the current super admin's personal menu configuration.
@@ -309,7 +309,7 @@ async def get_user_menu_config(
async def update_user_menu_visibility(
update_data: MenuVisibilityUpdateRequest,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_super_admin),
current_user: UserContext = Depends(get_current_super_admin),
):
"""
Update visibility for a single menu item for the current super admin.
@@ -336,7 +336,7 @@ async def update_user_menu_visibility(
@router.post("/user/reset", response_model=MenuActionResponse)
async def reset_user_menu_config(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_super_admin),
current_user: UserContext = Depends(get_current_super_admin),
):
"""
Reset the current super admin's menu configuration (hide all except mandatory).
@@ -356,7 +356,7 @@ async def reset_user_menu_config(
@router.post("/user/show-all", response_model=MenuActionResponse)
async def show_all_user_menu_config(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_super_admin),
current_user: UserContext = Depends(get_current_super_admin),
):
"""
Show all menu items for the current super admin.
@@ -380,7 +380,7 @@ async def show_all_platform_menu_config(
FrontendType.ADMIN, description="Frontend type (admin or vendor)"
),
db: Session = Depends(get_db),
current_user: User = Depends(get_current_super_admin),
current_user: UserContext = Depends(get_current_super_admin),
):
"""
Show all menu items for a platform.
@@ -409,7 +409,7 @@ async def show_all_platform_menu_config(
@router.get("/render/admin", response_model=RenderedMenuResponse)
async def get_rendered_admin_menu(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_admin_from_cookie_or_header),
current_user: UserContext = Depends(get_current_admin_from_cookie_or_header),
):
"""
Get the rendered admin menu for the current user.