PMFlow/backend/app/api/ai_steward_api/routes_executive_briefing.py

67 lines
2.6 KiB
Python

from __future__ import annotations
from fastapi import Depends, Query
from fastapi.responses import JSONResponse
from ...services.auth.service import parse_role_values
from ...services.executive_briefing_service import ExecutiveBriefingService
from .schemas import ExecutiveBriefingConfigRequest
from .shared import AsyncSession, ErrorCode, User, error_response, get_current_user, get_db, get_tenant_id, router, success_response
def _manager_roles(user: User) -> set[str]:
return set(parse_role_values(getattr(user, "role", "")))
def _forbidden_response() -> JSONResponse:
return JSONResponse(status_code=403, content=error_response(ErrorCode.FORBIDDEN, "Manager permission required"))
@router.get("/executive-briefing")
async def get_executive_briefing(
mode: str = Query(default="snapshot", pattern="^(snapshot|live)$"),
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
if not _manager_roles(current_user).intersection({"OWNER", "REVIEWER"}):
return _forbidden_response()
try:
payload = await ExecutiveBriefingService(db, tenant_id=tenant_id).get_briefing(mode=mode)
except ValueError as exc:
return JSONResponse(status_code=400, content=error_response(400, str(exc)))
return success_response(payload)
@router.get("/executive-briefing/config")
async def get_executive_briefing_config(
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
if not _manager_roles(current_user).intersection({"OWNER", "REVIEWER"}):
return _forbidden_response()
payload = await ExecutiveBriefingService(db, tenant_id=tenant_id).get_config()
return success_response(payload)
@router.put("/executive-briefing/config")
async def update_executive_briefing_config(
request: ExecutiveBriefingConfigRequest,
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
if not _manager_roles(current_user).intersection({"OWNER", "REVIEWER"}):
return _forbidden_response()
service = ExecutiveBriefingService(db, tenant_id=tenant_id)
try:
payload = await service.update_config(payload=request.model_dump(), operator_user_id=int(current_user.id))
await db.commit()
except ValueError as exc:
await db.rollback()
return JSONResponse(status_code=422, content=error_response(ErrorCode.VALIDATION_ERROR, str(exc)))
return success_response(payload)