api.py
3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException, Request
from sqlalchemy.orm import Session
from app.agent_core.im_service import IMService
from app.infrastructure.database import get_db
from app.schema.common import ApiResponse, success_response_payload, SuccessFlagData
from app.customer_info.schema import (
CustomerGroupPhoneResponse,
PhoneBindingSubmitRequest,
SendByPhoneRequest,
WeeklyReportTriggerResponse,
)
from app.customer_info.send_service import PhoneMessageSender, SendByPhoneError
from app.customer_info.service import CustomerInfoService
from app.customer_info.weekly_report_service import WeeklyReportService
router = APIRouter(prefix="/api/v1/customer-info", tags=["customer-info"])
def get_customer_info_service(db: Session = Depends(get_db)) -> CustomerInfoService:
return CustomerInfoService(db)
def get_weekly_report_service(db: Session = Depends(get_db)) -> WeeklyReportService:
return WeeklyReportService(db)
@router.post("/phone", response_model=ApiResponse[SuccessFlagData])
def submit_phone(
req: PhoneBindingSubmitRequest,
service: CustomerInfoService = Depends(get_customer_info_service),
):
"""用户提交手机号绑定。"""
record = service.bind_phone(
group_id=req.group_id, phone=req.phone
)
if record is None:
raise HTTPException(status_code=404, detail="未找到匹配的客户群组记录")
return success_response_payload(200, SuccessFlagData(success=True))
@router.get("/phone", response_model=ApiResponse[CustomerGroupPhoneResponse])
def get_phone(
group_id: str,
service: CustomerInfoService = Depends(get_customer_info_service),
):
"""查询群手机号绑定信息。"""
record = service.get_phone(group_id)
if record is None:
raise HTTPException(status_code=404, detail="未找到匹配的客户群组记录")
return success_response_payload(200, record)
def _get_im_service(request: Request) -> IMService:
im = getattr(request.app.state, "im_service", None)
if im is None:
raise HTTPException(status_code=503, detail="IM 服务未就绪")
return im
@router.post("/send-by-phone")
async def send_by_phone(
req: SendByPhoneRequest,
request: Request,
db: Session = Depends(get_db),
):
"""外部服务按手机号查找群组并发送消息。"""
im_service = _get_im_service(request)
sender = PhoneMessageSender(db=db, im_service=im_service)
try:
result = await sender.send_by_phone(phone=req.phone, content=req.content, push_type=req.push_type)
except SendByPhoneError as e:
raise HTTPException(status_code=400, detail=str(e))
if result["success"]:
return success_response_payload(200, result)
raise HTTPException(status_code=502, detail=result["message"])
@router.post("/weekly-report/trigger", response_model=ApiResponse[WeeklyReportTriggerResponse])
def trigger_weekly_report(
service: WeeklyReportService = Depends(get_weekly_report_service),
):
"""手动触发客户群周报推送(测试用)。"""
result = service.run_and_send()
if not result["send_result"]["success"]:
raise HTTPException(status_code=502, detail=result["send_result"]["message"])
return success_response_payload(200, result)