'use strict'; const { recordAndSend } = require('./senders/notificationRecorder'); const { sendEmail } = require('./senders/emailSender'); const { sendBaleMessage } = require('./senders/baleBotSender'); const { resolveNotifyFlags } = require('./notifyResolver'); const { findActionDef } = require('../components/settings/notificationActions'); const { isSmsBypassNumber } = require('../components/settings/messagingFlags'); /** * Send SMS (via handler), email, and bot notifications for an action when enabled. * SMS handler should use template-based recordAndSend internally. */ const notifyAction = async ({ actionKey, userId = null, phoneNumber = null, email = null, subject = '', body = '', smsHandler = null, requestSource = {}, relatedEvent = null }) => { const flags = await resolveNotifyFlags(requestSource, actionKey); const def = findActionDef(actionKey); const eventTag = relatedEvent || def?.relatedEvent || actionKey; const results = {}; const isBypass = phoneNumber ? await isSmsBypassNumber(phoneNumber) : false; if ((flags.sms || isBypass) && phoneNumber && typeof smsHandler === 'function') { results.sms = await smsHandler(); } if (flags.email && email) { results.email = await recordAndSend({ userId, channel: 'email', subject, body, relatedEvent: eventTag, sendFn: () => sendEmail({ to: email, subject, body }) }); } if (flags.bot && phoneNumber) { results.bot = await recordAndSend({ userId, channel: 'baleBot', subject, body, relatedEvent: eventTag, sendFn: () => sendBaleMessage({ chatId: phoneNumber, body }) }); } return results; }; module.exports = { notifyAction };