import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4'; const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', }; Deno.serve(async (req) => { if (req.method === 'OPTIONS') { return new Response(null, { headers: corsHeaders }); } try { const supabaseUrl = Deno.env.get('SUPABASE_URL')!; const supabaseKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!; const supabase = createClient(supabaseUrl, supabaseKey); console.log('Starting data retention cleanup...'); // Call the master cleanup function const { data, error } = await supabase.rpc('run_data_retention_cleanup'); if (error) { console.error('Error running data retention cleanup:', error); throw error; } console.log('Data retention cleanup completed:', data); return new Response( JSON.stringify({ success: true, cleanup_results: data.cleanup_results, timestamp: data.timestamp, }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' } } ); } catch (error) { console.error('Error in data-retention-cleanup function:', error); return new Response( JSON.stringify({ error: error.message }), { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' }, } ); } });