- Replace black, isort, and flake8 with Ruff (all-in-one linter and formatter) - Add comprehensive pyproject.toml configuration - Simplify Makefile code quality targets - Configure exclusions for venv/.venv in pyproject.toml - Auto-fix 1,359 linting issues across codebase Benefits: - Much faster builds (Ruff is written in Rust) - Single tool replaces multiple tools - More comprehensive rule set (UP, B, C4, SIM, PIE, RET, Q) - All configuration centralized in pyproject.toml - Better import sorting and formatting consistency 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
# app/api/v1/vendor/dashboard.py
|
|
"""
|
|
Vendor dashboard and statistics endpoints.
|
|
"""
|
|
|
|
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.services.stats_service import stats_service
|
|
from models.database.user import User
|
|
|
|
router = APIRouter(prefix="/dashboard")
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@router.get("/stats")
|
|
def get_vendor_dashboard_stats(
|
|
request: Request,
|
|
current_user: User = 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 authenticated user's vendor_user association.
|
|
Requires Authorization header (API endpoint).
|
|
"""
|
|
# Get vendor from authenticated user's vendor_user record
|
|
from models.database.vendor import VendorUser
|
|
|
|
vendor_user = (
|
|
db.query(VendorUser).filter(VendorUser.user_id == current_user.id).first()
|
|
)
|
|
|
|
if not vendor_user:
|
|
from fastapi import HTTPException
|
|
|
|
raise HTTPException(
|
|
status_code=403, detail="User is not associated with any vendor"
|
|
)
|
|
|
|
vendor = vendor_user.vendor
|
|
if not vendor or not vendor.is_active:
|
|
from fastapi import HTTPException
|
|
|
|
raise HTTPException(status_code=404, detail="Vendor not found or inactive")
|
|
|
|
# Get vendor-scoped statistics
|
|
stats_data = stats_service.get_vendor_stats(db=db, vendor_id=vendor.id)
|
|
|
|
return {
|
|
"vendor": {
|
|
"id": vendor.id,
|
|
"name": vendor.name,
|
|
"vendor_code": vendor.vendor_code,
|
|
},
|
|
"products": {
|
|
"total": stats_data.get("total_products", 0),
|
|
"active": stats_data.get("active_products", 0),
|
|
},
|
|
"orders": {
|
|
"total": stats_data.get("total_orders", 0),
|
|
"pending": stats_data.get("pending_orders", 0),
|
|
"completed": stats_data.get("completed_orders", 0),
|
|
},
|
|
"customers": {
|
|
"total": stats_data.get("total_customers", 0),
|
|
"active": stats_data.get("active_customers", 0),
|
|
},
|
|
"revenue": {
|
|
"total": stats_data.get("total_revenue", 0),
|
|
"this_month": stats_data.get("revenue_this_month", 0),
|
|
},
|
|
}
|