132 lines
4.1 KiB
Python
132 lines
4.1 KiB
Python
"""
|
|
@router AdminCustomerRouter (Backend/app/api/v1/routers/AdminCustomerRouter.py)
|
|
@purpose CRM Admin endpoint controllers to search, list, and inspect storefront e-commerce customers, addresses, and order history.
|
|
"""
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from pydantic import BaseModel, EmailStr
|
|
from typing import List, Optional
|
|
from datetime import datetime
|
|
|
|
from app.core.database.db_session import get_db
|
|
from app.core.permissions.RoleChecker import get_current_user
|
|
from app.models.UserModel import User
|
|
from app.models.EcomCustomerModel import EcomCustomer, CustomerAddress
|
|
from app.models.OrderModel import Order
|
|
|
|
router = APIRouter(prefix="/api/v1/admin/customers", tags=["Admin CRM Storefront Customers"])
|
|
|
|
# --- Schemas ---
|
|
|
|
class CustomerAddressSchema(BaseModel):
|
|
address_id: str
|
|
address_type: str
|
|
full_name: str
|
|
phone: str
|
|
street_address: str
|
|
city: str
|
|
state: str
|
|
pincode: str
|
|
is_default: bool
|
|
|
|
class CustomerOrderSummary(BaseModel):
|
|
order_id: str
|
|
order_no: str
|
|
final_amount: float
|
|
status: str
|
|
created_at: datetime
|
|
|
|
class AdminCustomerListResponse(BaseModel):
|
|
customer_id: str
|
|
email: str
|
|
first_name: str
|
|
last_name: str
|
|
phone: Optional[str] = None
|
|
created_at: datetime
|
|
|
|
class AdminCustomerDetailResponse(BaseModel):
|
|
customer_id: str
|
|
email: str
|
|
first_name: str
|
|
last_name: str
|
|
phone: Optional[str] = None
|
|
profile_picture: Optional[str] = None
|
|
created_at: datetime
|
|
addresses: List[CustomerAddressSchema]
|
|
orders: List[CustomerOrderSummary]
|
|
|
|
# --- Endpoints ---
|
|
|
|
@router.get("", response_model=List[AdminCustomerListResponse])
|
|
def list_all_customers(
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
List all registered storefront customers.
|
|
"""
|
|
customers = db.query(EcomCustomer).order_by(EcomCustomer.created_at.desc()).all()
|
|
return [
|
|
AdminCustomerListResponse(
|
|
customer_id=c.customer_id,
|
|
email=c.email,
|
|
first_name=c.first_name,
|
|
last_name=c.last_name,
|
|
phone=c.phone,
|
|
created_at=c.created_at
|
|
) for c in customers
|
|
]
|
|
|
|
@router.get("/{customer_id}", response_model=AdminCustomerDetailResponse)
|
|
def get_customer_details(
|
|
customer_id: str,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Retrieve deep profile details, saved address books, and complete sales purchase history of a storefront customer.
|
|
"""
|
|
customer = db.query(EcomCustomer).filter(EcomCustomer.customer_id == customer_id).first()
|
|
if not customer:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Storefront customer not found"
|
|
)
|
|
|
|
# Fetch addresses
|
|
addresses = db.query(CustomerAddress).filter(CustomerAddress.customer_id == customer_id).all()
|
|
|
|
# Fetch orders
|
|
orders = db.query(Order).filter(Order.customer_id == customer_id).order_by(Order.created_at.desc()).all()
|
|
|
|
return AdminCustomerDetailResponse(
|
|
customer_id=customer.customer_id,
|
|
email=customer.email,
|
|
first_name=customer.first_name,
|
|
last_name=customer.last_name,
|
|
phone=customer.phone,
|
|
profile_picture=customer.profile_picture,
|
|
created_at=customer.created_at,
|
|
addresses=[
|
|
CustomerAddressSchema(
|
|
address_id=a.address_id,
|
|
address_type=a.address_type,
|
|
full_name=a.full_name,
|
|
phone=a.phone,
|
|
street_address=a.street_address,
|
|
city=a.city,
|
|
state=a.state,
|
|
pincode=a.pincode,
|
|
is_default=a.is_default
|
|
) for a in addresses
|
|
],
|
|
orders=[
|
|
CustomerOrderSummary(
|
|
order_id=o.order_id,
|
|
order_no=o.order_no,
|
|
final_amount=float(o.final_amount),
|
|
status=o.status,
|
|
created_at=o.created_at
|
|
) for o in orders
|
|
]
|
|
)
|