fix: resolve architecture validation errors in media and customers APIs
- Add proper media exceptions (MediaNotFoundException, MediaUploadException, etc.) - Update media service to use exceptions from app/exceptions/media - Remove direct HTTPException raises from vendor/media.py and vendor/customers.py - Move db.query from customers API to service layer (get_customer_orders) - Fix pagination macro call in vendor/media.html template - Update media.js: add parent init call, PlatformSettings, apiClient.postFormData - Add try/catch error handling to media.js init method 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
187
app/api/v1/vendor/customers.py
vendored
187
app/api/v1/vendor/customers.py
vendored
@@ -8,14 +8,12 @@ The get_current_vendor_api dependency guarantees token_vendor_id is present.
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
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.exceptions.customer import CustomerNotFoundException
|
||||
from app.services.customer_service import customer_service
|
||||
from models.database.order import Order
|
||||
from models.database.user import User
|
||||
from models.schema.customer import (
|
||||
CustomerDetailResponse,
|
||||
@@ -78,38 +76,35 @@ def get_customer_details(
|
||||
- Verify customer belongs to vendor
|
||||
- Include order statistics
|
||||
"""
|
||||
try:
|
||||
customer = customer_service.get_customer(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
)
|
||||
# Service will raise CustomerNotFoundException if not found
|
||||
customer = customer_service.get_customer(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
)
|
||||
|
||||
# Get statistics
|
||||
stats = customer_service.get_customer_statistics(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
)
|
||||
# Get statistics
|
||||
stats = customer_service.get_customer_statistics(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
)
|
||||
|
||||
return CustomerDetailResponse(
|
||||
id=customer.id,
|
||||
email=customer.email,
|
||||
first_name=customer.first_name,
|
||||
last_name=customer.last_name,
|
||||
phone=customer.phone,
|
||||
customer_number=customer.customer_number,
|
||||
is_active=customer.is_active,
|
||||
marketing_consent=customer.marketing_consent,
|
||||
total_orders=stats["total_orders"],
|
||||
total_spent=stats["total_spent"],
|
||||
average_order_value=stats["average_order_value"],
|
||||
last_order_date=stats["last_order_date"],
|
||||
created_at=customer.created_at,
|
||||
)
|
||||
|
||||
except CustomerNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Customer not found")
|
||||
return CustomerDetailResponse(
|
||||
id=customer.id,
|
||||
email=customer.email,
|
||||
first_name=customer.first_name,
|
||||
last_name=customer.last_name,
|
||||
phone=customer.phone,
|
||||
customer_number=customer.customer_number,
|
||||
is_active=customer.is_active,
|
||||
marketing_consent=customer.marketing_consent,
|
||||
total_orders=stats["total_orders"],
|
||||
total_spent=stats["total_spent"],
|
||||
average_order_value=stats["average_order_value"],
|
||||
last_order_date=stats["last_order_date"],
|
||||
created_at=customer.created_at,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{customer_id}/orders", response_model=CustomerOrdersResponse)
|
||||
@@ -127,45 +122,30 @@ def get_customer_orders(
|
||||
- Filter by vendor_id
|
||||
- Return order details
|
||||
"""
|
||||
try:
|
||||
# Verify customer belongs to vendor
|
||||
customer_service.get_customer(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
)
|
||||
# Service will raise CustomerNotFoundException if not found
|
||||
orders, total = customer_service.get_customer_orders(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
# Get customer orders
|
||||
query = (
|
||||
db.query(Order)
|
||||
.filter(
|
||||
Order.customer_id == customer_id,
|
||||
Order.vendor_id == current_user.token_vendor_id,
|
||||
)
|
||||
.order_by(Order.created_at.desc())
|
||||
)
|
||||
|
||||
total = query.count()
|
||||
orders = query.offset(skip).limit(limit).all()
|
||||
|
||||
return CustomerOrdersResponse(
|
||||
orders=[
|
||||
{
|
||||
"id": o.id,
|
||||
"order_number": o.order_number,
|
||||
"status": o.status,
|
||||
"total": o.total_cents / 100 if o.total_cents else 0,
|
||||
"created_at": o.created_at,
|
||||
}
|
||||
for o in orders
|
||||
],
|
||||
total=total,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
except CustomerNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Customer not found")
|
||||
return CustomerOrdersResponse(
|
||||
orders=[
|
||||
{
|
||||
"id": o.id,
|
||||
"order_number": o.order_number,
|
||||
"status": o.status,
|
||||
"total": o.total_cents / 100 if o.total_cents else 0,
|
||||
"created_at": o.created_at,
|
||||
}
|
||||
for o in orders
|
||||
],
|
||||
total=total,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
|
||||
@router.put("/{customer_id}", response_model=CustomerMessageResponse)
|
||||
@@ -181,23 +161,17 @@ def update_customer(
|
||||
- Update customer details
|
||||
- Verify customer belongs to vendor
|
||||
"""
|
||||
try:
|
||||
customer_service.update_customer(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
customer_data=customer_data,
|
||||
)
|
||||
# Service will raise CustomerNotFoundException if not found
|
||||
customer_service.update_customer(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
customer_data=customer_data,
|
||||
)
|
||||
|
||||
db.commit()
|
||||
db.commit()
|
||||
|
||||
return CustomerMessageResponse(message="Customer updated successfully")
|
||||
|
||||
except CustomerNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Customer not found")
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
return CustomerMessageResponse(message="Customer updated successfully")
|
||||
|
||||
|
||||
@router.put("/{customer_id}/status", response_model=CustomerMessageResponse)
|
||||
@@ -212,23 +186,17 @@ def toggle_customer_status(
|
||||
- Toggle customer is_active status
|
||||
- Verify customer belongs to vendor
|
||||
"""
|
||||
try:
|
||||
customer = customer_service.toggle_customer_status(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
)
|
||||
# Service will raise CustomerNotFoundException if not found
|
||||
customer = customer_service.toggle_customer_status(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
)
|
||||
|
||||
db.commit()
|
||||
db.commit()
|
||||
|
||||
status = "activated" if customer.is_active else "deactivated"
|
||||
return CustomerMessageResponse(message=f"Customer {status} successfully")
|
||||
|
||||
except CustomerNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Customer not found")
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
status = "activated" if customer.is_active else "deactivated"
|
||||
return CustomerMessageResponse(message=f"Customer {status} successfully")
|
||||
|
||||
|
||||
@router.get("/{customer_id}/stats", response_model=CustomerStatisticsResponse)
|
||||
@@ -245,14 +213,11 @@ def get_customer_statistics(
|
||||
- Average order value
|
||||
- Last order date
|
||||
"""
|
||||
try:
|
||||
stats = customer_service.get_customer_statistics(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
)
|
||||
# Service will raise CustomerNotFoundException if not found
|
||||
stats = customer_service.get_customer_statistics(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
)
|
||||
|
||||
return CustomerStatisticsResponse(**stats)
|
||||
|
||||
except CustomerNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Customer not found")
|
||||
return CustomerStatisticsResponse(**stats)
|
||||
|
||||
Reference in New Issue
Block a user