Files
yunke/yunke-server/app/api/routers/customer.py
T
浅念 1e0467b40f feat: customer list API
Minimal read-only FastAPI surface over the mirrored DB:
- GET /api/customers with pagination (page/page_size) and optional
  channel / owner_account_id / department_id filters, ordered by
  last_chat_time desc (nulls last) then friend_id.
- GET /api/health liveness probe.
- Pydantic v2 CustomerOut + generic Page[T] envelope.
- CORS configured permissively for the local frontend.
- Tests override get_session to an in-memory async SQLite session
  (StaticPool) and drive the app via httpx ASGITransport.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-01 12:10:03 +08:00

71 lines
2.5 KiB
Python

"""GET /api/customers — paginated, filterable customer list.
Read-only query over the mirrored ``customer`` table. Supports filtering by
``channel``, ``owner_account_id`` (direct columns) and ``department_id``
(resolved through ``customer.user_id`` -> ``staff.dept_id``). Results are
ordered by ``last_chat_time desc`` (nulls last) then ``friend_id`` so the
most-recently-active contacts surface first.
"""
from __future__ import annotations
from typing import Annotated
import sqlalchemy as sa
from fastapi import APIRouter, Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.deps import get_session
from app.api.schemas import CustomerOut, Page
from app.db.models import Customer, Staff
router = APIRouter(prefix="/api", tags=["customers"])
SessionDep = Annotated[AsyncSession, Depends(get_session)]
PageParam = Annotated[int, Query(ge=1)]
PageSizeParam = Annotated[int, Query(ge=1, le=100)]
@router.get("/customers")
async def list_customers(
session: SessionDep,
page: PageParam = 1,
page_size: PageSizeParam = 20,
channel: Annotated[str | None, Query()] = None,
department_id: Annotated[str | None, Query()] = None,
owner_account_id: Annotated[str | None, Query()] = None,
) -> Page[CustomerOut]:
"""List customers with optional filters and pagination."""
conditions = []
if channel is not None:
conditions.append(Customer.channel == channel)
if owner_account_id is not None:
conditions.append(Customer.owner_account_id == owner_account_id)
if department_id is not None:
# Customer has no dept column; resolve via the owning staff member.
dept_user_ids = sa.select(Staff.user_id).where(
Staff.dept_id == department_id
)
conditions.append(Customer.user_id.in_(dept_user_ids))
total_stmt = sa.select(sa.func.count()).select_from(Customer)
if conditions:
total_stmt = total_stmt.where(*conditions)
total = (await session.execute(total_stmt)).scalar_one()
list_stmt = sa.select(Customer)
if conditions:
list_stmt = list_stmt.where(*conditions)
list_stmt = (
list_stmt.order_by(
sa.nullslast(Customer.last_chat_time.desc()),
Customer.friend_id.asc(),
)
.limit(page_size)
.offset((page - 1) * page_size)
)
rows = (await session.execute(list_stmt)).scalars().all()
items = [CustomerOut.model_validate(row) for row in rows]
return Page(items=items, total=total, page=page, page_size=page_size)