refactor(arch): use CustomerContext schema for dependency injection
Phase 5 of storefront restructure plan - fix direct model imports in API routes by using schemas for dependency injection. Created CustomerContext schema: - Lightweight Pydantic model for customer data in API routes - Populated from Customer DB model in auth dependency - Contains all fields needed by storefront routes - Includes from_db_model() factory method Updated app/api/deps.py: - _validate_customer_token now returns CustomerContext instead of Customer - Updated docstrings for all customer auth functions Updated module storefront routes: - customers: Uses CustomerContext for profile/address endpoints - orders: Uses CustomerContext for order history endpoints - checkout: Uses CustomerContext for order placement - messaging: Uses CustomerContext for messaging endpoints This enforces the layered architecture (Routes → Services → Models) by ensuring API routes never import database models directly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -716,7 +716,7 @@ def get_current_vendor_api(
|
||||
|
||||
def _validate_customer_token(token: str, request: Request, db: Session):
|
||||
"""
|
||||
Validate customer JWT token and return Customer object.
|
||||
Validate customer JWT token and return CustomerContext schema.
|
||||
|
||||
Validates:
|
||||
1. Token signature and expiration
|
||||
@@ -730,7 +730,7 @@ def _validate_customer_token(token: str, request: Request, db: Session):
|
||||
db: Database session
|
||||
|
||||
Returns:
|
||||
Customer: Authenticated customer object
|
||||
CustomerContext: Authenticated customer context schema
|
||||
|
||||
Raises:
|
||||
InvalidTokenException: If token is invalid or expired
|
||||
@@ -741,6 +741,7 @@ def _validate_customer_token(token: str, request: Request, db: Session):
|
||||
from jose import JWTError, jwt
|
||||
|
||||
from app.modules.customers.models.customer import Customer
|
||||
from app.modules.customers.schemas import CustomerContext
|
||||
|
||||
# Decode and validate customer JWT token
|
||||
try:
|
||||
@@ -800,7 +801,8 @@ def _validate_customer_token(token: str, request: Request, db: Session):
|
||||
|
||||
logger.debug(f"Customer authenticated: {customer.email} (ID: {customer.id})")
|
||||
|
||||
return customer
|
||||
# Return CustomerContext schema instead of database model
|
||||
return CustomerContext.from_db_model(customer)
|
||||
|
||||
|
||||
def get_current_customer_from_cookie_or_header(
|
||||
@@ -828,7 +830,7 @@ def get_current_customer_from_cookie_or_header(
|
||||
db: Database session
|
||||
|
||||
Returns:
|
||||
Customer: Authenticated customer object
|
||||
CustomerContext: Authenticated customer context schema
|
||||
|
||||
Raises:
|
||||
InvalidTokenException: If no token or invalid token
|
||||
@@ -862,7 +864,7 @@ def get_current_customer_api(
|
||||
db: Database session
|
||||
|
||||
Returns:
|
||||
Customer: Authenticated customer object
|
||||
CustomerContext: Authenticated customer context schema
|
||||
|
||||
Raises:
|
||||
InvalidTokenException: If no token or invalid token
|
||||
@@ -1327,7 +1329,7 @@ def get_current_customer_optional(
|
||||
db: Database session
|
||||
|
||||
Returns:
|
||||
Customer: Authenticated customer if valid token exists
|
||||
CustomerContext: Authenticated customer context if valid token exists
|
||||
None: If no token, invalid token, or vendor mismatch
|
||||
"""
|
||||
token, source = _get_token_from_request(
|
||||
|
||||
Reference in New Issue
Block a user