The require_module_access dependency was using path-based detection to determine admin vs vendor authentication, which failed for API routes (/api/v1/admin/*) because it only checked for /admin/*. Changes: - Make frontend_type parameter mandatory (was optional with fallback) - Remove path-based detection logic from require_module_access - Update all 33 module route files to pass explicit FrontendType: - 15 admin routes use FrontendType.ADMIN - 18 vendor routes use FrontendType.VENDOR This ensures authentication method is explicitly declared at route definition time, making it independent of URL structure and future-proof for API version changes. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
# app/modules/analytics/routes/api/vendor.py
|
|
"""
|
|
Vendor Analytics API
|
|
|
|
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.
|
|
|
|
Feature Requirements:
|
|
- basic_reports: Basic analytics (Essential tier)
|
|
- analytics_dashboard: Advanced analytics (Business tier)
|
|
"""
|
|
|
|
import logging
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_vendor_api, get_db, require_module_access
|
|
from app.modules.billing.dependencies.feature_gate import RequireFeature
|
|
from app.modules.analytics.services import stats_service
|
|
from app.modules.analytics.schemas import (
|
|
VendorAnalyticsCatalog,
|
|
VendorAnalyticsImports,
|
|
VendorAnalyticsInventory,
|
|
VendorAnalyticsResponse,
|
|
)
|
|
from app.modules.billing.models import FeatureCode
|
|
from app.modules.enums import FrontendType
|
|
from app.modules.tenancy.models import User
|
|
|
|
router = APIRouter(
|
|
prefix="/analytics",
|
|
dependencies=[Depends(require_module_access("analytics", FrontendType.VENDOR))],
|
|
)
|
|
vendor_router = router # Alias for discovery
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@router.get("", response_model=VendorAnalyticsResponse)
|
|
def get_vendor_analytics(
|
|
period: str = Query("30d", description="Time period: 7d, 30d, 90d, 1y"),
|
|
current_user: User = Depends(get_current_vendor_api),
|
|
db: Session = Depends(get_db),
|
|
_: None = Depends(RequireFeature(FeatureCode.BASIC_REPORTS, FeatureCode.ANALYTICS_DASHBOARD)),
|
|
):
|
|
"""Get vendor analytics data for specified time period."""
|
|
data = stats_service.get_vendor_analytics(db, current_user.token_vendor_id, period)
|
|
|
|
return VendorAnalyticsResponse(
|
|
period=data["period"],
|
|
start_date=data["start_date"],
|
|
imports=VendorAnalyticsImports(count=data["imports"]["count"]),
|
|
catalog=VendorAnalyticsCatalog(
|
|
products_added=data["catalog"]["products_added"]
|
|
),
|
|
inventory=VendorAnalyticsInventory(
|
|
total_locations=data["inventory"]["total_locations"]
|
|
),
|
|
)
|