mmmh
This commit is contained in:
@@ -11,173 +11,186 @@ serve(async (req) => {
|
||||
if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders })
|
||||
|
||||
try {
|
||||
const bodyText = await req.text();
|
||||
const payload = JSON.parse(bodyText);
|
||||
const bodyText = await req.text()
|
||||
const payload = JSON.parse(bodyText)
|
||||
|
||||
// Estraggo i dati dal payload standard di Supabase
|
||||
const tableName = payload.table;
|
||||
const record = payload.record;
|
||||
const tableName = payload.table
|
||||
const eventType = payload.type
|
||||
const record = payload.record
|
||||
const old_record = payload.old_record
|
||||
|
||||
if (!tableName || !record) {
|
||||
throw new Error("Payload non valido, manca table o record.");
|
||||
throw new Error("Payload non valido, manca table o record.")
|
||||
}
|
||||
|
||||
let event_type = '';
|
||||
let target_staff_id = '';
|
||||
let title = '';
|
||||
let description = '';
|
||||
let reference_id = '';
|
||||
|
||||
// Inizializziamo il client Supabase subito, ci serve per le query
|
||||
const supabaseClient = createClient(
|
||||
Deno.env.get('SUPABASE_URL') ?? '',
|
||||
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
|
||||
)
|
||||
|
||||
// SMISTAMENTO IN BASE ALLA TABELLA
|
||||
if (tableName === 'task_assignments') {
|
||||
event_type = 'task_assigned';
|
||||
target_staff_id = record.staff_id;
|
||||
reference_id = record.task_id;
|
||||
title = 'Nuovo Task Assegnato';
|
||||
|
||||
// 1. Peschiamo i dettagli completi del task
|
||||
const { data: taskData } = await supabaseClient
|
||||
.from('tasks')
|
||||
.select('*')
|
||||
.eq('id', reference_id)
|
||||
.single();
|
||||
|
||||
// 2. Peschiamo il nome del creatore
|
||||
let creatorName = "Admin";
|
||||
if (taskData?.created_by_id) {
|
||||
const { data: creatorData } = await supabaseClient
|
||||
.from('staff_members')
|
||||
.select('first_name, last_name')
|
||||
.eq('id', taskData.created_by_id)
|
||||
.single();
|
||||
|
||||
if (creatorData) {
|
||||
creatorName = `${creatorData.first_name} ${creatorData.last_name}`.trim();
|
||||
}
|
||||
// =========================================================================
|
||||
// 🥷 1. IDENTIFICARE ESTRARRE I BERSAGLI (CHI DEVO NOTIFICARE?)
|
||||
// =========================================================================
|
||||
let usersToNotify: string[] = []
|
||||
let notificationTitle = ''
|
||||
let notificationBody = ''
|
||||
let referenceId = ''
|
||||
let fluxEventType = '' // 'task_assigned', 'task_completed', etc.
|
||||
|
||||
// SCENARIO A: ASSEGNAZIONE TASK
|
||||
if (tableName === 'task_assignments' && eventType === 'INSERT') {
|
||||
const assigneeId = record.staff_id
|
||||
const assignerId = record.assigned_by_id
|
||||
referenceId = record.task_id
|
||||
fluxEventType = 'task_assigned'
|
||||
|
||||
if (assigneeId === assignerId) {
|
||||
return new Response(JSON.stringify({ message: "Auto-assegnazione ignorata." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
// 3. Formattiamo la data (se esiste)
|
||||
let dueDateStr = 'Nessuna scadenza';
|
||||
usersToNotify.push(assigneeId)
|
||||
|
||||
// Costruiamo i testi
|
||||
const { data: taskData } = await supabaseClient.from('tasks').select('title, description, due_date').eq('id', referenceId).single()
|
||||
const { data: assignerData } = await supabaseClient.from('staff_members').select('first_name, last_name').eq('id', assignerId).single()
|
||||
|
||||
const taskTitle = taskData?.title || 'Senza titolo'
|
||||
const taskDesc = taskData?.description || 'Nessuna descrizione'
|
||||
const assignerName = assignerData ? `${assignerData.first_name} ${assignerData.last_name}`.trim() : 'Un collega'
|
||||
|
||||
let dueDateStr = 'Nessuna scadenza'
|
||||
if (taskData?.due_date) {
|
||||
const d = new Date(taskData.due_date);
|
||||
dueDateStr = d.toLocaleDateString('it-IT');
|
||||
dueDateStr = new Date(taskData.due_date).toLocaleDateString('it-IT')
|
||||
}
|
||||
|
||||
// 4. Costruiamo il Body multilinea per Android
|
||||
const taskTitle = taskData?.title || 'Senza titolo';
|
||||
const taskDesc = taskData?.description || 'Nessuna descrizione fornita.';
|
||||
notificationTitle = 'Nuovo Task Assegnato'
|
||||
notificationBody = `${taskTitle}\n\nCreato da: ${assignerName}\nScadenza: ${dueDateStr}\nDettagli: ${taskDesc}`
|
||||
}
|
||||
|
||||
// SCENARIO B: COMPLETAMENTO TASK
|
||||
else if (tableName === 'tasks' && eventType === 'UPDATE') {
|
||||
const justCompleted = record.status === 'completed' && old_record.status !== 'completed';
|
||||
|
||||
description = `${taskTitle}\n\nCreato da: ${creatorName}\nScadenza: ${dueDateStr}\nDettagli: ${taskDesc}`;
|
||||
}
|
||||
if (!justCompleted) {
|
||||
return new Response(JSON.stringify({ message: "Update ignorato (non è un completamento)." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
// 1. Leggiamo le preferenze specifiche di questo dipendente
|
||||
const { data: settings, error: settingsError } = await supabaseClient
|
||||
.from('staff_notification_settings')
|
||||
.select('*')
|
||||
.eq('staff_id', target_staff_id)
|
||||
.single()
|
||||
const completerId = record.updated_by_id
|
||||
referenceId = record.id
|
||||
fluxEventType = 'task_completed' // Nota: assicurati di avere questa colonna o un fallback nelle preferenze
|
||||
|
||||
if (settingsError || !settings) throw new Error('Preferenze utente non trovate')
|
||||
|
||||
// 2. Determiniamo QUALI canali usare in base all'evento e agli switch dell'utente
|
||||
let sendPush = false
|
||||
let sendEmail = false
|
||||
|
||||
switch (event_type) {
|
||||
case 'task_assigned':
|
||||
sendPush = settings.task_assigned_push
|
||||
sendEmail = settings.task_assigned_email
|
||||
break
|
||||
case 'note_invited':
|
||||
sendPush = settings.note_invited_push
|
||||
sendEmail = settings.note_invited_email
|
||||
break
|
||||
case 'new_operation':
|
||||
sendPush = settings.new_operation_push
|
||||
sendEmail = settings.new_operation_email
|
||||
break
|
||||
case 'new_ticket':
|
||||
sendPush = settings.new_ticket_push
|
||||
sendEmail = settings.new_ticket_email
|
||||
break
|
||||
default:
|
||||
throw new Error('Tipo evento non riconosciuto')
|
||||
}
|
||||
|
||||
// Se l'utente ha spento tutto, interrompiamo subito risparmiando risorse
|
||||
if (!sendPush && !sendEmail) {
|
||||
return new Response(JSON.stringify({ message: 'L\'utente ha disattivato le notifiche per questo evento.' }), {
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 200
|
||||
})
|
||||
}
|
||||
|
||||
// Se arriviamo qui, dobbiamo inviare qualcosa. Prepariamo i dati dell'utente.
|
||||
const { data: staffMember } = await supabaseClient
|
||||
.from('staff_members')
|
||||
.select('email, first_name')
|
||||
.eq('id', target_staff_id)
|
||||
.single()
|
||||
|
||||
// 3. LOGICA PUSH (FCM)
|
||||
if (sendPush) {
|
||||
const firebaseSecret = Deno.env.get('FIREBASE_SERVICE_ACCOUNT');
|
||||
const { data: assignments } = await supabaseClient.from('task_assignments').select('staff_id').eq('task_id', referenceId)
|
||||
|
||||
if (!firebaseSecret) {
|
||||
console.error("ERRORE: Secret FIREBASE_SERVICE_ACCOUNT mancante nel progetto!");
|
||||
} else {
|
||||
const credentials = JSON.parse(firebaseSecret);
|
||||
const jwtClient = new JWT({
|
||||
email: credentials.client_email,
|
||||
key: credentials.private_key,
|
||||
scopes: ['https://www.googleapis.com/auth/firebase.messaging'],
|
||||
});
|
||||
const fcmAccessToken = (await jwtClient.getAccessToken()).token;
|
||||
if (assignments && assignments.length > 0) {
|
||||
usersToNotify = assignments.map(a => a.staff_id).filter(id => id !== completerId)
|
||||
}
|
||||
|
||||
const { data: devices } = await supabaseClient
|
||||
.from('staff_devices')
|
||||
.select('fcm_token')
|
||||
.eq('staff_id', target_staff_id);
|
||||
if (usersToNotify.length === 0) {
|
||||
return new Response(JSON.stringify({ message: "Nessun altro assegnatario da notificare per la chiusura." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
if (devices && devices.length > 0) {
|
||||
const { data: completerData } = await supabaseClient.from('staff_members').select('first_name, last_name').eq('id', completerId).single()
|
||||
const completerName = completerData ? `${completerData.first_name} ${completerData.last_name}`.trim() : 'Un collega'
|
||||
const taskTitle = record.title || 'Senza titolo'
|
||||
|
||||
notificationTitle = 'Task Completato ✅'
|
||||
notificationBody = `${completerName} ha appena chiuso il task: ${taskTitle}`
|
||||
}
|
||||
|
||||
// SCENARIO C: ALTRI EVENTI (Es. note_invited, ecc. Mettili qui quando ti serviranno)
|
||||
else {
|
||||
return new Response(JSON.stringify({ message: "Tabella o evento non gestito." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
|
||||
// =========================================================================
|
||||
// 🥷 2. MOTORE DI INVIO MASSIVO PER I BERSAGLI IDENTIFICATI
|
||||
// =========================================================================
|
||||
|
||||
// Inizializziamo FCM una volta sola per risparmiare tempo se ci sono push da mandare
|
||||
const firebaseSecret = Deno.env.get('FIREBASE_SERVICE_ACCOUNT')
|
||||
let fcmAccessToken = ''
|
||||
let fcmProjectId = ''
|
||||
|
||||
if (firebaseSecret) {
|
||||
const credentials = JSON.parse(firebaseSecret)
|
||||
fcmProjectId = credentials.project_id
|
||||
const jwtClient = new JWT({
|
||||
email: credentials.client_email,
|
||||
key: credentials.private_key,
|
||||
scopes: ['https://www.googleapis.com/auth/firebase.messaging'],
|
||||
})
|
||||
fcmAccessToken = (await jwtClient.getAccessToken()).token ?? ''
|
||||
}
|
||||
|
||||
const resendApiKey = Deno.env.get('RESEND_API_KEY')
|
||||
|
||||
let pushSentCount = 0;
|
||||
let emailSentCount = 0;
|
||||
|
||||
// Cicliamo tutti gli utenti che meritano la notifica
|
||||
for (const targetStaffId of usersToNotify) {
|
||||
|
||||
// A) Leggiamo le preferenze dello specifico utente
|
||||
const { data: settings } = await supabaseClient
|
||||
.from('staff_notification_settings')
|
||||
.select('*')
|
||||
.eq('staff_id', targetStaffId)
|
||||
.single()
|
||||
|
||||
if (!settings) continue; // Salta se non ha le preferenze
|
||||
|
||||
let sendPush = false
|
||||
let sendEmail = false
|
||||
|
||||
// B) Traduciamo l'evento nelle sue preferenze
|
||||
// (Se aggiungi 'task_completed' al DB settings, mettilo qui. Per ora riuso le preesistenti se manca)
|
||||
switch (fluxEventType) {
|
||||
case 'task_assigned':
|
||||
sendPush = settings.task_assigned_push
|
||||
sendEmail = settings.task_assigned_email
|
||||
break
|
||||
case 'task_completed':
|
||||
// Se nel DB hai aggiunto task_completed_push usa quello.
|
||||
// Altrimenti puoi fare fallback su task_assigned_push per testare.
|
||||
sendPush = settings.task_assigned_push
|
||||
sendEmail = settings.task_assigned_email
|
||||
break
|
||||
// Aggiungi qui gli altri case (note_invited, new_operation)
|
||||
}
|
||||
|
||||
if (!sendPush && !sendEmail) continue; // Questo utente non vuole essere disturbato
|
||||
|
||||
// Recuperiamo nome ed email per questo specifico utente
|
||||
const { data: staffMember } = await supabaseClient.from('staff_members').select('email, first_name').eq('id', targetStaffId).single()
|
||||
|
||||
// C) INVIO PUSH (FCM)
|
||||
if (sendPush && fcmAccessToken) {
|
||||
const { data: devices } = await supabaseClient.from('staff_devices').select('fcm_token').eq('staff_id', targetStaffId)
|
||||
|
||||
if (devices) {
|
||||
for (const device of devices) {
|
||||
try {
|
||||
const res = await fetch(`https://fcm.googleapis.com/v1/projects/${credentials.project_id}/messages:send`, {
|
||||
const res = await fetch(`https://fcm.googleapis.com/v1/projects/${fcmProjectId}/messages:send`, {
|
||||
method: 'POST',
|
||||
headers: { 'Authorization': `Bearer ${fcmAccessToken}`, 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
message: {
|
||||
token: device.fcm_token,
|
||||
notification: { title, body: description },
|
||||
data: { click_action: 'FLUTTER_NOTIFICATION_CLICK', eventType: event_type, referenceId: reference_id },
|
||||
notification: { title: notificationTitle, body: notificationBody },
|
||||
data: { click_action: 'FLUTTER_NOTIFICATION_CLICK', eventType: fluxEventType, referenceId: referenceId },
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
// QUI È DOVE CATTURIAMO LA RISPOSTA DI GOOGLE
|
||||
const fcmResponseData = await res.json();
|
||||
|
||||
if (!res.ok) {
|
||||
console.error("FCM HA RIFIUTATO LA NOTIFICA:", fcmResponseData);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.error('Errore di rete durante invio Push:', err);
|
||||
}
|
||||
if (res.ok) pushSentCount++;
|
||||
else console.error("FCM HA RIFIUTATO LA NOTIFICA:", await res.json());
|
||||
} catch (err) { console.error('Errore rete FCM:', err) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. LOGICA EMAIL (Resend)
|
||||
if (sendEmail && staffMember?.email) {
|
||||
const resendApiKey = Deno.env.get('RESEND_API_KEY')
|
||||
if (resendApiKey) {
|
||||
// D) INVIO EMAIL (Resend)
|
||||
if (sendEmail && resendApiKey && staffMember?.email) {
|
||||
try {
|
||||
await fetch('https://api.resend.com/emails', {
|
||||
method: 'POST',
|
||||
@@ -185,20 +198,24 @@ serve(async (req) => {
|
||||
body: JSON.stringify({
|
||||
from: 'FLUX Notifiche <onboarding@resend.dev>',
|
||||
to: staffMember.email,
|
||||
subject: title,
|
||||
html: `<p>Ciao ${staffMember.first_name},</p><p>${description}</p><p><br>Il team FLUX</p>`,
|
||||
subject: notificationTitle,
|
||||
html: `<p>Ciao ${staffMember.first_name},</p><p>${notificationBody.replace(/\n/g, '<br>')}</p><p><br>Il team FLUX</p>`,
|
||||
}),
|
||||
})
|
||||
emailSentCount++;
|
||||
} catch (err) { console.error('Errore invio Email:', err) }
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify({ success: true, push_sent: sendPush, email_sent: sendEmail }), {
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 200
|
||||
})
|
||||
return new Response(JSON.stringify({
|
||||
success: true,
|
||||
targets_found: usersToNotify.length,
|
||||
push_sent: pushSentCount,
|
||||
email_sent: emailSentCount
|
||||
}), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 200 })
|
||||
|
||||
} catch (error) {
|
||||
console.error("ERRORE FATALE NELLA FUNZIONE:", error);
|
||||
console.error("ERRORE FATALE NELLA FUNZIONE:", error)
|
||||
return new Response(JSON.stringify({ error: error.message }), {
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 500
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user