2026-09-11 15:29:20 +08:00
|
|
|
"""Recommendation generation and reviewed publication endpoints."""
|
|
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, Header, Path
|
|
|
|
|
|
|
|
|
|
from app.api.dependencies.auth import build_request_context
|
|
|
|
|
from app.api.dependencies.rate_limit import enforce_rate_limit
|
|
|
|
|
from app.core.contracts import RequestContext
|
|
|
|
|
from app.core.product_recommendation_contracts import ProductRecommendationQuery
|
2026-09-11 20:27:56 +08:00
|
|
|
from app.service.advisor_rollout_service import enforce_advisor_rollout
|
2026-09-11 15:29:20 +08:00
|
|
|
from app.service.product_recommendation_service import ProductRecommendationService
|
|
|
|
|
|
|
|
|
|
advisor_router = APIRouter(
|
|
|
|
|
prefix="/api/v1/advisor",
|
|
|
|
|
tags=["advisor-recommendations"],
|
2026-09-11 20:27:56 +08:00
|
|
|
dependencies=[Depends(enforce_rate_limit), Depends(enforce_advisor_rollout)],
|
2026-09-11 15:29:20 +08:00
|
|
|
)
|
|
|
|
|
admin_router = APIRouter(
|
|
|
|
|
prefix="/api/v1/admin",
|
|
|
|
|
tags=["platform-admin"],
|
|
|
|
|
dependencies=[Depends(enforce_rate_limit)],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@advisor_router.post("/recommendations")
|
|
|
|
|
async def generate_recommendation(
|
|
|
|
|
payload: ProductRecommendationQuery,
|
|
|
|
|
context: RequestContext = Depends(build_request_context), # noqa: B008
|
|
|
|
|
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
|
|
|
|
) -> dict[str, object]:
|
2026-09-11 16:42:31 +08:00
|
|
|
return await ProductRecommendationService(enforce_profile_governance=True).generate(
|
|
|
|
|
payload, context, key
|
|
|
|
|
)
|
2026-09-11 15:29:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@advisor_router.get("/recommendations/published")
|
|
|
|
|
async def published_recommendations(
|
|
|
|
|
context: RequestContext = Depends(build_request_context), # noqa: B008
|
|
|
|
|
) -> dict[str, object]:
|
|
|
|
|
return await ProductRecommendationService().published(context)
|
|
|
|
|
|
|
|
|
|
|
2026-09-14 00:17:46 +08:00
|
|
|
@admin_router.get(
|
|
|
|
|
"/advisor/pending-contents",
|
|
|
|
|
dependencies=[Depends(enforce_advisor_rollout)],
|
|
|
|
|
)
|
|
|
|
|
async def pending_advisor_contents(
|
|
|
|
|
context: RequestContext = Depends(build_request_context), # noqa: B008
|
|
|
|
|
) -> dict[str, object]:
|
|
|
|
|
"""待审核的投顾内容(推荐方案 + 投资方案书)。编号 `A047`。
|
|
|
|
|
|
|
|
|
|
补这个入口的原因:审核/发布端点都要求先拿到 `content_id`,而此前**没有**任何
|
|
|
|
|
端点能列出待审内容,管理员拿不到 id ⇒ 审核链路不可达。返回体里的
|
|
|
|
|
`content_type` 用于前端区分两类内容。
|
|
|
|
|
"""
|
|
|
|
|
return await ProductRecommendationService().pending_reviews(context)
|
|
|
|
|
|
|
|
|
|
|
2026-09-11 20:27:56 +08:00
|
|
|
@admin_router.post(
|
|
|
|
|
"/advisor/recommendations/{content_id}/reviews",
|
|
|
|
|
dependencies=[Depends(enforce_advisor_rollout)],
|
|
|
|
|
)
|
2026-09-11 15:29:20 +08:00
|
|
|
async def review_recommendation(
|
|
|
|
|
payload: dict[str, Any],
|
|
|
|
|
content_id: int = Path(gt=0),
|
|
|
|
|
context: RequestContext = Depends(build_request_context), # noqa: B008
|
|
|
|
|
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
|
|
|
|
) -> dict[str, object]:
|
|
|
|
|
decision = payload.get("decision")
|
|
|
|
|
if decision not in {"approved", "rejected"}:
|
|
|
|
|
from app.core.errors import ValidationAgentError
|
|
|
|
|
|
|
|
|
|
raise ValidationAgentError("decision 必须为 approved 或 rejected")
|
|
|
|
|
comment = payload.get("comment", "")
|
|
|
|
|
if not isinstance(comment, str):
|
|
|
|
|
raise ValueError("comment must be a string")
|
|
|
|
|
return await ProductRecommendationService().review(content_id, decision, comment, context, key)
|
|
|
|
|
|
|
|
|
|
|
2026-09-11 20:27:56 +08:00
|
|
|
@admin_router.post(
|
|
|
|
|
"/advisor/recommendations/{content_id}/publications",
|
|
|
|
|
dependencies=[Depends(enforce_advisor_rollout)],
|
|
|
|
|
)
|
2026-09-11 15:29:20 +08:00
|
|
|
async def publish_recommendation(
|
|
|
|
|
content_id: int = Path(gt=0),
|
|
|
|
|
context: RequestContext = Depends(build_request_context), # noqa: B008
|
|
|
|
|
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
|
|
|
|
) -> dict[str, object]:
|
|
|
|
|
return await ProductRecommendationService().publish(content_id, context, key)
|