refactor: fix architecture violations with provider patterns and dependency inversion
Major changes: - Add AuditProvider protocol for cross-module audit logging - Move customer order operations to orders module (dependency inversion) - Add customer order metrics via MetricsProvider pattern - Fix missing db parameter in get_admin_context() calls - Move ProductMedia relationship to catalog module (proper ownership) - Add marketplace breakdown stats to marketplace_widgets New files: - contracts/audit.py - AuditProviderProtocol - core/services/audit_aggregator.py - Aggregates audit providers - monitoring/services/audit_provider.py - Monitoring audit implementation - orders/services/customer_order_service.py - Customer order operations - orders/routes/api/vendor_customer_orders.py - Customer order endpoints - catalog/services/product_media_service.py - Product media service - Architecture documentation for patterns Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,9 +19,7 @@ from models.schema.auth import UserContext
|
||||
from app.modules.customers.schemas import (
|
||||
CustomerDetailResponse,
|
||||
CustomerMessageResponse,
|
||||
CustomerOrdersResponse,
|
||||
CustomerResponse,
|
||||
CustomerStatisticsResponse,
|
||||
CustomerUpdate,
|
||||
VendorCustomerListResponse,
|
||||
)
|
||||
@@ -79,7 +77,9 @@ def get_customer_details(
|
||||
|
||||
- Get customer by ID
|
||||
- Verify customer belongs to vendor
|
||||
- Include order statistics
|
||||
|
||||
Note: Order statistics are available via the orders module endpoint:
|
||||
GET /api/vendor/customers/{customer_id}/order-stats
|
||||
"""
|
||||
# Service will raise CustomerNotFoundException if not found
|
||||
customer = customer_service.get_customer(
|
||||
@@ -88,13 +88,6 @@ def get_customer_details(
|
||||
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,
|
||||
@@ -104,55 +97,10 @@ def get_customer_details(
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
@vendor_router.get("/{customer_id}/orders", response_model=CustomerOrdersResponse)
|
||||
def get_customer_orders(
|
||||
customer_id: int,
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(50, ge=1, le=100),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
Get order history for a specific customer.
|
||||
|
||||
- Get all orders for customer
|
||||
- Filter by vendor_id
|
||||
- Return order details
|
||||
"""
|
||||
# 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,
|
||||
)
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
@vendor_router.put("/{customer_id}", response_model=CustomerMessageResponse)
|
||||
def update_customer(
|
||||
customer_id: int,
|
||||
@@ -203,26 +151,3 @@ def toggle_customer_status(
|
||||
status = "activated" if customer.is_active else "deactivated"
|
||||
return CustomerMessageResponse(message=f"Customer {status} successfully")
|
||||
|
||||
|
||||
@vendor_router.get("/{customer_id}/stats", response_model=CustomerStatisticsResponse)
|
||||
def get_customer_statistics(
|
||||
customer_id: int,
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
Get customer statistics and metrics.
|
||||
|
||||
- Total orders
|
||||
- Total spent
|
||||
- Average order value
|
||||
- Last order date
|
||||
"""
|
||||
# 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)
|
||||
|
||||
@@ -244,7 +244,12 @@ class VendorCustomerListResponse(BaseModel):
|
||||
|
||||
|
||||
class CustomerDetailResponse(BaseModel):
|
||||
"""Detailed customer response for vendor management."""
|
||||
"""Detailed customer response for vendor management.
|
||||
|
||||
Note: Order-related statistics (total_orders, total_spent, last_order_date)
|
||||
are available via the orders module endpoint:
|
||||
GET /api/vendor/customers/{customer_id}/order-stats
|
||||
"""
|
||||
|
||||
id: int | None = None
|
||||
vendor_id: int | None = None
|
||||
@@ -255,9 +260,6 @@ class CustomerDetailResponse(BaseModel):
|
||||
customer_number: str | None = None
|
||||
marketing_consent: bool | None = None
|
||||
preferred_language: str | None = None
|
||||
last_order_date: datetime | None = None
|
||||
total_orders: int | None = None
|
||||
total_spent: Decimal | None = None
|
||||
is_active: bool | None = None
|
||||
created_at: datetime | None = None
|
||||
updated_at: datetime | None = None
|
||||
|
||||
@@ -305,109 +305,11 @@ class CustomerService:
|
||||
|
||||
return customers, total
|
||||
|
||||
def get_customer_orders(
|
||||
self,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
customer_id: int,
|
||||
skip: int = 0,
|
||||
limit: int = 50,
|
||||
) -> tuple[list, int]:
|
||||
"""
|
||||
Get orders for a specific customer.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_id: Vendor ID
|
||||
customer_id: Customer ID
|
||||
skip: Pagination offset
|
||||
limit: Pagination limit
|
||||
|
||||
Returns:
|
||||
Tuple of (orders, total_count)
|
||||
|
||||
Raises:
|
||||
CustomerNotFoundException: If customer not found
|
||||
"""
|
||||
# Verify customer belongs to vendor
|
||||
self.get_customer(db, vendor_id, customer_id)
|
||||
|
||||
try:
|
||||
from app.modules.orders.models import Order
|
||||
|
||||
# Get customer orders
|
||||
query = (
|
||||
db.query(Order)
|
||||
.filter(
|
||||
Order.customer_id == customer_id,
|
||||
Order.vendor_id == vendor_id,
|
||||
)
|
||||
.order_by(Order.created_at.desc())
|
||||
)
|
||||
|
||||
total = query.count()
|
||||
orders = query.offset(skip).limit(limit).all()
|
||||
|
||||
return orders, total
|
||||
except ImportError:
|
||||
# Orders module not available
|
||||
logger.warning("Orders module not available for customer orders")
|
||||
return [], 0
|
||||
|
||||
def get_customer_statistics(
|
||||
self, db: Session, vendor_id: int, customer_id: int
|
||||
) -> dict:
|
||||
"""
|
||||
Get detailed statistics for a customer.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_id: Vendor ID
|
||||
customer_id: Customer ID
|
||||
|
||||
Returns:
|
||||
Dict with customer statistics
|
||||
"""
|
||||
customer = self.get_customer(db, vendor_id, customer_id)
|
||||
|
||||
# Get order statistics if orders module is available
|
||||
try:
|
||||
from sqlalchemy import func
|
||||
|
||||
from app.modules.orders.models import Order
|
||||
|
||||
order_stats = (
|
||||
db.query(
|
||||
func.count(Order.id).label("total_orders"),
|
||||
func.sum(Order.total_cents).label("total_spent_cents"),
|
||||
func.avg(Order.total_cents).label("avg_order_cents"),
|
||||
func.max(Order.created_at).label("last_order_date"),
|
||||
)
|
||||
.filter(Order.customer_id == customer_id)
|
||||
.first()
|
||||
)
|
||||
|
||||
total_orders = order_stats.total_orders or 0
|
||||
total_spent_cents = order_stats.total_spent_cents or 0
|
||||
avg_order_cents = order_stats.avg_order_cents or 0
|
||||
last_order_date = order_stats.last_order_date
|
||||
except ImportError:
|
||||
# Orders module not available
|
||||
logger.warning("Orders module not available for customer statistics")
|
||||
total_orders = 0
|
||||
total_spent_cents = 0
|
||||
avg_order_cents = 0
|
||||
last_order_date = None
|
||||
|
||||
return {
|
||||
"customer_id": customer_id,
|
||||
"total_orders": total_orders,
|
||||
"total_spent": total_spent_cents / 100, # Convert to euros
|
||||
"average_order_value": avg_order_cents / 100 if avg_order_cents else 0.0,
|
||||
"last_order_date": last_order_date,
|
||||
"member_since": customer.created_at,
|
||||
"is_active": customer.is_active,
|
||||
}
|
||||
# Note: Customer order methods have been moved to the orders module.
|
||||
# Use orders.services.customer_order_service for:
|
||||
# - get_customer_orders()
|
||||
# Use orders.services.order_metrics.get_customer_order_metrics() for:
|
||||
# - customer order statistics
|
||||
|
||||
def toggle_customer_status(
|
||||
self, db: Session, vendor_id: int, customer_id: int
|
||||
|
||||
Reference in New Issue
Block a user