@ideal-photography/shared
Version:
Shared MongoDB and utility logic for Ideal Photography PWAs: users, products, services, bookings, orders/cart, galleries, reviews, notifications, campaigns, settings, audit logs, minimart items/orders, and push notification subscriptions.
56 lines (48 loc) • 2.48 kB
JavaScript
import { sendMail } from '../utils/email.js';
// Mapping from templateKey to builder function imported lazily to avoid heavy require cost
const templateBuilders = {
'welcome': async () => (await import('../utils/email.js')).buildWelcomeEmail,
'verification-status': async () => (await import('../utils/email.js')).buildIDVerificationStatusEmail,
'booking-confirmation': async () => (await import('../utils/email.js')).buildBookingConfirmationEmail,
'payment-confirmation': async () => (await import('../utils/email.js')).buildPaymentConfirmationEmail,
'booking-reminder': async () => (await import('../utils/email.js')).buildReminderEmail,
'error-notification': async () => (await import('../utils/email.js')).buildErrorNotificationEmail,
// add more mapping as templates are added
};
class EmailNotificationService {
/**
* Sends an email using a template builder
* @param {string} templateKey key in templateBuilders
* @param {object} data data passed to template builder
* @param {string|string[]} to recipient email(s)
*/
static async send(templateKey, data, to) {
if (!templateBuilders[templateKey]) {
throw new Error(`Email template '${templateKey}' not registered`);
}
const builderImporter = templateBuilders[templateKey];
const builder = await builderImporter();
const { html, text } = builder(data);
const subject = EmailNotificationService._deriveSubject(templateKey, data);
return sendMail({ to, subject, html, text });
}
static _deriveSubject(templateKey, data) {
switch (templateKey) {
case 'welcome':
return 'Welcome to IDEAS MEDIA COMPANY';
case 'verification-status':
return `ID Verification ${data.status === 'verified' ? 'Approved' : 'Update'}`;
case 'booking-confirmation':
return 'Your Booking is Confirmed!';
case 'payment-confirmation':
return 'Payment Received';
case 'booking-reminder':
return 'Session Reminder';
case 'error-notification':
return `🚨 Error in ${data.pwaType?.toUpperCase?.()} PWA - ${data.errorId}`;
default:
return 'Notification from IDEAS MEDIA COMPANY';
}
}
}
export default EmailNotificationService;