diff --git a/lib/notifier/index.js b/lib/notifier/index.js index 7c3841fb..abdb8b81 100644 --- a/lib/notifier/index.js +++ b/lib/notifier/index.js @@ -166,6 +166,51 @@ function transactionNotify (tx, rec) { }) } +function complianceNotify (customer, deviceId, ...args) { + return Promise.all([ + settingsLoader.loadLatest(), + queries.getMachineName(deviceId) + ]) + .then(([settings, machineName]) => { + const notifications = configManager.getGlobalNotifications(settings.config) + + const action = args[0] + const period = args[1] + + const msgCore = { + BLOCKED: `was blocked`, + SUSPENDED: `was suspended for ${!!period && period} days` + } + + const rec = { + sms: { + body: `Customer ${customer.phone} ${msgCore[action]} - ${machineName}` + }, + email: { + subject: `Customer compliance`, + body: `Customer ${customer.phone} ${msgCore[action]} in machine ${machineName}` + } + } + + const promises = [] + + const emailActive = + notifications.email.active && + notifications.email.compliance + + const smsActive = + notifications.sms.active && + notifications.sms.compliance + + if (emailActive) promises.push(emailFuncs.sendMessage(settings, rec)) + if (smsActive) promises.push(smsFuncs.sendMessage(settings, rec)) + + notifyIfActive('compliance', 'customerComplianceNotify', customer, deviceId, ...args) + + return Promise.all(promises) + }) +} + function sendRedemptionMessage (txId, error) { const subject = `Here's an update on transaction ${txId}` const body = error @@ -216,6 +261,7 @@ const notifyIfActive = (type, fnName, ...args) => { module.exports = { transactionNotify, + complianceNotify, checkNotification, checkPings, checkStuckScreen, diff --git a/lib/routes/customerRoutes.js b/lib/routes/customerRoutes.js index 0f5f6902..c4745c6a 100644 --- a/lib/routes/customerRoutes.js +++ b/lib/routes/customerRoutes.js @@ -61,7 +61,7 @@ function triggerBlock (req, res, next) { customers.update(id, { authorizedOverride: 'blocked' }) .then(customer => { - notifier.notifyIfActive('compliance', 'customerComplianceNotify', customer, req.deviceId, 'BLOCKED') + notifier.complianceNotify(customer, req.deviceId, 'BLOCKED') return respond(req, res, { customer }) }) .catch(next) @@ -80,7 +80,7 @@ function triggerSuspend (req, res, next) { date.setDate(date.getDate() + days) customers.update(id, { suspendedUntil: date }) .then(customer => { - notifier.notifyIfActive('compliance', 'customerComplianceNotify', customer, req.deviceId, 'SUSPENDED', days) + notifier.complianceNotify(customer, req.deviceId, 'SUSPENDED', days) return respond(req, res, { customer }) }) .catch(next)