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

@@ -24,7 +24,7 @@ from app.api.deps import get_current_vendor_api
from app.core.database import get_db
from app.exceptions import FeatureNotFoundError
from app.services.feature_service import feature_service
from models.database.user import User
from models.schema.auth import UserContext
router = APIRouter(prefix="/features")
logger = logging.getLogger(__name__)
@@ -114,7 +114,7 @@ class FeatureCheckResponse(BaseModel):
@router.get("/available", response_model=FeatureCodeListResponse)
def get_available_features(
current_user: User = Depends(get_current_vendor_api),
current_user: UserContext = Depends(get_current_vendor_api),
db: Session = Depends(get_db),
):
"""
@@ -148,7 +148,7 @@ def get_available_features(
def get_features(
category: str | None = Query(None, description="Filter by category"),
include_unavailable: bool = Query(True, description="Include features not available to vendor"),
current_user: User = Depends(get_current_vendor_api),
current_user: UserContext = Depends(get_current_vendor_api),
db: Session = Depends(get_db),
):
"""
@@ -211,7 +211,7 @@ def get_features(
@router.get("/categories", response_model=CategoryListResponse)
def get_feature_categories(
current_user: User = Depends(get_current_vendor_api),
current_user: UserContext = Depends(get_current_vendor_api),
db: Session = Depends(get_db),
):
"""
@@ -226,7 +226,7 @@ def get_feature_categories(
@router.get("/grouped", response_model=FeatureGroupedResponse)
def get_features_grouped(
current_user: User = Depends(get_current_vendor_api),
current_user: UserContext = Depends(get_current_vendor_api),
db: Session = Depends(get_db),
):
"""
@@ -273,7 +273,7 @@ def get_features_grouped(
@router.get("/{feature_code}", response_model=FeatureDetailResponse)
def get_feature_detail(
feature_code: str,
current_user: User = Depends(get_current_vendor_api),
current_user: UserContext = Depends(get_current_vendor_api),
db: Session = Depends(get_db),
):
"""
@@ -328,7 +328,7 @@ def get_feature_detail(
@router.get("/check/{feature_code}", response_model=FeatureCheckResponse)
def check_feature(
feature_code: str,
current_user: User = Depends(get_current_vendor_api),
current_user: UserContext = Depends(get_current_vendor_api),
db: Session = Depends(get_db),
):
"""