fix bug merge missing route and supabase-client

This commit is contained in:
jigoong
2026-02-27 17:24:20 +07:00
parent c57755c09c
commit 550d926139
3 changed files with 51 additions and 1 deletions

View File

@@ -45,6 +45,8 @@ def upsert_feed_checkpoint(
db: Annotated[Session, Depends(get_db)],
):
rows = []
supabase_rows = []
for item in payload:
rows.append(
{
@@ -63,6 +65,23 @@ def upsert_feed_checkpoint(
}
)
supabase_row = {
"vn": item.vn,
"txn": item.txn,
"hn": item.hn,
"name": item.name,
"doctor_code": item.doctor_code,
"doctor_name": item.doctor_name,
"location_code": item.location_code,
"location_name": item.location_name,
"step_name": item.step_name,
"time": _to_tz(item.time).isoformat(),
"updated_at": datetime.now(ZoneInfo(settings.TIMEZONE)).isoformat(),
}
# if item.id is None:
# del supabase_row[-1]["id"]
supabase_rows.append(supabase_row)
stmt = insert(RawWaitingTime).values(rows)
update_cols = {
"vn": stmt.excluded.vn,
@@ -82,7 +101,35 @@ def upsert_feed_checkpoint(
result = db.execute(stmt)
db.commit()
return {"upserted": len(rows), "rowcount": result.rowcount}
# Send data to Supabase via API call
supabase_result = None
supabase_error = None
try:
logger.info(f"Sending {len(supabase_rows)} records to Supabase API")
supabase_result = upsert_to_supabase_sync(
table="raw_waiting_time",
data=supabase_rows
)
logger.info(f"Successfully sent data to Supabase: {supabase_result.get('status_code')}")
except SupabaseAPIError as e:
logger.error(f"Failed to send data to Supabase: {str(e)}")
supabase_error = str(e)
except Exception as e:
logger.error(f"Unexpected error sending 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,
},
}
#return {"upserted": len(rows), "rowcount": result.rowcount}
@router.post("/feed/old-checkpoint")

View File

@@ -0,0 +1 @@
# Keep templates directory

View File

@@ -40,6 +40,7 @@ async def upsert_to_supabase(
"apikey": settings.SUPABASE_API_KEY,
"Authorization": f"Bearer {settings.SUPABASE_API_KEY}",
"Content-Type": "application/json",
"Content-Profile": "rawdata",
"Prefer": "resolution=merge-duplicates",
}
@@ -93,6 +94,7 @@ def upsert_to_supabase_sync(
"apikey": settings.SUPABASE_API_KEY,
"Authorization": f"Bearer {settings.SUPABASE_API_KEY}",
"Content-Type": "application/json",
"Content-Profile": "rawdata",
"Prefer": "resolution=merge-duplicates",
}