Simplify calendar cleanup: handle in SQL function, remove HTTP dependency
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 <noreply@anthropic.com>
This commit is contained in:
@@ -20,6 +20,7 @@ DECLARE
|
|||||||
expired_order RECORD;
|
expired_order RECORD;
|
||||||
expired_session RECORD;
|
expired_session RECORD;
|
||||||
processed_count INTEGER := 0;
|
processed_count INTEGER := 0;
|
||||||
|
calendar_cleanup_count INTEGER := 0;
|
||||||
BEGIN
|
BEGIN
|
||||||
-- Log start
|
-- Log start
|
||||||
RAISE NOTICE '[CANCEL-EXPIRED] Starting check for expired consulting orders';
|
RAISE NOTICE '[CANCEL-EXPIRED] Starting check for expired consulting orders';
|
||||||
@@ -57,6 +58,16 @@ BEGIN
|
|||||||
DELETE FROM consulting_time_slots
|
DELETE FROM consulting_time_slots
|
||||||
WHERE session_id = expired_session.id;
|
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;
|
RAISE NOTICE '[CANCEL-EXPIRED] Cancelled session: %', expired_session.id;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
|
|
||||||
@@ -68,7 +79,8 @@ BEGIN
|
|||||||
RETURN jsonb_build_object(
|
RETURN jsonb_build_object(
|
||||||
'success', true,
|
'success', true,
|
||||||
'processed', processed_count,
|
'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;
|
END;
|
||||||
$$;
|
$$;
|
||||||
@@ -86,14 +98,18 @@ $$;
|
|||||||
-- Timeout: 30 seconds
|
-- Timeout: 30 seconds
|
||||||
-- Container: supabase-db (or supabase-rest if it has psql client)
|
-- 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
|
-- Due to Docker networking limitations between containers, we cannot
|
||||||
-- Command: curl -X POST http://supabase-edge-functions:8000/functions/v1/trigger-calendar-cleanup
|
-- automatically trigger the edge function from the scheduled task.
|
||||||
-- Alternative: wget -qO- --post-data='' http://supabase-edge-functions:8000/functions/v1/trigger-calendar-cleanup
|
-- The SQL function now handles cleanup of database references.
|
||||||
-- Frequency: */15 * * * *
|
-- To manually clean up Google Calendar events, trigger the edge function:
|
||||||
-- Timeout: 30 seconds
|
-- POST http://your-supabase-project.supabase.co/functions/v1/trigger-calendar-cleanup
|
||||||
-- Container: supabase-db (IMPORTANT: Must run on supabase-db service which has curl/wget)
|
|
||||||
|
|
||||||
-- ============================================
|
-- ============================================
|
||||||
-- Manual Testing
|
-- Manual Testing
|
||||||
|
|||||||
@@ -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;
|
|
||||||
Reference in New Issue
Block a user