import { createClient } from '@supabase/supabase-js'; const supabaseUrl = 'https://jqklhhpyykzrktikjnmb.supabase.co'; const supabaseAnonKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Impxa2xoaHB5eWt6cmt0aWtqbm1iIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjgzODQ0MDgsImV4cCI6MjA4Mzk2MDQwOH0.QsPuBnyUtRPSavlqKj3IGR9c8juT02LY_hSi-j3c6M0'; const supabase = createClient(supabaseUrl, supabaseAnonKey); async function verifyNotifications() { console.log("--- INICIANDO VERIFICAÇÃO DE SISTEMA DE NOTIFICAÇÕES ---"); // 1. Verificar se existem tokens FCM registados const { data: usersWithTokens, error: err1 } = await supabase .from('profiles') .select('id, name, fcm_token') .not('fcm_token', 'is', null); if (err1) { console.error("Erro ao buscar perfis:", err1); } else { console.log(`Tokens encontrados: ${usersWithTokens.length}`); usersWithTokens.forEach(u => { console.log(` - Utilizador: ${u.name} | Token: ${u.fcm_token ? 'PRESENTE (' + u.fcm_token.substring(0, 20) + '...)' : 'AUSENTE'}`); }); } // 2. Verificar agendamentos pendentes com lembrete ativado const { data: appts, error: err2 } = await supabase .from('appointments') .select('id, date, status, reminder_minutes, reminder_sent') .eq('status', 'pendente') .eq('reminder_sent', false); if (err2) { console.error("Erro ao buscar agendamentos:", err2); } else { console.log(`Agendamentos pendentes para lembrete: ${appts.length}`); const now = new Date(); appts.forEach(a => { const apptDate = new Date(a.date.replace(' ', 'T')); const diffMin = (apptDate.getTime() - now.getTime()) / (1000 * 60); const status = diffMin <= (a.reminder_minutes || 1440) ? "PROXIMO (Deve disparar)" : "FUTURO"; console.log(` - ID: ${a.id} | Data: ${a.date} | Faltam: ${Math.round(diffMin)}min | Config: ${a.reminder_minutes}min | Estado: ${status}`); }); } // 3. Verificar se a tabela de notificações está a receber dados const { data: recentNotifs, error: err3 } = await supabase .from('notifications') .select('*') .order('created_at', { ascending: false }) .limit(5); if (err3) { console.error("Erro ao buscar log de notificações:", err3); } else { console.log(`Últimas 5 notificações registadas na BD:`); recentNotifs.forEach(n => { console.log(` - [${n.created_at}] Para User: ${n.user_id} | MSG: ${n.message} | Lida: ${n.read}`); }); } console.log("--- FIM DA VERIFICAÇÃO ---"); } verifyNotifications();