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:
22
app/api/v1/vendor/messages.py
vendored
22
app/api/v1/vendor/messages.py
vendored
@@ -47,7 +47,7 @@ from app.modules.messaging.schemas import (
|
||||
ReopenConversationResponse,
|
||||
UnreadCountResponse,
|
||||
)
|
||||
from models.database.user import User
|
||||
from models.schema.auth import UserContext
|
||||
|
||||
router = APIRouter(prefix="/messages")
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -177,7 +177,7 @@ def list_conversations(
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(20, ge=1, le=100),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_vendor_api),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
) -> ConversationListResponse:
|
||||
"""List conversations for vendor (vendor_customer and admin_vendor channels)."""
|
||||
vendor_id = current_user.token_vendor_id
|
||||
@@ -208,7 +208,7 @@ def list_conversations(
|
||||
@router.get("/unread-count", response_model=UnreadCountResponse)
|
||||
def get_unread_count(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_vendor_api),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
) -> UnreadCountResponse:
|
||||
"""Get total unread message count for header badge."""
|
||||
vendor_id = current_user.token_vendor_id
|
||||
@@ -234,7 +234,7 @@ def get_recipients(
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(50, ge=1, le=100),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_vendor_api),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
) -> RecipientListResponse:
|
||||
"""Get list of available recipients for compose modal."""
|
||||
vendor_id = current_user.token_vendor_id
|
||||
@@ -275,7 +275,7 @@ def get_recipients(
|
||||
def create_conversation(
|
||||
data: ConversationCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_vendor_api),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
) -> ConversationDetailResponse:
|
||||
"""Create a new conversation with a customer."""
|
||||
vendor_id = current_user.token_vendor_id
|
||||
@@ -399,7 +399,7 @@ def get_conversation(
|
||||
conversation_id: int,
|
||||
mark_read: bool = Query(True, description="Automatically mark as read"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_vendor_api),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
) -> ConversationDetailResponse:
|
||||
"""Get conversation detail with messages."""
|
||||
vendor_id = current_user.token_vendor_id
|
||||
@@ -442,7 +442,7 @@ async def send_message(
|
||||
content: str = Form(...),
|
||||
files: list[UploadFile] = File(default=[]),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_vendor_api),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
) -> MessageResponse:
|
||||
"""Send a message in a conversation, optionally with attachments."""
|
||||
vendor_id = current_user.token_vendor_id
|
||||
@@ -505,7 +505,7 @@ async def send_message(
|
||||
def close_conversation(
|
||||
conversation_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_vendor_api),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
) -> CloseConversationResponse:
|
||||
"""Close a conversation."""
|
||||
vendor_id = current_user.token_vendor_id
|
||||
@@ -547,7 +547,7 @@ def close_conversation(
|
||||
def reopen_conversation(
|
||||
conversation_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_vendor_api),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
) -> ReopenConversationResponse:
|
||||
"""Reopen a closed conversation."""
|
||||
vendor_id = current_user.token_vendor_id
|
||||
@@ -589,7 +589,7 @@ def reopen_conversation(
|
||||
def mark_read(
|
||||
conversation_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_vendor_api),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
) -> MarkReadResponse:
|
||||
"""Mark conversation as read."""
|
||||
success = messaging_service.mark_conversation_read(
|
||||
@@ -617,7 +617,7 @@ def update_preferences(
|
||||
conversation_id: int,
|
||||
preferences: NotificationPreferencesUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_vendor_api),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
) -> PreferencesUpdateResponse:
|
||||
"""Update notification preferences for a conversation."""
|
||||
success = messaging_service.update_notification_preferences(
|
||||
|
||||
Reference in New Issue
Block a user