// Script to manually close active sessions // Usage: node scripts/closeSessions.js const { createClient } = require('@supabase/supabase-js'); // Load environment variables require('dotenv').config(); // Supabase configuration const supabaseUrl = process.env.REACT_APP_SUPABASE_URL; const supabaseAnonKey = process.env.REACT_APP_SUPABASE_ANON_KEY; if (!supabaseUrl || !supabaseAnonKey) { console.error('Missing Supabase credentials. Please check your .env file.'); process.exit(1); } // Create Supabase client const supabase = createClient(supabaseUrl, supabaseAnonKey); async function closeActiveSessions() { try { console.log('Closing active sessions...'); // Get current timestamp at midnight const now = new Date(); const midnight = new Date(now.getFullYear(), now.getMonth(), now.getDate()); // Update all active sessions (where end_time is NULL) const { data, error } = await supabase .from('timers') .update({ end_time: midnight.toISOString(), }) .eq('end_time', null) .lt('start_time', midnight.toISOString()) .select(); if (error) { console.error('Error closing sessions:', error); return; } console.log(`Successfully closed ${data.length} active sessions (history preserved).`); } catch (error) { console.error('Error:', error); } } // Run the function closeActiveSessions();