Files
trade-assistant/backend/app/api/v1/customer.py
T
TradeMate Dev 23a31f7c00 feat: silent wechat login, marketing tab optimization, admin page foundation
- Add silent WeChat login for MP/browser environments
- Fix Python 3.6 compatibility (remove typing.Annotated usage)
- Marketing page: tab-based content generation with category support
- Translate page: add auto-detect language default
- Homepage: add TTS playback, announcement ticker, remove redundant quick-actions
- Fix FAB button overlap with custom tabbar on customers/quotation pages
- Make openai/anthropic imports lazy for Python 3.6 compat
2026-05-14 00:30:48 +08:00

192 lines
5.7 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, Query, UploadFile, File, Response
from sqlalchemy.ext.asyncio import AsyncSession
from typing import Optional, List
from app.database import get_db
from app.services.customer import CustomerService
from app.services.customer_health import CustomerHealthService
from app.services.import_service import import_service
from app.services import export
from app.core.security import decode_token
from app.api.v1.deps import get_current_user_id
router = APIRouter()
@router.get("")
async def list_customers(
status: Optional[str] = None,
page: int = Query(1, ge=1),
size: int = Query(20, ge=1, le=100),
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
service = CustomerService(db)
return await service.list_customers(user_id, status, page, size)
@router.get("/silent")
async def get_silent(
days: int = Query(3, ge=1),
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
service = CustomerService(db)
customers = await service.get_silent_customers(user_id, days)
return {
"customers": customers,
"count": len(customers),
"silence_days": days,
}
@router.get("/{customer_id}")
async def get_customer(
customer_id: str,
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
service = CustomerService(db)
customer = await service.get_customer(user_id, customer_id)
if not customer:
raise HTTPException(status_code=404, detail="Customer not found")
return customer
@router.post("")
async def create_customer(
data: dict,
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
service = CustomerService(db)
customer = await service.create_customer(user_id, data)
return customer
@router.patch("/{customer_id}")
async def update_customer(
customer_id: str,
data: dict,
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
service = CustomerService(db)
customer = await service.update_customer(user_id, customer_id, data)
if not customer:
raise HTTPException(status_code=404, detail="Customer not found")
return customer
@router.delete("/{customer_id}")
async def delete_customer(
customer_id: str,
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
service = CustomerService(db)
deleted = await service.delete_customer(user_id, customer_id)
if not deleted:
raise HTTPException(status_code=404, detail="Customer not found")
return {"message": "Customer deleted"}
@router.post("/import")
async def import_customers(
file: UploadFile = File(...),
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
from app.workers.tasks import process_customer_import
content = await file.read()
filename = file.filename or ""
if filename.endswith(".xlsx"):
records, parse_errors = import_service.parse_xlsx(content)
elif filename.endswith(".csv"):
records, parse_errors = import_service.parse_csv(content)
else:
raise HTTPException(status_code=400, detail="Unsupported file format. Use .xlsx or .csv")
if parse_errors and not records:
raise HTTPException(status_code=400, detail=f"Parse failed: {'; '.join(parse_errors)}")
valid, validation_errors = import_service.validate_records(records)
all_errors = parse_errors + validation_errors
imported_count = 0
for record in valid:
try:
svc = CustomerService(db)
await svc.create_customer(user_id, record)
imported_count += 1
except Exception as e:
all_errors.append(f"Import failed for {record.get('name', 'unknown')}: {str(e)}")
return {
"imported": imported_count,
"total": len(records),
"errors": all_errors,
"filename": filename,
}
@router.get("/export/csv")
async def export_customers(
status: Optional[str] = None,
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
service = CustomerService(db)
result = await service.list_customers(user_id, status, 1, 9999)
items = result.get("items", [])
csv_bytes = export.export_customers_csv(items)
return Response(
content=csv_bytes,
media_type="text/csv",
headers={"Content-Disposition": "attachment; filename=customers.csv"},
)
@router.get("/health-overview")
async def get_health_overview(
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
service = CustomerHealthService(db)
return await service.get_health_overview(user_id)
@router.get("/health-scores")
async def get_all_health_scores(
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
service = CustomerHealthService(db)
return {"items": await service.get_all_health_scores(user_id)}
@router.get("/{customer_id}/health")
async def get_customer_health(
customer_id: str,
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
service = CustomerHealthService(db)
health = await service.get_customer_health(user_id, customer_id)
if not health:
raise HTTPException(status_code=404, detail="Customer not found")
return health
@router.get("/{customer_id}/conversation")
async def get_conversation(
customer_id: str,
page: int = Query(1, ge=1),
size: int = Query(50, ge=1, le=200),
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
service = CustomerService(db)
return await service.get_conversation(user_id, customer_id, page, size)