import { Resend } from 'resend' /** * @description Email service integration using Resend API for transactional emails * @audit BUSINESS RULE: Sends HTML-formatted emails with PDF receipt attachments * @audit SECURITY: Requires RESEND_API_KEY environment variable for authentication * @audit PERFORMANCE: Uses Resend SDK for reliable email delivery * @audit AUDIT: Email send results logged for delivery tracking */ /** Resend client instance configured with API key */ const resendClient = new Resend(process.env.RESEND_API_KEY!) /** * @description Interface defining data required for receipt email * @property {string} to - Recipient email address * @property {string} customerName - Customer's first name for personalization * @property {string} bookingId - UUID of the booking for receipt generation * @property {string} serviceName - Name of the booked service * @property {string} date - Formatted date of the appointment * @property {string} time - Formatted time of the appointment * @property {string} location - Name and address of the salon location * @property {string} staffName - Assigned staff member name * @property {number} price - Total price of the service in MXN * @property {string} pdfUrl - URL path to the generated PDF receipt */ interface ReceiptEmailData { to: string customerName: string bookingId: string serviceName: string date: string time: string location: string staffName: string price: number pdfUrl: string } /** * @description Sends a receipt confirmation email with PDF attachment to the customer * @param {ReceiptEmailData} data - Email data including customer details and booking information * @returns {Promise<{ success: boolean; data?: any; error?: any }>} - Result of email send operation * @example sendReceiptEmail({ to: 'customer@email.com', customerName: 'Ana', bookingId: '...', serviceName: 'Manicure', date: '2026-01-21', time: '10:00', location: 'ANCHOR:23 Saltillo', staffName: 'Maria', price: 1500, pdfUrl: '/receipts/...' }) * @audit BUSINESS RULE: Sends branded HTML email with ANCHOR:23 styling and Spanish content * @audit Validate: Attaches PDF receipt with booking ID in filename * @audit PERFORMANCE: Single API call to Resend with HTML content and attachment * @audit AUDIT: Email sending logged for customer communication tracking */ export async function sendReceiptEmail(data: ReceiptEmailData) { try { const emailHtml = ` Recibo de Reserva - ANCHOR:23

Confirmación de Reserva

Hola ${data.customerName},

Tu reserva ha sido confirmada exitosamente. Aquí están los detalles:

Detalles de la Reserva

Servicio: ${data.serviceName}

Fecha: ${data.date}

Hora: ${data.time}

Ubicación: ${data.location}

Profesional: ${data.staffName}

Total: $${data.price} MXN

Adjunto encontrarás el recibo en formato PDF para tus registros.

Descargar Recibo PDF

Si tienes alguna pregunta, no dudes en contactarnos.

¡Te esperamos en ANCHOR:23!

` const { data: result, error } = await resendClient.emails.send({ from: 'ANCHOR:23 ', to: data.to, subject: 'Confirmación de Reserva - ANCHOR:23', html: emailHtml, attachments: [ { filename: `recibo-${data.bookingId.slice(-8)}.pdf`, path: data.pdfUrl } ] }) if (error) { console.error('Email send error:', error) return { success: false, error } } return { success: true, data: result } } catch (error) { console.error('Email service error:', error) return { success: false, error } } }