- Remove |safe from |tojson in HTML attributes (x-data) - quotes must become " for browsers to parse correctly - Update LANG-002 and LANG-003 architecture rules to document correct |tojson usage patterns: - HTML attributes: |tojson (no |safe) - Script blocks: |tojson|safe - Fix validator to warn when |tojson|safe is used in x-data (breaks HTML attribute parsing) - Improve code quality across services, APIs, and tests 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
# app/api/v1/vendor/analytics.py
|
|
"""
|
|
Vendor analytics and reporting 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, Query
|
|
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
|
|
from models.schema.stats import (
|
|
VendorAnalyticsCatalog,
|
|
VendorAnalyticsImports,
|
|
VendorAnalyticsInventory,
|
|
VendorAnalyticsResponse,
|
|
)
|
|
|
|
router = APIRouter(prefix="/analytics")
|
|
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),
|
|
):
|
|
"""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"]
|
|
),
|
|
)
|