77 lines
2.8 KiB
Python
77 lines
2.8 KiB
Python
import logging
|
|
import uuid6
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from common.deps import verify_api_key
|
|
from common.db import insert_hospital, fetchone
|
|
from common.utils import get_env
|
|
from integrations.firecrawl import FirecrawlClient
|
|
from models.clinic import ClinicCreate, ClinicCreateResponse, ClinicResponse, ClinicHistoryResponse, RunSummary
|
|
|
|
router = APIRouter(prefix="/api/clinics", tags=["clinics"], dependencies=[Depends(verify_api_key)])
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_REQUIRED_FIELDS = ["clinicName"]
|
|
_COLLECTED_FIELDS = ["clinicName", "clinicNameEn", "address", "phone", "slogan", "services", "doctors"]
|
|
|
|
|
|
@router.post("", status_code=status.HTTP_201_CREATED, response_model=ClinicCreateResponse)
|
|
async def create_clinic(body: ClinicCreate):
|
|
logger.info("POST /api/clinics url=%s", body.url)
|
|
info = await FirecrawlClient(get_env("FIRECRAWL_API_KEY")).fetch_clinic_info(body.url)
|
|
|
|
missing = [f for f in _COLLECTED_FIELDS if not (info or {}).get(f)]
|
|
required_missing = [f for f in _REQUIRED_FIELDS if f in missing]
|
|
if required_missing:
|
|
raise HTTPException(status_code=404, detail={"missing": missing})
|
|
|
|
hospital_id = str(uuid6.uuid7())
|
|
row = await insert_hospital(
|
|
hospital_id,
|
|
name=info["clinicName"],
|
|
name_en=info.get("clinicNameEn"),
|
|
road_address=info.get("address"),
|
|
url=body.url,
|
|
raw_data=info,
|
|
)
|
|
return ClinicCreateResponse(
|
|
id=hospital_id,
|
|
url=body.url,
|
|
name=info["clinicName"],
|
|
created_at=str(row["created_at"]),
|
|
)
|
|
|
|
|
|
@router.get("/{hospital_id}", response_model=ClinicResponse)
|
|
async def get_clinic(hospital_id: str):
|
|
logger.info("GET /api/clinics/%s", hospital_id)
|
|
row = await fetchone(
|
|
"SELECT hospital_id, hospital_name, hospital_name_en, road_address, url, status, raw_data, created_at, updated_at"
|
|
" FROM hospital_baseinfo WHERE hospital_id = %s",
|
|
(hospital_id,),
|
|
)
|
|
if not row:
|
|
raise HTTPException(status_code=404, detail="Clinic not found")
|
|
return ClinicResponse(**{**row, "created_at": str(row["created_at"]), "updated_at": str(row["updated_at"])})
|
|
|
|
|
|
# Not done
|
|
|
|
@router.get("/{id}/history", response_model=ClinicHistoryResponse)
|
|
async def get_clinic_history(id: str):
|
|
logger.info("GET /api/clinics/%s/history", id)
|
|
return ClinicHistoryResponse(
|
|
clinic_id=id,
|
|
runs=[
|
|
RunSummary(
|
|
run_id="22222222-2222-2222-2222-222222222222",
|
|
status="complete",
|
|
started_at="2026-04-20T09:00:00Z",
|
|
completed_at="2026-04-20T09:01:30Z",
|
|
overall_score=82,
|
|
)
|
|
],
|
|
metrics_timeseries={
|
|
"youtube_subscribers": [{"date": "2026-04-20", "value": 12345}]
|
|
},
|
|
)
|