/** * Resend email helper — server-side only, never exposes API key. * Uses the Resend REST API directly. */ const RESEND_API_KEY = process.env.RESEND_API_KEY; const RESEND_API_URL = "https://api.resend.com/emails"; const FROM_EMAIL = "Vela "; // Update with verified domain in production interface SendEmailOptions { to: string[]; subject: string; html: string; replyTo?: string; } export async function sendEmail(options: SendEmailOptions): Promise<{ id: string } | { error: string }> { if (!RESEND_API_KEY) { console.warn("RESEND_API_KEY not configured — email not sent"); return { error: "RESEND_API_KEY not configured" }; } try { const res = await fetch(RESEND_API_URL, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${RESEND_API_KEY}`, }, body: JSON.stringify({ from: FROM_EMAIL, to: options.to, subject: options.subject, html: options.html, reply_to: options.replyTo, }), }); const data = await res.json(); if (!res.ok) { console.error("Resend API error:", data); return { error: data.message || "Failed to send email" }; } return { id: data.id }; } catch (error) { console.error("Resend API network error:", error); return { error: "Network error sending email" }; } } // ── Email templates ────────────────────────────────────────── export function leaveApprovalEmailTemplate( employeeName: string, leaveType: string, startDate: string, endDate: string ): { subject: string; html: string } { return { subject: `Leave Request Approved — ${employeeName}`, html: `

Leave Request Approved

Hi ${employeeName},

Your leave request has been approved.

Type: ${leaveType}
From: ${startDate}
To: ${endDate}

This is an automated notification from Vela.

`, }; } export function leaveRejectionEmailTemplate( employeeName: string, leaveType: string, startDate: string, endDate: string, reason: string ): { subject: string; html: string } { return { subject: `Leave Request Update — ${employeeName}`, html: `

Leave Request Not Approved

Hi ${employeeName},

Unfortunately, your leave request could not be approved at this time.

Type: ${leaveType}
From: ${startDate}
To: ${endDate}
Reason: ${reason}

This is an automated notification from Vela.

`, }; } export function invoiceNotificationTemplate( clientName: string, invoiceNumber: string, amount: string, dueDate: string, invoiceUrl?: string ): { subject: string; html: string } { return { subject: `Invoice ${invoiceNumber} from Vela`, html: `

Invoice ${invoiceNumber}

Hi ${clientName},

Please find your invoice below.

Invoice: ${invoiceNumber}
Amount: ${amount}
Due: ${dueDate}

${invoiceUrl ? `View Invoice` : ""}

This is an automated notification from Vela.

`, }; } export function contractSignatureTemplate( employeeName: string, contractTitle: string, signUrl: string ): { subject: string; html: string } { return { subject: `Please sign your contract: ${contractTitle}`, html: `

Sign Your Contract

Hi ${employeeName},

Please review and sign your contract: ${contractTitle}

Sign Contract

This is an automated notification from Vela.

`, }; }