feat: email notifications via Resend on new incident
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { Resend } from 'resend'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { newIncidentTemplate } from '@/lib/notifications/templates/new-incident'
|
||||
|
||||
export async function sendNewIncidentEmail(
|
||||
incidentId: string,
|
||||
siteId: string,
|
||||
reference_no: string,
|
||||
incidentType: string,
|
||||
): Promise<void> {
|
||||
const supabase = await createClient()
|
||||
|
||||
const { data: recipients } = await supabase
|
||||
.from('users')
|
||||
.select('email, name, role')
|
||||
.in('role', ['supervisor', 'hse'])
|
||||
.eq('site_id', siteId)
|
||||
|
||||
if (!recipients || recipients.length === 0) return
|
||||
|
||||
const to = recipients.map((r: { email: string }) => r.email).filter(Boolean)
|
||||
if (to.length === 0) return
|
||||
|
||||
const { data: incident } = await supabase
|
||||
.from('incidents')
|
||||
.select('reported_at, sites (name), reporter:users!reported_by (name)')
|
||||
.eq('id', incidentId)
|
||||
.single()
|
||||
|
||||
const siteName = (incident?.sites as any)?.name ?? 'Unknown Site'
|
||||
const reporterName = (incident?.reporter as any)?.name ?? 'Unknown'
|
||||
const reportedAt = incident?.reported_at
|
||||
? new Date(incident.reported_at).toLocaleString('en-MY', { timeZone: 'Asia/Kuala_Lumpur' })
|
||||
: '-'
|
||||
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL ?? 'http://localhost:3000'
|
||||
|
||||
const { subject, html, text } = newIncidentTemplate({
|
||||
reference_no,
|
||||
incident_type: incidentType,
|
||||
site_name: siteName,
|
||||
reporter_name: reporterName,
|
||||
reported_at: reportedAt,
|
||||
site_url: siteUrl,
|
||||
incident_id: incidentId,
|
||||
})
|
||||
|
||||
const resend = new Resend(process.env.RESEND_API_KEY)
|
||||
const from = process.env.RESEND_FROM_EMAIL ?? 'onboarding@resend.dev'
|
||||
|
||||
const { error } = await resend.emails.send({ from, to, subject, html, text })
|
||||
if (error) console.error('Resend error:', error)
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
export function newIncidentTemplate(params: {
|
||||
reference_no: string
|
||||
incident_type: string
|
||||
site_name: string
|
||||
reporter_name: string
|
||||
reported_at: string
|
||||
site_url: string
|
||||
incident_id: string
|
||||
}): { subject: string; html: string; text: string } {
|
||||
const typeLabel = params.incident_type.replace(/_/g, ' ')
|
||||
const subject = `[IMS] New incident ${params.reference_no} — ${typeLabel} at ${params.site_name}`
|
||||
const link = `${params.site_url}/hse/incidents/${params.incident_id}`
|
||||
|
||||
const text = `
|
||||
New incident report received.
|
||||
|
||||
Reference: ${params.reference_no}
|
||||
Type: ${typeLabel}
|
||||
Site: ${params.site_name}
|
||||
Reported by: ${params.reporter_name}
|
||||
Time: ${params.reported_at}
|
||||
|
||||
View incident: ${link}
|
||||
`.trim()
|
||||
|
||||
const html = `
|
||||
<div style="font-family:sans-serif;max-width:600px;margin:0 auto">
|
||||
<h2 style="color:#1e293b">New Incident Report</h2>
|
||||
<table style="border-collapse:collapse;width:100%">
|
||||
<tr><td style="padding:6px 0;color:#64748b">Reference</td><td style="padding:6px 0;font-weight:600">${params.reference_no}</td></tr>
|
||||
<tr><td style="padding:6px 0;color:#64748b">Type</td><td style="padding:6px 0">${typeLabel}</td></tr>
|
||||
<tr><td style="padding:6px 0;color:#64748b">Site</td><td style="padding:6px 0">${params.site_name}</td></tr>
|
||||
<tr><td style="padding:6px 0;color:#64748b">Reported by</td><td style="padding:6px 0">${params.reporter_name}</td></tr>
|
||||
<tr><td style="padding:6px 0;color:#64748b">Time</td><td style="padding:6px 0">${params.reported_at}</td></tr>
|
||||
</table>
|
||||
<p style="margin-top:20px">
|
||||
<a href="${link}" style="background:#2563eb;color:#fff;padding:10px 18px;text-decoration:none;border-radius:6px;display:inline-block">
|
||||
View Incident
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
`.trim()
|
||||
|
||||
return { subject, html, text }
|
||||
}
|
||||
Reference in New Issue
Block a user