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)
|
||||
|
||||
183
app/api/v1/vendor/media.py
vendored
183
app/api/v1/vendor/media.py
vendored
@@ -8,12 +8,13 @@ The get_current_vendor_api dependency guarantees token_vendor_id is present.
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, Query, UploadFile
|
||||
from fastapi import APIRouter, Depends, File, Query, UploadFile
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_vendor_api
|
||||
from app.core.database import get_db
|
||||
from app.services.media_service import MediaNotFoundException, media_service
|
||||
from app.exceptions.media import MediaOptimizationException
|
||||
from app.services.media_service import media_service
|
||||
from models.database.user import User
|
||||
from models.schema.media import (
|
||||
MediaDetailResponse,
|
||||
@@ -86,35 +87,29 @@ async def upload_media(
|
||||
- Save metadata to database
|
||||
- Return file URL
|
||||
"""
|
||||
try:
|
||||
# Read file content
|
||||
file_content = await file.read()
|
||||
# Read file content
|
||||
file_content = await file.read()
|
||||
|
||||
# Upload using service
|
||||
media_file = await media_service.upload_file(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
file_content=file_content,
|
||||
filename=file.filename or "unnamed",
|
||||
folder=folder or "general",
|
||||
)
|
||||
# Upload using service (exceptions will propagate to handler)
|
||||
media_file = await media_service.upload_file(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
file_content=file_content,
|
||||
filename=file.filename or "unnamed",
|
||||
folder=folder or "general",
|
||||
)
|
||||
|
||||
db.commit()
|
||||
db.commit()
|
||||
|
||||
return MediaUploadResponse(
|
||||
id=media_file.id,
|
||||
file_url=media_file.file_url,
|
||||
thumbnail_url=media_file.thumbnail_url,
|
||||
filename=media_file.original_filename,
|
||||
file_size=media_file.file_size,
|
||||
media_type=media_file.media_type,
|
||||
message="File uploaded successfully",
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Failed to upload media: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
return MediaUploadResponse(
|
||||
id=media_file.id,
|
||||
file_url=media_file.file_url,
|
||||
thumbnail_url=media_file.thumbnail_url,
|
||||
filename=media_file.original_filename,
|
||||
file_size=media_file.file_size,
|
||||
media_type=media_file.media_type,
|
||||
message="File uploaded successfully",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/upload/multiple", response_model=MultipleUploadResponse)
|
||||
@@ -185,17 +180,14 @@ def get_media_details(
|
||||
- Return file URL
|
||||
- Return basic info
|
||||
"""
|
||||
try:
|
||||
media = media_service.get_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
# Service will raise MediaNotFoundException if not found
|
||||
media = media_service.get_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
|
||||
return MediaDetailResponse.model_validate(media)
|
||||
|
||||
except MediaNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Media file not found")
|
||||
return MediaDetailResponse.model_validate(media)
|
||||
|
||||
|
||||
@router.put("/{media_id}", response_model=MediaDetailResponse)
|
||||
@@ -213,27 +205,21 @@ def update_media_metadata(
|
||||
- Update description
|
||||
- Move to different folder
|
||||
"""
|
||||
try:
|
||||
media = media_service.update_media_metadata(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
filename=metadata.filename,
|
||||
alt_text=metadata.alt_text,
|
||||
description=metadata.description,
|
||||
folder=metadata.folder,
|
||||
metadata=metadata.metadata,
|
||||
)
|
||||
# Service will raise MediaNotFoundException if not found
|
||||
media = media_service.update_media_metadata(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
filename=metadata.filename,
|
||||
alt_text=metadata.alt_text,
|
||||
description=metadata.description,
|
||||
folder=metadata.folder,
|
||||
metadata=metadata.metadata,
|
||||
)
|
||||
|
||||
db.commit()
|
||||
db.commit()
|
||||
|
||||
return MediaDetailResponse.model_validate(media)
|
||||
|
||||
except MediaNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Media file not found")
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
return MediaDetailResponse.model_validate(media)
|
||||
|
||||
|
||||
@router.delete("/{media_id}", response_model=MediaDetailResponse)
|
||||
@@ -250,22 +236,16 @@ def delete_media(
|
||||
- Delete database record
|
||||
- Return success/error
|
||||
"""
|
||||
try:
|
||||
media_service.delete_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
# Service will raise MediaNotFoundException if not found
|
||||
media_service.delete_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
|
||||
db.commit()
|
||||
db.commit()
|
||||
|
||||
return MediaDetailResponse(message="Media file deleted successfully")
|
||||
|
||||
except MediaNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Media file not found")
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
return MediaDetailResponse(message="Media file deleted successfully")
|
||||
|
||||
|
||||
@router.get("/{media_id}/usage", response_model=MediaUsageResponse)
|
||||
@@ -280,17 +260,14 @@ def get_media_usage(
|
||||
- Check products using this media
|
||||
- Return list of usage
|
||||
"""
|
||||
try:
|
||||
usage = media_service.get_media_usage(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
# Service will raise MediaNotFoundException if not found
|
||||
usage = media_service.get_media_usage(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
|
||||
return MediaUsageResponse(**usage)
|
||||
|
||||
except MediaNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Media file not found")
|
||||
return MediaUsageResponse(**usage)
|
||||
|
||||
|
||||
@router.post("/optimize/{media_id}", response_model=OptimizationResultResponse)
|
||||
@@ -304,30 +281,24 @@ def optimize_media(
|
||||
|
||||
Note: Image optimization requires PIL/Pillow to be installed.
|
||||
"""
|
||||
try:
|
||||
media = media_service.get_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
# Service will raise MediaNotFoundException if not found
|
||||
media = media_service.get_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
|
||||
if media.media_type != "image":
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Only images can be optimized"
|
||||
)
|
||||
if media.media_type != "image":
|
||||
raise MediaOptimizationException("Only images can be optimized")
|
||||
|
||||
# For now, return current state - optimization is done on upload
|
||||
return OptimizationResultResponse(
|
||||
media_id=media_id,
|
||||
original_size=media.file_size,
|
||||
optimized_size=media.optimized_size or media.file_size,
|
||||
savings_percent=0.0 if not media.optimized_size else
|
||||
round((1 - media.optimized_size / media.file_size) * 100, 1),
|
||||
optimized_url=media.file_url,
|
||||
message="Image optimization applied on upload" if media.is_optimized
|
||||
else "Image not yet optimized",
|
||||
)
|
||||
|
||||
except MediaNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Media file not found")
|
||||
# For now, return current state - optimization is done on upload
|
||||
return OptimizationResultResponse(
|
||||
media_id=media_id,
|
||||
original_size=media.file_size,
|
||||
optimized_size=media.optimized_size or media.file_size,
|
||||
savings_percent=0.0 if not media.optimized_size else
|
||||
round((1 - media.optimized_size / media.file_size) * 100, 1),
|
||||
optimized_url=media.file_url,
|
||||
message="Image optimization applied on upload" if media.is_optimized
|
||||
else "Image not yet optimized",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user