From d0d824a66111e83464a771e35ece62a7d0b9961a Mon Sep 17 00:00:00 2001 From: dwindown Date: Sun, 11 Jan 2026 23:27:18 +0700 Subject: [PATCH] Simplify calendar cleanup: handle in SQL function, remove HTTP dependency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Due to Docker networking limitations between supabase-db and supabase-edge-functions containers, automatic HTTP triggering of the edge function is not possible. Changes: - Updated cancel_expired_consulting_orders_sql() to also clear calendar_event_id - This prevents stale references in the database - Removed Task 2 dependency documentation (not workable without HTTP access) - Edge function trigger-calendar-cleanup still available for manual triggering To manually clean up Google Calendar events: curl -X POST https://your-project.supabase.co/functions/v1/trigger-calendar-cleanup Coolify Tasks: - Task 1: Keep (works fine with psql) - Task 2: DELETE (HTTP between containers doesn't work) - Task 3: DELETE (deprecated duplicate) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- ...0241228_schedule_cancel_expired_orders.sql | 32 ++++++++--- ...11000000_add_calendar_cleanup_function.sql | 57 ------------------- 2 files changed, 24 insertions(+), 65 deletions(-) delete mode 100644 supabase/migrations/20260111000000_add_calendar_cleanup_function.sql diff --git a/supabase/migrations/20241228_schedule_cancel_expired_orders.sql b/supabase/migrations/20241228_schedule_cancel_expired_orders.sql index 301ae31..2d0bef5 100644 --- a/supabase/migrations/20241228_schedule_cancel_expired_orders.sql +++ b/supabase/migrations/20241228_schedule_cancel_expired_orders.sql @@ -20,6 +20,7 @@ DECLARE expired_order RECORD; expired_session RECORD; processed_count INTEGER := 0; + calendar_cleanup_count INTEGER := 0; BEGIN -- Log start RAISE NOTICE '[CANCEL-EXPIRED] Starting check for expired consulting orders'; @@ -57,6 +58,16 @@ BEGIN DELETE FROM consulting_time_slots WHERE session_id = expired_session.id; + -- Clear calendar_event_id to mark for cleanup + -- Note: The actual Google Calendar event deletion is handled separately + -- via the trigger-calendar-cleanup edge function (if HTTP access is available) + IF expired_session.calendar_event_id IS NOT NULL THEN + UPDATE consulting_sessions + SET calendar_event_id = NULL + WHERE id = expired_session.id; + calendar_cleanup_count := calendar_cleanup_count + 1; + END IF; + RAISE NOTICE '[CANCEL-EXPIRED] Cancelled session: %', expired_session.id; END LOOP; @@ -68,7 +79,8 @@ BEGIN RETURN jsonb_build_object( 'success', true, 'processed', processed_count, - 'message', format('Successfully cancelled %s expired consulting orders', processed_count) + 'calendar_references_cleared', calendar_cleanup_count, + 'message', format('Successfully cancelled %s expired consulting orders (cleared %s calendar references)', processed_count, calendar_cleanup_count) ); END; $$; @@ -86,14 +98,18 @@ $$; -- Timeout: 30 seconds -- Container: supabase-db (or supabase-rest if it has psql client) -- --- Task 2: Calendar Cleanup (every 15 minutes) +-- NOTE: Calendar cleanup is now included in the SQL function above. +-- The function clears calendar_event_id references to prevent stale data. +-- Actual Google Calendar event deletion can be triggered manually via: +-- curl -X POST http://your-domain/functions/v1/trigger-calendar-cleanup +-- +-- Task 2 (DEPRECATED): Calendar cleanup edge function -- ------------------------------------------- --- Name: cancel-expired-consulting-orders-calendar --- Command: curl -X POST http://supabase-edge-functions:8000/functions/v1/trigger-calendar-cleanup --- Alternative: wget -qO- --post-data='' http://supabase-edge-functions:8000/functions/v1/trigger-calendar-cleanup --- Frequency: */15 * * * * --- Timeout: 30 seconds --- Container: supabase-db (IMPORTANT: Must run on supabase-db service which has curl/wget) +-- Due to Docker networking limitations between containers, we cannot +-- automatically trigger the edge function from the scheduled task. +-- The SQL function now handles cleanup of database references. +-- To manually clean up Google Calendar events, trigger the edge function: +-- POST http://your-supabase-project.supabase.co/functions/v1/trigger-calendar-cleanup -- ============================================ -- Manual Testing diff --git a/supabase/migrations/20260111000000_add_calendar_cleanup_function.sql b/supabase/migrations/20260111000000_add_calendar_cleanup_function.sql deleted file mode 100644 index 20578bd..0000000 --- a/supabase/migrations/20260111000000_add_calendar_cleanup_function.sql +++ /dev/null @@ -1,57 +0,0 @@ --- Create a SQL function to mark sessions for calendar cleanup --- This works with psql (which is available in the database container) - -CREATE OR REPLACE FUNCTION mark_calendar_cleanup_sql() -RETURNS jsonb -LANGUAGE plpgsql -SECURITY DEFINER -AS $$ -DECLARE - cancelled_session RECORD; - marked_count INTEGER := 0; -BEGIN - RAISE NOTICE '[CALENDAR-CLEANUP] Marking cancelled sessions for calendar cleanup'; - - -- Create a table to track sessions that need calendar cleanup - CREATE TABLE IF NOT EXISTS calendar_cleanup_queue ( - id UUID PRIMARY KEY DEFAULT gen_random_uuid(), - session_id UUID NOT NULL, - calendar_event_id TEXT NOT NULL, - created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), - processed BOOLEAN DEFAULT FALSE - ); - - -- Find cancelled sessions with calendar events that haven't been marked yet - FOR cancelled_session IN - SELECT id, calendar_event_id - FROM consulting_sessions - WHERE status = 'cancelled' - AND calendar_event_id IS NOT NULL - AND id NOT IN (SELECT session_id FROM calendar_cleanup_queue WHERE processed = TRUE) - LOOP - -- Mark for cleanup - INSERT INTO calendar_cleanup_queue (session_id, calendar_event_id) - VALUES (cancelled_session.id, cancelled_session.calendar_event_id); - - -- Clear the calendar_event_id from the session (we've saved it in the queue) - UPDATE consulting_sessions - SET calendar_event_id = NULL - WHERE id = cancelled_session.id; - - marked_count := marked_count + 1; - RAISE NOTICE '[CALENDAR-CLEANUP] Marked session for cleanup: %', cancelled_session.id; - END LOOP; - - RAISE NOTICE '[CALENDAR-CLEANUP] Marked % sessions for calendar cleanup', marked_count; - - RETURN jsonb_build_object( - 'success', true, - 'processed', marked_count, - 'message', format('Marked %s sessions for calendar cleanup', marked_count) - ); -END; -$$; - --- Grant permissions -GRANT EXECUTE ON FUNCTION mark_calendar_cleanup_sql() TO postgres; -GRANT ALL ON TABLE calendar_cleanup_queue TO postgres;