ff7e9cb1d9
Adds the read-only API surface the upcoming Vue frontend needs to browse all
mirrored entities. Follows the existing customer-router pattern: Annotated
deps, Page[T] envelope, select+where+order_by, count() for total.
Endpoints (all GET):
* /api/departments -> list[DepartmentOut] (path-ordered, plain list)
* /api/staff -> Page[StaffOut] (department_id, is_on_job filters)
* /api/accounts -> Page[AccountOut] (channel filter; unified
wechat+whatsapp projection built in memory)
* /api/messages -> Page[MessageOut] (channel/owner/talker/room/
[start,end] filters; ordered timestamp asc)
* /api/groups -> Page[GroupOut] (channel/owner filters)
* /api/groups/{id}/members -> list[GroupMemberOut] (404 on unknown room)
* /api/calls -> Page[PhoneCallOut] (channel/user/time window;
ordered start_call_time desc)
* /api/sms -> Page[SmsOut] (user/time window; create_time desc)
* /api/sync/status -> {states, recent_runs} monitor payload
Schemas (Pydantic v2, from_attributes=True): MessageOut, GroupOut,
GroupMemberOut, PhoneCallOut, SmsOut, DepartmentOut, StaffOut, AccountOut
(unified across two tables), SyncStateOut, SyncRunOut, SyncStatusOut.
Only human-relevant fields per entity are exposed.
Tests: per-router TDD via httpx ASGITransport against an in-memory SQLite
override of get_session. A shared make_client factory in conftest.py builds
the seeded client, deduping the ~40-line fixture across the six new files.
41 new tests cover shape + every documented filter + pagination + ordering
(nulls-last verified on all desc/asc time columns).
Suite: 226 -> 267 passing. ruff check clean.
Co-Authored-By: Claude <noreply@anthropic.com>
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
"""GET /api/sync/status — sync monitor (cursors + recent runs).
|
|
|
|
Returns the current :class:`SyncState` cursors and the most recent N
|
|
:class:`SyncRun` audit rows (default 20). Read-only — purely for the operator
|
|
dashboard.
|
|
"""
|
|
|
|
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 SyncRunOut, SyncStateOut, SyncStatusOut
|
|
from app.db.models import SyncRun, SyncState
|
|
|
|
router = APIRouter(prefix="/api/sync", tags=["sync"])
|
|
|
|
SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
|
RecentLimitParam = Annotated[int, Query(ge=1, le=200)]
|
|
|
|
|
|
@router.get("/status")
|
|
async def sync_status(
|
|
session: SessionDep,
|
|
recent: RecentLimitParam = 20,
|
|
) -> SyncStatusOut:
|
|
"""Return sync cursors plus the most recent ``recent`` runs."""
|
|
state_rows = (
|
|
await session.execute(
|
|
sa.select(SyncState).order_by(
|
|
SyncState.entity_type.asc(),
|
|
sa.nullslast(SyncState.channel.asc()),
|
|
sa.nullslast(SyncState.scope_id.asc()),
|
|
)
|
|
)
|
|
).scalars().all()
|
|
states = [SyncStateOut.model_validate(row) for row in state_rows]
|
|
|
|
run_rows = (
|
|
await session.execute(
|
|
sa.select(SyncRun)
|
|
.order_by(SyncRun.started_at.desc(), SyncRun.run_id.desc())
|
|
.limit(recent)
|
|
)
|
|
).scalars().all()
|
|
recent_runs = [SyncRunOut.model_validate(row) for row in run_rows]
|
|
|
|
return SyncStatusOut(states=states, recent_runs=recent_runs)
|