fix bug merge missing route and supabase-client
This commit is contained in:
@@ -45,6 +45,8 @@ def upsert_feed_checkpoint(
|
|||||||
db: Annotated[Session, Depends(get_db)],
|
db: Annotated[Session, Depends(get_db)],
|
||||||
):
|
):
|
||||||
rows = []
|
rows = []
|
||||||
|
supabase_rows = []
|
||||||
|
|
||||||
for item in payload:
|
for item in payload:
|
||||||
rows.append(
|
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)
|
stmt = insert(RawWaitingTime).values(rows)
|
||||||
update_cols = {
|
update_cols = {
|
||||||
"vn": stmt.excluded.vn,
|
"vn": stmt.excluded.vn,
|
||||||
@@ -82,7 +101,35 @@ def upsert_feed_checkpoint(
|
|||||||
result = db.execute(stmt)
|
result = db.execute(stmt)
|
||||||
db.commit()
|
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")
|
@router.post("/feed/old-checkpoint")
|
||||||
|
|||||||
1
03-apiservice/app/templates/.gitkeep
Normal file
1
03-apiservice/app/templates/.gitkeep
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# Keep templates directory
|
||||||
@@ -40,6 +40,7 @@ async def upsert_to_supabase(
|
|||||||
"apikey": settings.SUPABASE_API_KEY,
|
"apikey": settings.SUPABASE_API_KEY,
|
||||||
"Authorization": f"Bearer {settings.SUPABASE_API_KEY}",
|
"Authorization": f"Bearer {settings.SUPABASE_API_KEY}",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
"Content-Profile": "rawdata",
|
||||||
"Prefer": "resolution=merge-duplicates",
|
"Prefer": "resolution=merge-duplicates",
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +94,7 @@ def upsert_to_supabase_sync(
|
|||||||
"apikey": settings.SUPABASE_API_KEY,
|
"apikey": settings.SUPABASE_API_KEY,
|
||||||
"Authorization": f"Bearer {settings.SUPABASE_API_KEY}",
|
"Authorization": f"Bearer {settings.SUPABASE_API_KEY}",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
"Content-Profile": "rawdata",
|
||||||
"Prefer": "resolution=merge-duplicates",
|
"Prefer": "resolution=merge-duplicates",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user