From 45a11f91f01acc64cc4d718a546f555006f2acc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Thu, 13 Oct 2022 19:25:29 +0100 Subject: [PATCH] feat: backport webhook firehose --- .sample.env | 1 + lib/notifier/index.js | 24 ++++++++++++++++++++++++ lib/notifier/utils.js | 4 ++++ lib/notifier/webhook.js | 21 +++++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 lib/notifier/webhook.js diff --git a/.sample.env b/.sample.env index ae44d16f..6f867352 100644 --- a/.sample.env +++ b/.sample.env @@ -57,3 +57,4 @@ HTTP= DEV_MODE= ## Uncategorized variables +WEBHOOK_URL= diff --git a/lib/notifier/index.js b/lib/notifier/index.js index 9fd21848..81baf3ba 100644 --- a/lib/notifier/index.js +++ b/lib/notifier/index.js @@ -10,6 +10,7 @@ const notificationCenter = require('./notificationCenter') const utils = require('./utils') const emailFuncs = require('./email') const smsFuncs = require('./sms') +const webhookFuncs = require('./webhook') const { STALE, STALE_STATE } = require('./codes') function buildMessage (alerts, notifications) { @@ -185,6 +186,10 @@ function complianceNotify (customer, deviceId, action, period) { email: { subject: `Customer compliance`, body: `Customer ${customer.phone} ${msgCore[action]} in machine ${machineName}` + }, + webhook: { + topic: `Customer compliance`, + content: `Customer ${customer.phone} ${msgCore[action]} in machine ${machineName}` } } @@ -198,8 +203,11 @@ function complianceNotify (customer, deviceId, action, period) { notifications.sms.active && notifications.sms.compliance + const webhookActive = true + if (emailActive) promises.push(emailFuncs.sendMessage(settings, rec)) if (smsActive) promises.push(smsFuncs.sendMessage(settings, rec)) + if (webhookActive) promises.push(webhookFuncs.sendMessage(settings, rec)) notifyIfActive('compliance', 'customerComplianceNotify', customer, deviceId, action, period) @@ -220,6 +228,10 @@ function sendRedemptionMessage (txId, error) { email: { subject, body + }, + webhook: { + topic: `Transaction update`, + content: body } } return sendTransactionMessage(rec) @@ -241,6 +253,11 @@ function sendTransactionMessage (rec, isHighValueTx) { (notifications.sms.transactions || isHighValueTx) if (smsActive) promises.push(smsFuncs.sendMessage(settings, rec)) + // TODO: Webhook transaction notifications are dependent on notification settings, due to how transactionNotify() is programmed + // As changing it would require structural change to that function and the current behavior is temporary (webhooks will eventually have settings tied to them), it's not worth those changes right now + const webhookActive = true + if (webhookActive) promises.push(webhookFuncs.sendMessage(settings, rec)) + return Promise.all(promises) }) } @@ -259,6 +276,10 @@ function cashboxNotify (deviceId) { email: { subject: `Cashbox removal`, body: `Cashbox removed in machine ${machineName}` + }, + webhook: { + topic: `Cashbox removal`, + content: `Cashbox removed in machine ${machineName}` } } @@ -271,9 +292,12 @@ function cashboxNotify (deviceId) { const smsActive = notifications.sms.active && notifications.sms.security + + const webhookActive = true if (emailActive) promises.push(emailFuncs.sendMessage(settings, rec)) if (smsActive) promises.push(smsFuncs.sendMessage(settings, rec)) + if (webhookActive) promises.push(webhookFuncs.sendMessage(settings, rec)) notifyIfActive('security', 'cashboxNotify', deviceId) return Promise.all(promises) diff --git a/lib/notifier/utils.js b/lib/notifier/utils.js index edcea3df..d47d359b 100644 --- a/lib/notifier/utils.js +++ b/lib/notifier/utils.js @@ -132,6 +132,10 @@ const buildTransactionMessage = (tx, rec, highValueTx, machineName, customer) => email: { emailSubject, body + }, + webhook: { + topic: `New transaction`, + content: body } }, highValueTx] } diff --git a/lib/notifier/webhook.js b/lib/notifier/webhook.js new file mode 100644 index 00000000..fcddf1ed --- /dev/null +++ b/lib/notifier/webhook.js @@ -0,0 +1,21 @@ +const axios = require('axios') +const _ = require('lodash/fp') +const uuid = require('uuid') + +const WEBHOOK_URL = process.env.WEBHOOK_URL + +const sendMessage = (settings, rec) => { + if (_.isEmpty(WEBHOOK_URL)) return Promise.resolve() + + const body = _.merge(rec.webhook, { id: uuid.v4() }) + + return axios({ + method: 'POST', + url: WEBHOOK_URL, + data: body + }) +} + +module.exports = { + sendMessage +}