tasq/supabase/functions/send_fcm/index.ts
Marc Rejohn Castillano 5979a04254 * Push Notification Setup and attempt
* Office Ordering
* Allow editing of Task and Ticket Details after creation
2026-02-24 21:06:46 +08:00

78 lines
3.3 KiB
TypeScript

import { createClient } from 'npm:@supabase/supabase-js@2';
// we no longer use google-auth-library or a service account; instead
// send FCM via a legacy server key provided as an environment variable
// (FCM_SERVER_KEY). This avoids compatibility problems on the edge.
const supabase = createClient(Deno.env.get('SUPABASE_URL')!, Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!);
Deno.serve(async (req) => {
try {
await supabase.from('send_fcm_errors').insert({ payload: null, error: 'step1', stack: null });
const body = await req.json();
try { await supabase.from('send_fcm_errors').insert({ payload: body, error: 'step2', stack: null }); } catch (_) {}
// gather tokens (same as before)
let tokens: any[] = [];
if (Array.isArray(body.tokens)) {
tokens = body.tokens;
} else if (Array.isArray(body.user_ids)) {
try {
const { data: rows } = await supabase
.from('fcm_tokens')
.select('token')
.in('user_id', body.user_ids);
if (rows) tokens = rows.map((r: any) => r.token);
try { await supabase.from('send_fcm_errors').insert({ payload: rows ?? null, error: 'step4a', stack: null }); } catch (_) {}
} catch (_) {}
}
if (body.record && body.record.user_id) {
try {
const { data: rows } = await supabase
.from('fcm_tokens')
.select('token')
.eq('user_id', body.record.user_id);
if (rows) tokens = rows.map((r: any) => r.token);
try { await supabase.from('send_fcm_errors').insert({ payload: rows ?? null, error: 'step4b', stack: null }); } catch (_) {}
} catch (_) {}
}
try { await supabase.from('send_fcm_errors').insert({ payload: tokens, error: 'step3', stack: null }); } catch (_) {}
// build notification text
let notificationTitle = '';
let notificationBody = '';
if (body.record) {
notificationTitle = `New ${body.record.type}`;
notificationBody = 'You have a new update in TasQ.';
if (body.record.ticket_id) {
notificationBody = 'You have a new update on your ticket.';
} else if (body.record.task_id) {
notificationBody = 'You have a new update on your task.';
}
}
try { await supabase.from('send_fcm_errors').insert({ payload: {title: notificationTitle, body: notificationBody}, error: 'step5', stack: null }); } catch (_) {}
// use legacy server key for FCM send
const serverKey = Deno.env.get('FCM_SERVER_KEY');
if (serverKey && tokens.length) {
const sendPromises = tokens.map((tok) => {
return fetch('https://fcm.googleapis.com/fcm/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `key=${serverKey}`,
},
body: JSON.stringify({ to: tok, notification: { title: notificationTitle, body: notificationBody }, data: body.data || {} }),
});
});
try {
const results = await Promise.all(sendPromises);
try { await supabase.from('send_fcm_errors').insert({ payload: results, error: 'step8', stack: null }); } catch (_) {}
} catch (e) {
try { await supabase.from('send_fcm_errors').insert({ payload: e.toString(), error: 'step8error', stack: null }); } catch (_) {}
}
}
} catch (_) {}
return new Response('ok', { headers: { 'Access-Control-Allow-Origin': '*' } });
});