refactor(customers): migrate routes to module with auto-discovery
- Move customer route implementations to app/modules/customers/routes/
- Convert legacy app/api/v1/{admin,vendor}/customers.py to re-exports
- Update router registrations to use module routers with access control
- Fix CustomerListResponse pagination (page/per_page/total_pages)
- Update URL routing docs to use storefront consistently
- Fix mkdocs.yml nav references (shop -> storefront)
- Fix broken doc links in logging.md and cdn-fallback-strategy.md
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -49,7 +49,7 @@ from . import (
|
||||
code_quality,
|
||||
companies,
|
||||
# content_pages - moved to app.modules.cms.routes.api.admin
|
||||
customers,
|
||||
# customers - moved to app.modules.customers.routes.admin
|
||||
dashboard,
|
||||
email_templates,
|
||||
features,
|
||||
@@ -92,6 +92,9 @@ from app.modules.marketplace.routes.api.admin import admin_letzshop_router as le
|
||||
# CMS module router
|
||||
from app.modules.cms.routes.api.admin import router as cms_admin_router
|
||||
|
||||
# Customers module router
|
||||
from app.modules.customers.routes.admin import admin_router as customers_admin_router
|
||||
|
||||
# Create admin router
|
||||
router = APIRouter()
|
||||
|
||||
@@ -149,8 +152,9 @@ router.include_router(users.router, tags=["admin-users"])
|
||||
# Include admin user management endpoints (super admin only)
|
||||
router.include_router(admin_users.router, tags=["admin-admin-users"])
|
||||
|
||||
# Include customer management endpoints
|
||||
router.include_router(customers.router, tags=["admin-customers"])
|
||||
# Include customers module router (with module access control)
|
||||
router.include_router(customers_admin_router, tags=["admin-customers"])
|
||||
# Legacy: router.include_router(customers.router, tags=["admin-customers"])
|
||||
|
||||
|
||||
# ============================================================================
|
||||
|
||||
@@ -1,112 +1,31 @@
|
||||
# app/api/v1/admin/customers.py
|
||||
"""
|
||||
Customer management endpoints for admin.
|
||||
LEGACY LOCATION - Re-exports from module for backwards compatibility.
|
||||
|
||||
Provides admin-level access to customer data across all vendors.
|
||||
The canonical implementation is now in:
|
||||
app/modules/customers/routes/admin.py
|
||||
|
||||
This file exists to maintain backwards compatibility with code that
|
||||
imports from the old location. All new code should import directly
|
||||
from the module:
|
||||
|
||||
from app.modules.customers.routes.admin import admin_router
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_admin_api
|
||||
from app.core.database import get_db
|
||||
from app.services.admin_customer_service import admin_customer_service
|
||||
from models.schema.auth import UserContext
|
||||
from app.modules.customers.schemas import (
|
||||
CustomerDetailResponse,
|
||||
CustomerListResponse,
|
||||
CustomerMessageResponse,
|
||||
CustomerStatisticsResponse,
|
||||
from app.modules.customers.routes.admin import (
|
||||
admin_router,
|
||||
router,
|
||||
list_customers,
|
||||
get_customer_stats,
|
||||
get_customer,
|
||||
toggle_customer_status,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/customers")
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# List Customers
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@router.get("", response_model=CustomerListResponse)
|
||||
def list_customers(
|
||||
vendor_id: int | None = Query(None, description="Filter by vendor ID"),
|
||||
search: str = Query("", description="Search by email, name, or customer number"),
|
||||
is_active: bool | None = Query(None, description="Filter by active status"),
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(20, ge=1, le=100),
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
) -> CustomerListResponse:
|
||||
"""
|
||||
Get paginated list of customers across all vendors.
|
||||
|
||||
Admin can filter by vendor, search, and active status.
|
||||
"""
|
||||
customers, total = admin_customer_service.list_customers(
|
||||
db=db,
|
||||
vendor_id=vendor_id,
|
||||
search=search if search else None,
|
||||
is_active=is_active,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
return CustomerListResponse(
|
||||
customers=customers,
|
||||
total=total,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Customer Statistics
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@router.get("/stats", response_model=CustomerStatisticsResponse)
|
||||
def get_customer_stats(
|
||||
vendor_id: int | None = Query(None, description="Filter by vendor ID"),
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
) -> CustomerStatisticsResponse:
|
||||
"""Get customer statistics."""
|
||||
stats = admin_customer_service.get_customer_stats(db=db, vendor_id=vendor_id)
|
||||
return CustomerStatisticsResponse(**stats)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Get Single Customer
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@router.get("/{customer_id}", response_model=CustomerDetailResponse)
|
||||
def get_customer(
|
||||
customer_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
) -> CustomerDetailResponse:
|
||||
"""Get customer details by ID."""
|
||||
customer = admin_customer_service.get_customer(db=db, customer_id=customer_id)
|
||||
return CustomerDetailResponse(**customer)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Toggle Customer Status
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@router.patch("/{customer_id}/toggle-status", response_model=CustomerMessageResponse)
|
||||
def toggle_customer_status(
|
||||
customer_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
) -> CustomerMessageResponse:
|
||||
"""Toggle customer active status."""
|
||||
result = admin_customer_service.toggle_customer_status(
|
||||
db=db,
|
||||
customer_id=customer_id,
|
||||
admin_email=current_admin.email,
|
||||
)
|
||||
db.commit()
|
||||
return CustomerMessageResponse(message=result["message"])
|
||||
__all__ = [
|
||||
"admin_router",
|
||||
"router",
|
||||
"list_customers",
|
||||
"get_customer_stats",
|
||||
"get_customer",
|
||||
"toggle_customer_status",
|
||||
]
|
||||
|
||||
11
app/api/v1/vendor/__init__.py
vendored
11
app/api/v1/vendor/__init__.py
vendored
@@ -35,7 +35,7 @@ from . import (
|
||||
auth,
|
||||
billing,
|
||||
# content_pages - moved to app.modules.cms.routes.api.vendor
|
||||
customers,
|
||||
# customers - moved to app.modules.customers.routes.vendor
|
||||
dashboard,
|
||||
email_settings,
|
||||
email_templates,
|
||||
@@ -71,6 +71,9 @@ from app.modules.marketplace.routes.api.vendor import vendor_letzshop_router as
|
||||
# CMS module router
|
||||
from app.modules.cms.routes.api.vendor import router as cms_vendor_router
|
||||
|
||||
# Customers module router
|
||||
from app.modules.customers.routes.vendor import vendor_router as customers_vendor_router
|
||||
|
||||
# Create vendor router
|
||||
router = APIRouter()
|
||||
|
||||
@@ -104,7 +107,11 @@ router.include_router(orders_exceptions_router, tags=["vendor-order-exceptions"]
|
||||
# Legacy: router.include_router(order_item_exceptions.router, tags=["vendor-order-exceptions"])
|
||||
|
||||
router.include_router(invoices.router, tags=["vendor-invoices"])
|
||||
router.include_router(customers.router, tags=["vendor-customers"])
|
||||
|
||||
# Include customers module router (with module access control)
|
||||
router.include_router(customers_vendor_router, tags=["vendor-customers"])
|
||||
# Legacy: router.include_router(customers.router, tags=["vendor-customers"])
|
||||
|
||||
router.include_router(team.router, tags=["vendor-team"])
|
||||
|
||||
# Include inventory module router (with module access control)
|
||||
|
||||
244
app/api/v1/vendor/customers.py
vendored
244
app/api/v1/vendor/customers.py
vendored
@@ -1,223 +1,35 @@
|
||||
# app/api/v1/vendor/customers.py
|
||||
"""
|
||||
Vendor customer management endpoints.
|
||||
LEGACY LOCATION - Re-exports from module for backwards compatibility.
|
||||
|
||||
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.
|
||||
The canonical implementation is now in:
|
||||
app/modules/customers/routes/vendor.py
|
||||
|
||||
This file exists to maintain backwards compatibility with code that
|
||||
imports from the old location. All new code should import directly
|
||||
from the module:
|
||||
|
||||
from app.modules.customers.routes.vendor import vendor_router
|
||||
"""
|
||||
|
||||
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.customer_service import customer_service
|
||||
from models.schema.auth import UserContext
|
||||
from app.modules.customers.schemas import (
|
||||
CustomerDetailResponse,
|
||||
CustomerMessageResponse,
|
||||
CustomerOrdersResponse,
|
||||
CustomerResponse,
|
||||
CustomerStatisticsResponse,
|
||||
CustomerUpdate,
|
||||
VendorCustomerListResponse,
|
||||
from app.modules.customers.routes.vendor import (
|
||||
vendor_router,
|
||||
router,
|
||||
get_vendor_customers,
|
||||
get_customer_details,
|
||||
get_customer_orders,
|
||||
update_customer,
|
||||
toggle_customer_status,
|
||||
get_customer_statistics,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/customers")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.get("", response_model=VendorCustomerListResponse)
|
||||
def get_vendor_customers(
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
search: str | None = Query(None),
|
||||
is_active: bool | None = Query(None),
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
Get all customers for this vendor.
|
||||
|
||||
- Query customers filtered by vendor_id
|
||||
- Support search by name/email
|
||||
- Support filtering by active status
|
||||
- Return paginated results
|
||||
"""
|
||||
customers, total = customer_service.get_vendor_customers(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
search=search,
|
||||
is_active=is_active,
|
||||
)
|
||||
|
||||
return VendorCustomerListResponse(
|
||||
customers=[CustomerResponse.model_validate(c) for c in customers],
|
||||
total=total,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{customer_id}", response_model=CustomerDetailResponse)
|
||||
def get_customer_details(
|
||||
customer_id: int,
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
Get detailed customer information.
|
||||
|
||||
- Get customer by ID
|
||||
- Verify customer belongs to vendor
|
||||
- Include order statistics
|
||||
"""
|
||||
# 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,
|
||||
)
|
||||
|
||||
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)
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
@router.put("/{customer_id}", response_model=CustomerMessageResponse)
|
||||
def update_customer(
|
||||
customer_id: int,
|
||||
customer_data: CustomerUpdate,
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
Update customer information.
|
||||
|
||||
- Update customer details
|
||||
- Verify customer belongs to vendor
|
||||
"""
|
||||
# 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()
|
||||
|
||||
return CustomerMessageResponse(message="Customer updated successfully")
|
||||
|
||||
|
||||
@router.put("/{customer_id}/status", response_model=CustomerMessageResponse)
|
||||
def toggle_customer_status(
|
||||
customer_id: int,
|
||||
current_user: UserContext = Depends(get_current_vendor_api),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
Activate/deactivate customer account.
|
||||
|
||||
- Toggle customer is_active status
|
||||
- Verify customer belongs to vendor
|
||||
"""
|
||||
# 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()
|
||||
|
||||
status = "activated" if customer.is_active else "deactivated"
|
||||
return CustomerMessageResponse(message=f"Customer {status} successfully")
|
||||
|
||||
|
||||
@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)
|
||||
__all__ = [
|
||||
"vendor_router",
|
||||
"router",
|
||||
"get_vendor_customers",
|
||||
"get_customer_details",
|
||||
"get_customer_orders",
|
||||
"update_customer",
|
||||
"toggle_customer_status",
|
||||
"get_customer_statistics",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user