import json import logging from fastapi import APIRouter, Depends, HTTPException, Response from common.db.run import select_run_with_clinic from common.db.source import select_run_source_raw from common.deps import verify_api_key from common.utils import _with_scheme from integrations.llm.schemas.plan import PlanOutput from models.plan import PlanApiResponse from models.status import SourceType router = APIRouter(prefix="/api/plan", tags=["plan"], dependencies=[Depends(verify_api_key)]) logger = logging.getLogger(__name__) @router.get("/{run_id}", response_model=PlanApiResponse, response_model_by_alias=True) async def get_plan(run_id: str): logger.info("GET /api/plan/%s", run_id) row = await select_run_with_clinic(run_id) if row is None: raise HTTPException(status_code=404, detail="Run not found") if row["plan_data"] is None: return Response(status_code=204) data = json.loads(row["plan_data"]) if isinstance(row["plan_data"], str) else row["plan_data"] plan = PlanOutput(**data) # 강남언니에서 긁어온 이름이 있으면 우선 (hospital_baseinfo 의 정식 이름보다 강남언니가 더 광고용 표기). gu = await select_run_source_raw(run_id, SourceType.GANGNAM_UNNI) or {} clinic_name = gu.get("name") or row["hospital_name"] return PlanApiResponse( id=run_id, clinic_name=clinic_name, clinic_name_en=row["hospital_name_en"], created_at=str(row["created_at"]), target_url=_with_scheme(row["target_url"]), **plan.model_dump(), )