update configuration docker setup for data platform
This commit is contained in:
@@ -9,9 +9,9 @@ from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.v1.schemas import FeedCheckpointIn, FeedWaitingTimeIn
|
||||
from app.api.v1.schemas import FeedCheckpointIn, FeedWaitingTimeIn, PatientAppointmentIn
|
||||
from app.core.config import settings
|
||||
from app.db.models import RawOpdCheckpoint, RawWaitingTime
|
||||
from app.db.models import RawOpdCheckpoint, RawWaitingTime, PatientAppointment
|
||||
from app.security.dependencies import get_db, require_permission
|
||||
from app.utils.supabase_client import SupabaseAPIError, upsert_to_supabase_sync
|
||||
|
||||
@@ -21,6 +21,7 @@ router = APIRouter(prefix="/api/v1")
|
||||
|
||||
PERM_FEED_CHECKPOINT_WRITE = "feed.checkpoint:write"
|
||||
PERM_FEED_OLD_CHECKPOINT_WRITE = "feed.old-checkpoint:write"
|
||||
PERM_FEED_PATIENT_APPOINTMENT_WRITE = "feed.patient-appointment:write"
|
||||
|
||||
|
||||
def _to_tz(dt):
|
||||
@@ -220,3 +221,86 @@ def upsert_opd_checkpoint(
|
||||
"error": supabase_error,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@router.post("/feed/patient-appointment")
|
||||
def upsert_patient_appointment(
|
||||
payload: list[PatientAppointmentIn],
|
||||
_: Annotated[object, Depends(require_permission(PERM_FEED_PATIENT_APPOINTMENT_WRITE))],
|
||||
db: Annotated[Session, Depends(get_db)],
|
||||
):
|
||||
rows = []
|
||||
supabase_rows = []
|
||||
|
||||
for item in payload:
|
||||
# Prepare data for local database
|
||||
row = {
|
||||
"hn": item.hn,
|
||||
"txn": item.txn,
|
||||
"date": item.date, #+' 0:00:00'
|
||||
"time": _to_tz(datetime.strptime(item.date.strftime("%Y-%m-%d")+' '+item.time,'%Y-%m-%d %H:%M:%S')),
|
||||
"doctor_code": item.doctor_code,
|
||||
"period": item.period,
|
||||
"appointment_type": item.appointment_type,
|
||||
"updated_at": datetime.now(ZoneInfo(settings.TIMEZONE)),
|
||||
}
|
||||
rows.append(row)
|
||||
|
||||
# Prepare data for Supabase API (convert datetime to ISO string)
|
||||
supabase_row = {
|
||||
"hn": item.hn,
|
||||
"txn": item.txn,
|
||||
"date": _to_iso(_to_tz(datetime.strptime(item.date.strftime("%Y-%m-%d")+' 00:00:00+07:00','%Y-%m-%d %H:%M:%S%z'))),
|
||||
"time": _to_iso(_to_tz(datetime.strptime(item.date.strftime("%Y-%m-%d")+' '+item.time+'+07:00','%Y-%m-%d %H:%M:%S%z'))),
|
||||
"doctor_code": item.doctor_code,
|
||||
"period": item.period,
|
||||
"appointment_type": item.appointment_type,
|
||||
"updated_at": datetime.now(ZoneInfo(settings.TIMEZONE)).isoformat(),
|
||||
}
|
||||
supabase_rows.append(supabase_row)
|
||||
|
||||
# Insert/update to local database
|
||||
stmt = insert(PatientAppointment).values(rows)
|
||||
update_cols = {
|
||||
"txn": stmt.excluded.txn,
|
||||
"doctor_code": stmt.excluded.doctor_code,
|
||||
"period": stmt.excluded.period,
|
||||
"appointment_type": stmt.excluded.appointment_type,
|
||||
"updated_at": stmt.excluded.updated_at,
|
||||
}
|
||||
|
||||
stmt = stmt.on_conflict_do_update(
|
||||
index_elements=[PatientAppointment.hn, PatientAppointment.date, PatientAppointment.time],
|
||||
set_=update_cols,
|
||||
)
|
||||
result = db.execute(stmt)
|
||||
db.commit()
|
||||
|
||||
# Send data to Supabase via API call
|
||||
supabase_result = None
|
||||
supabase_error = None
|
||||
|
||||
try:
|
||||
logger.info(f"Sending {len(supabase_rows)} patient appointment records to Supabase API")
|
||||
supabase_result = upsert_to_supabase_sync(
|
||||
table="patient_appointment",
|
||||
data=supabase_rows,
|
||||
on_conflict="hn,date,time",
|
||||
)
|
||||
logger.info(f"Successfully sent patient appointment data to Supabase: {supabase_result.get('status_code')}")
|
||||
except SupabaseAPIError as e:
|
||||
logger.error(f"Failed to send patient appointment data to Supabase: {str(e)}")
|
||||
supabase_error = str(e)
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error sending patient appointment data to Supabase: {str(e)}")
|
||||
supabase_error = f"Unexpected error: {str(e)}"
|
||||
|
||||
return {
|
||||
"upserted": len(rows),
|
||||
"rowcount": result.rowcount,
|
||||
"supabase": {
|
||||
"success": supabase_result is not None,
|
||||
"result": supabase_result,
|
||||
"error": supabase_error,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, time, date
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
@@ -27,3 +27,13 @@ class FeedCheckpointIn(BaseModel):
|
||||
timestamp_out: datetime | None = None
|
||||
waiting_time: int | None = None
|
||||
bu: str | None = None
|
||||
|
||||
|
||||
class PatientAppointmentIn(BaseModel):
|
||||
hn: str
|
||||
txn: int | None = None
|
||||
date: date
|
||||
time: str
|
||||
doctor_code: str | None = None
|
||||
period: str | None = None
|
||||
appointment_type: str | None = None
|
||||
|
||||
Reference in New Issue
Block a user