From 550d926139dd24740ff545b3c8bca46ab345ff46 Mon Sep 17 00:00:00 2001 From: jigoong Date: Fri, 27 Feb 2026 17:24:20 +0700 Subject: [PATCH] fix bug merge missing route and supabase-client --- 03-apiservice/app/api/v1/routes.py | 49 +++++++++++++++++++++- 03-apiservice/app/templates/.gitkeep | 1 + 03-apiservice/app/utils/supabase_client.py | 2 + 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 03-apiservice/app/templates/.gitkeep diff --git a/03-apiservice/app/api/v1/routes.py b/03-apiservice/app/api/v1/routes.py index d1a3021..bbda5ad 100644 --- a/03-apiservice/app/api/v1/routes.py +++ b/03-apiservice/app/api/v1/routes.py @@ -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") diff --git a/03-apiservice/app/templates/.gitkeep b/03-apiservice/app/templates/.gitkeep new file mode 100644 index 0000000..b292112 --- /dev/null +++ b/03-apiservice/app/templates/.gitkeep @@ -0,0 +1 @@ +# Keep templates directory diff --git a/03-apiservice/app/utils/supabase_client.py b/03-apiservice/app/utils/supabase_client.py index 92bad26..7f69658 100644 --- a/03-apiservice/app/utils/supabase_client.py +++ b/03-apiservice/app/utils/supabase_client.py @@ -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", }