feat: backport webhook firehose

This commit is contained in:
Sérgio Salgado 2022-10-13 19:25:29 +01:00
parent df5bc81700
commit 45a11f91f0
4 changed files with 50 additions and 0 deletions

View file

@ -57,3 +57,4 @@ HTTP=
DEV_MODE=
## Uncategorized variables
WEBHOOK_URL=

View file

@ -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)

View file

@ -132,6 +132,10 @@ const buildTransactionMessage = (tx, rec, highValueTx, machineName, customer) =>
email: {
emailSubject,
body
},
webhook: {
topic: `New transaction`,
content: body
}
}, highValueTx]
}

21
lib/notifier/webhook.js Normal file
View file

@ -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
}