ab5a918a3b
- /api/calls 加 scope=friend|group(@chatroom后缀判群,与isRoom实测等价120=120),CallList去掉「全部」tab改服务端查 - 对话发现:全部customer pair ∪ 语音pair(weChatId,talker),未扫过的排最前——修复纯语音关系客户聊天记录永远空(35%语音客户pair从未扫过) - voice-calls改浏览器直发(isRoom=0/1双趟+WA callSource=4,拦截方式已两次失效) - 断连恢复重试30×2min(云客约3h一次断连,已实战扛过多次) - WA messages upsert改整列表直调(_upsert_all逐条传dict被迭代成key字符串→TypeError) - 405测试绿含scope新增3个 Co-Authored-By: Claude <noreply@anthropic.com>
78 lines
2.9 KiB
Python
78 lines
2.9 KiB
Python
"""GET /api/calls — phone-call / voice-call log list.
|
|
|
|
Read-only query over the mirrored ``phone_call`` table. ``channel`` here is the
|
|
**call-type** in {``phone_call``, ``voice_call``} (calls are not tied to a
|
|
messaging channel). Filters: ``channel``, ``user_id``, and a ms-epoch
|
|
``[start, end]`` window compared against ``start_call_time`` (inclusive).
|
|
Ordered by ``start_call_time desc`` (nulls last) — most recent calls first.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Annotated, Literal
|
|
|
|
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 Page, PhoneCallOut
|
|
from app.db.models import PhoneCall
|
|
|
|
router = APIRouter(prefix="/api", tags=["calls"])
|
|
|
|
SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
|
PageParam = Annotated[int, Query(ge=1)]
|
|
PageSizeParam = Annotated[int, Query(ge=1, le=100)]
|
|
|
|
|
|
@router.get("/calls")
|
|
async def list_calls(
|
|
session: SessionDep,
|
|
page: PageParam = 1,
|
|
page_size: PageSizeParam = 20,
|
|
channel: Annotated[str | None, Query()] = None,
|
|
user_id: Annotated[str | None, Query()] = None,
|
|
start: Annotated[int | None, Query(ge=0)] = None,
|
|
end: Annotated[int | None, Query(ge=0)] = None,
|
|
scope: Annotated[Literal["friend", "group"] | None, Query()] = None,
|
|
) -> Page[PhoneCallOut]:
|
|
"""List phone/voice calls with optional filters, newest first.
|
|
|
|
``scope`` filters voice calls by peer kind: ``group`` keeps only group
|
|
calls (``call_number`` ends ``@chatroom``), ``friend`` excludes them —
|
|
verified exactly equivalent to the CRM's ``isRoom`` flag (2026-08-18).
|
|
"""
|
|
conditions = []
|
|
if channel is not None:
|
|
conditions.append(PhoneCall.channel == channel)
|
|
if user_id is not None:
|
|
conditions.append(PhoneCall.user_id == user_id)
|
|
if scope is not None:
|
|
is_room = PhoneCall.call_number.endswith("@chatroom")
|
|
conditions.append(is_room if scope == "group" else ~is_room)
|
|
if start is not None:
|
|
conditions.append(PhoneCall.start_call_time >= start)
|
|
if end is not None:
|
|
conditions.append(PhoneCall.start_call_time <= end)
|
|
|
|
total_stmt = sa.select(sa.func.count()).select_from(PhoneCall)
|
|
if conditions:
|
|
total_stmt = total_stmt.where(*conditions)
|
|
total = (await session.execute(total_stmt)).scalar_one()
|
|
|
|
list_stmt = sa.select(PhoneCall)
|
|
if conditions:
|
|
list_stmt = list_stmt.where(*conditions)
|
|
list_stmt = (
|
|
list_stmt.order_by(
|
|
sa.nullslast(PhoneCall.start_call_time.desc()),
|
|
PhoneCall.call_id.asc(),
|
|
)
|
|
.limit(page_size)
|
|
.offset((page - 1) * page_size)
|
|
)
|
|
rows = (await session.execute(list_stmt)).scalars().all()
|
|
items = [PhoneCallOut.model_validate(row) for row in rows]
|
|
return Page(items=items, total=total, page=page, page_size=page_size)
|