Files
orion/app/api/v1/vendor/dashboard.py
Samir Boulahtit cad862f469 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>
2026-01-30 20:47:33 +01:00

86 lines
2.7 KiB
Python

# app/api/v1/vendor/dashboard.py
"""
Vendor dashboard and statistics endpoints.
Vendor Context: Uses token_vendor_id from JWT token (authenticated vendor API pattern).
The get_current_vendor_api dependency guarantees token_vendor_id is present.
"""
import logging
from fastapi import APIRouter, Depends, Request
from sqlalchemy.orm import Session
from app.api.deps import get_current_vendor_api
from app.core.database import get_db
from app.exceptions import VendorNotActiveException
from app.services.stats_service import stats_service
from app.services.vendor_service import vendor_service
from models.schema.auth import UserContext
from app.modules.analytics.schemas import (
VendorCustomerStats,
VendorDashboardStatsResponse,
VendorInfo,
VendorOrderStats,
VendorProductStats,
VendorRevenueStats,
)
router = APIRouter(prefix="/dashboard")
logger = logging.getLogger(__name__)
@router.get("/stats", response_model=VendorDashboardStatsResponse)
def get_vendor_dashboard_stats(
request: Request,
current_user: UserContext = Depends(get_current_vendor_api),
db: Session = Depends(get_db),
):
"""
Get vendor-specific dashboard statistics.
Returns statistics for the current vendor only:
- Total products in catalog
- Total orders
- Total customers
- Revenue metrics
Vendor is determined from the JWT token (vendor_id claim).
Requires Authorization header (API endpoint).
"""
vendor_id = current_user.token_vendor_id
# Get vendor object (raises VendorNotFoundException if not found)
vendor = vendor_service.get_vendor_by_id(db, vendor_id)
if not vendor.is_active:
raise VendorNotActiveException(vendor.vendor_code)
# Get vendor-scoped statistics
stats_data = stats_service.get_vendor_stats(db=db, vendor_id=vendor_id)
return VendorDashboardStatsResponse(
vendor=VendorInfo(
id=vendor.id,
name=vendor.name,
vendor_code=vendor.vendor_code,
),
products=VendorProductStats(
total=stats_data.get("total_products", 0),
active=stats_data.get("active_products", 0),
),
orders=VendorOrderStats(
total=stats_data.get("total_orders", 0),
pending=stats_data.get("pending_orders", 0),
completed=stats_data.get("completed_orders", 0),
),
customers=VendorCustomerStats(
total=stats_data.get("total_customers", 0),
active=stats_data.get("active_customers", 0),
),
revenue=VendorRevenueStats(
total=stats_data.get("total_revenue", 0),
this_month=stats_data.get("revenue_this_month", 0),
),
)