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

@@ -22,7 +22,7 @@ from app.core.database import get_db
from app.exceptions.base import ResourceNotFoundException, ValidationException
from app.services.email_service import EmailService
from app.services.email_template_service import EmailTemplateService
from models.database.user import User
from models.schema.auth import UserContext
router = APIRouter(prefix="/email-templates")
logger = logging.getLogger(__name__)
@@ -92,7 +92,7 @@ class CategoriesResponse(BaseModel):
@router.get("", response_model=TemplateListResponse)
def list_templates(
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""
@@ -106,7 +106,7 @@ def list_templates(
@router.get("/categories", response_model=CategoriesResponse)
def get_categories(
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""Get list of email template categories."""
@@ -117,7 +117,7 @@ def get_categories(
@router.get("/{code}")
def get_template(
code: str,
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""
@@ -133,7 +133,7 @@ def get_template(
def get_template_language(
code: str,
language: str,
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""
@@ -164,7 +164,7 @@ def update_template(
code: str,
language: str,
template_data: TemplateUpdate,
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""
@@ -189,7 +189,7 @@ def update_template(
def preview_template(
code: str,
preview_data: PreviewRequest,
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""
@@ -212,7 +212,7 @@ def preview_template(
def send_test_email(
code: str,
test_data: TestEmailRequest,
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""
@@ -258,7 +258,7 @@ def get_template_logs(
code: str,
limit: int = 50,
offset: int = 0,
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""