61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
'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');
|
|
|
|
/**
|
|
* 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 = {};
|
|
|
|
if (flags.sms && 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
|
|
};
|