feat: implement product search, media library, and vendor customers
- Add full-text product search in ProductService.search_products() searching titles, descriptions, SKUs, brands, and GTINs - Implement complete vendor media library with file uploads, thumbnails, folders, and product associations - Implement vendor customers API with listing, details, orders, statistics, and status management - Add shop search results UI with pagination and add-to-cart - Add vendor media library UI with drag-drop upload and grid view - Add database migration for media_files and product_media tables - Update TODO file with current launch status (~95% complete) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
171
app/api/v1/vendor/customers.py
vendored
171
app/api/v1/vendor/customers.py
vendored
@@ -8,17 +8,20 @@ The get_current_vendor_api dependency guarantees token_vendor_id is present.
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi import APIRouter, Depends, HTTPException, 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.vendor_service import vendor_service
|
||||
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,
|
||||
CustomerMessageResponse,
|
||||
CustomerOrdersResponse,
|
||||
CustomerResponse,
|
||||
CustomerStatisticsResponse,
|
||||
CustomerUpdate,
|
||||
VendorCustomerListResponse,
|
||||
@@ -40,19 +43,25 @@ def get_vendor_customers(
|
||||
"""
|
||||
Get all customers for this vendor.
|
||||
|
||||
TODO: Implement in Slice 4
|
||||
- Query customers filtered by vendor_id
|
||||
- Support search by name/email
|
||||
- Support filtering by active status
|
||||
- Return paginated results
|
||||
"""
|
||||
vendor = vendor_service.get_vendor_by_id(db, current_user.token_vendor_id) # noqa: F841
|
||||
return VendorCustomerListResponse(
|
||||
customers=[],
|
||||
total=0,
|
||||
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,
|
||||
message="Customer management coming in Slice 4",
|
||||
)
|
||||
|
||||
|
||||
@@ -65,34 +74,98 @@ def get_customer_details(
|
||||
"""
|
||||
Get detailed customer information.
|
||||
|
||||
TODO: Implement in Slice 4
|
||||
- Get customer by ID
|
||||
- Verify customer belongs to vendor
|
||||
- Include order history
|
||||
- Include total spent, etc.
|
||||
- Include order statistics
|
||||
"""
|
||||
vendor = vendor_service.get_vendor_by_id(db, current_user.token_vendor_id) # noqa: F841
|
||||
return CustomerDetailResponse(message="Customer details coming in Slice 4")
|
||||
try:
|
||||
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,
|
||||
)
|
||||
|
||||
except CustomerNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Customer not found")
|
||||
|
||||
|
||||
@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: User = Depends(get_current_vendor_api),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
Get order history for a specific customer.
|
||||
|
||||
TODO: Implement in Slice 5
|
||||
- Get all orders for customer
|
||||
- Filter by vendor_id
|
||||
- Return order details
|
||||
"""
|
||||
vendor = vendor_service.get_vendor_by_id(db, current_user.token_vendor_id) # noqa: F841
|
||||
return CustomerOrdersResponse(
|
||||
orders=[], message="Customer orders coming in Slice 5"
|
||||
)
|
||||
try:
|
||||
# Verify customer belongs to vendor
|
||||
customer_service.get_customer(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
customer_id=customer_id,
|
||||
)
|
||||
|
||||
# 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")
|
||||
|
||||
|
||||
@router.put("/{customer_id}", response_model=CustomerMessageResponse)
|
||||
@@ -105,13 +178,26 @@ def update_customer(
|
||||
"""
|
||||
Update customer information.
|
||||
|
||||
TODO: Implement in Slice 4
|
||||
- Update customer details
|
||||
- Verify customer belongs to vendor
|
||||
- Update customer preferences
|
||||
"""
|
||||
vendor = vendor_service.get_vendor_by_id(db, current_user.token_vendor_id) # noqa: F841
|
||||
return CustomerMessageResponse(message="Customer update coming in Slice 4")
|
||||
try:
|
||||
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")
|
||||
|
||||
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))
|
||||
|
||||
|
||||
@router.put("/{customer_id}/status", response_model=CustomerMessageResponse)
|
||||
@@ -123,13 +209,26 @@ def toggle_customer_status(
|
||||
"""
|
||||
Activate/deactivate customer account.
|
||||
|
||||
TODO: Implement in Slice 4
|
||||
- Toggle customer is_active status
|
||||
- Verify customer belongs to vendor
|
||||
- Log the change
|
||||
"""
|
||||
vendor = vendor_service.get_vendor_by_id(db, current_user.token_vendor_id) # noqa: F841
|
||||
return CustomerMessageResponse(message="Customer status toggle coming in Slice 4")
|
||||
try:
|
||||
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")
|
||||
|
||||
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))
|
||||
|
||||
|
||||
@router.get("/{customer_id}/stats", response_model=CustomerStatisticsResponse)
|
||||
@@ -141,17 +240,19 @@ def get_customer_statistics(
|
||||
"""
|
||||
Get customer statistics and metrics.
|
||||
|
||||
TODO: Implement in Slice 4
|
||||
- Total orders
|
||||
- Total spent
|
||||
- Average order value
|
||||
- Last order date
|
||||
"""
|
||||
vendor = vendor_service.get_vendor_by_id(db, current_user.token_vendor_id) # noqa: F841
|
||||
return CustomerStatisticsResponse(
|
||||
total_orders=0,
|
||||
total_spent=0.0,
|
||||
average_order_value=0.0,
|
||||
last_order_date=None,
|
||||
message="Customer statistics coming in Slice 4",
|
||||
)
|
||||
try:
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user