finalized reminder notification system migrations

This commit is contained in:
2026-03-21 12:58:38 +08:00
parent b2c3202317
commit 7d9096963a
5 changed files with 530 additions and 27 deletions
@@ -10,9 +10,11 @@ const corsHeaders = {
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}
const supabase = createClient(Deno.env.get('SUPABASE_URL')!, Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!)
const SUPABASE_URL = Deno.env.get('SUPABASE_URL')!
const SERVICE_ROLE_KEY = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
const supabase = createClient(SUPABASE_URL, SERVICE_ROLE_KEY)
const SEND_FCM_URL = Deno.env.get('SEND_FCM_URL')
|| `${Deno.env.get('SUPABASE_URL')!}/functions/v1/send_fcm`
|| `${SUPABASE_URL}/functions/v1/send_fcm`
const BATCH_SIZE = Number(Deno.env.get('PROCESSOR_BATCH_SIZE') || '50')
// deterministic UUIDv5-like from a name using SHA-1
@@ -31,6 +33,18 @@ async function uuidFromName(name: string): Promise<string> {
}
async function processBatch() {
// Step 1: Enqueue any due notifications via RPC.
// This makes the edge function self-sufficient — a single trigger
// (pg_cron, external cron, or manual curl) handles both enqueue + process.
console.log('Calling enqueue_all_notifications RPC...')
const { error: enqueueErr, data: enqueueData } = await supabase.rpc('enqueue_all_notifications')
if (enqueueErr) {
console.error('❌ enqueue_all_notifications RPC FAILED:', JSON.stringify(enqueueErr))
} else {
console.log('✓ enqueue_all_notifications completed successfully', enqueueData)
}
// Step 2: Process enqueued rows
const nowIso = new Date().toISOString()
const { data: rows, error } = await supabase
.from('scheduled_notifications')
@@ -144,7 +158,10 @@ async function processBatch() {
const res = await fetch(SEND_FCM_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${SERVICE_ROLE_KEY}`,
},
body: JSON.stringify(payload),
})