/** * Notification Service (Orchestrator) * Central hub for sending notifications by coordinating templates, transports, and preferences. */ const pool = require('../config/db'); const templateService = require('./templateService'); const transportService = require('./transportService'); const preferenceService = require('./preferenceService'); const historyService = require('./notificationHistoryService'); const FRONTEND_URL = process.env.FRONTEND_URL || process.env.APP_URL || (process.env.NODE_ENV !== 'production' ? 'http://localhost:5173' : null); if (!FRONTEND_URL && process.env.NODE_ENV === 'production') { throw new Error('FRONTEND_URL must be set in production'); } class NotificationService { constructor() { // Expose preferences and history through this service for convenience this.getUserPreferences = preferenceService.getUserPreferences.bind(preferenceService); this.updatePreferences = preferenceService.updatePreferences.bind(preferenceService); this.savePushSubscription = preferenceService.savePushSubscription.bind(preferenceService); // Proxy history methods this.getNotificationHistory = historyService.getHistory.bind(historyService); this.markAsRead = historyService.markAsRead.bind(historyService); this.getUnreadCount = historyService.getUnreadCount.bind(historyService); // Ensure methods are bound to this instance this.sendNotification = this.sendNotification.bind(this); this.notifyTurnApproaching = this.notifyTurnApproaching.bind(this); this.notifyYourTurn = this.notifyYourTurn.bind(this); this.notifyQueueUpdate = this.notifyQueueUpdate.bind(this); this.notifyAppointmentReminder = this.notifyAppointmentReminder.bind(this); this.notifyDelay = this.notifyDelay.bind(this); this.notifyWaitlistOffer = this.notifyWaitlistOffer.bind(this); this.notifyCancellation = this.notifyCancellation.bind(this); this.notifyMissed = this.notifyMissed.bind(this); this.notifyEmergency = this.notifyEmergency.bind(this); this.notifyAppointmentBooked = this.notifyAppointmentBooked.bind(this); } /** * Main notification sending function */ async sendNotification(userId, type, templateData, options = {}) { const { priority = 'NORMAL', scheduledFor = null, forceChannels = null } = options; // 1. Get user preferences const prefs = await preferenceService.getUserPreferences(userId); // 2. Check if this notification type is enabled const prefField = preferenceService.getPreferenceField(type); if (prefField && !prefs[prefField]) { return { success: false, reason: 'Notification type disabled by user' }; } // 3. Check quiet hours (except for URGENT priority) if (priority !== 'URGENT' && preferenceService.isInQuietHours(prefs)) { return { success: false, reason: 'User in quiet hours' }; } // 4. Get and process template const template = await templateService.getTemplate(type); if (!template) { return { success: false, reason: 'Template not found' }; } const title = templateService.processTemplate(template.title_template, templateData); const message = templateService.processTemplate(template.message_template, templateData); const pushTitle = templateService.processTemplate(template.push_title, templateData); const pushBody = templateService.processTemplate(template.push_body, templateData); const smsText = templateService.processTemplate(template.sms_template, templateData); // Use dedicated WhatsApp template when available, fall back to SMS template const whatsappText = templateService.processTemplate( template.whatsapp_template || template.sms_template, templateData ); // 5. Get user details for contact info (joining patients for phone number) const [userRows] = await pool.query( `SELECT u.email, p.phone FROM users u LEFT JOIN patients p ON u.id = p.id WHERE u.id = ?`, [userId] ); const user = userRows[0]; // 6. Create notification record using history service const notificationId = await historyService.createRecord({ userId, type, title, message, templateData, priority, scheduledFor }); // 7. Determine channels const channels = forceChannels || { push: prefs.push_enabled, sms: prefs.sms_enabled, email: prefs.email_enabled, whatsapp: prefs.whatsapp_enabled !== false // default true if column missing }; const results = { notificationId, push: false, sms: false, email: false, whatsapp: false }; const updates = {}; // 8. Send via each enabled channel if (channels.push && prefs.push_subscription) { results.push = await transportService.sendPush( prefs.push_subscription, pushTitle, pushBody, { notificationId, type, ...templateData } ); updates.push_sent = results.push; } if (channels.sms && user?.phone && smsText) { results.sms = await transportService.sendSMS(user.phone, smsText); updates.sms_sent = results.sms; } if (channels.email && user?.email) { const htmlBody = this._generateEmailHtml(title, message); results.email = await transportService.sendEmail(user.email, title, htmlBody); updates.email_sent = results.email; } // WhatsApp channel — send to patient's registered phone if (channels.whatsapp && user?.phone && whatsappText) { results.whatsapp = await transportService.sendWhatsApp(user.phone, whatsappText); updates.whatsapp_sent = results.whatsapp; } // 9. Batch update transport status and finalize await historyService.updateStatus(notificationId, updates); await historyService.finalizeSentAt(notificationId); return { success: true, ...results }; } /** * Private helper for email HTML generation */ _generateEmailHtml(title, message) { return `

HealthSync

${title}

${message}

You are receiving this because of your appointment settings.

© 2026 HealthSync Patient Portal. All rights reserved.
Notification Settings

`; } // ============ Convenience functions ============ async notifyTurnApproaching(userId, position, doctorName, waitTime) { return this.sendNotification(userId, 'TURN_APPROACHING', { position, doctor_name: doctorName, wait_time: waitTime }, { priority: 'HIGH' }); } async notifyYourTurn(userId, doctorName, room) { return this.sendNotification(userId, 'YOUR_TURN', { doctor_name: doctorName, room: room || 'the consultation room' }, { priority: 'URGENT' }); } async notifyQueueUpdate(userId, position, doctorName) { return this.sendNotification(userId, 'QUEUE_UPDATE', { position, doctor_name: doctorName }); } async notifyAppointmentReminder(userId, doctorName, timeUntil) { return this.sendNotification(userId, 'APPOINTMENT_REMINDER', { doctor_name: doctorName, time_until: timeUntil }); } async notifyDelay(userId, doctorName, delayMins, newTime) { return this.sendNotification(userId, 'DELAY_ALERT', { doctor_name: doctorName, delay_mins: delayMins, new_time: newTime }, { priority: 'HIGH' }); } async notifyWaitlistOffer(userId, doctorName, date, time, expiresIn) { return this.sendNotification(userId, 'WAITLIST_OFFER', { doctor_name: doctorName, date, time, expires_in: expiresIn }, { priority: 'HIGH' }); } async notifyCancellation(userId, doctorName, date) { return this.sendNotification(userId, 'CANCELLATION', { doctor_name: doctorName, date }); } async notifyMissed(userId, doctorName, position, shift) { return this.sendNotification(userId, 'MISSED', { doctor_name: doctorName, position, shift }, { priority: 'HIGH' }); } async notifyEmergency(doctorId, patientName, reason) { return this.sendNotification(doctorId, 'EMERGENCY_ALERT', { patient_name: patientName, reason: reason || 'Urgent attention required' }, { priority: 'URGENT' }); } /** * Notify a patient that their appointment has been confirmed. * Triggered immediately after a successful booking — primarily for WhatsApp. * * @param {number} userId Patient's user ID * @param {string} doctorName Doctor's display name (e.g. "Dr. Sharma") * @param {string} date Appointment date string (YYYY-MM-DD) * @param {string} timeSlot Appointment time slot string */ async notifyAppointmentBooked(userId, doctorName, date, timeSlot) { return this.sendNotification( userId, 'APPOINTMENT_BOOKED', { doctor_name: doctorName, date, time_slot: timeSlot }, { priority: 'HIGH' } ); } } module.exports = new NotificationService();