lamassu-server/lib/notifier/sms.js
Cesar 3b3bdf839b Feat: crypto balance notifications saving in DB
Chore: add new column "detail" to transactions table migration

Feat: check if older notification is valid before sending new one

Feat: error saving to database

Fix: fix error when invalidating notification on
clearCryptoBalanceNotifications
Chre: code refactor in new-settings-loader for simplicity
Chore: refactor code on notifier and merge similar functions
2021-02-04 15:48:23 +00:00

67 lines
1.6 KiB
JavaScript

const _ = require('lodash/fp')
const utils = require('./utils')
const sms = require('../sms')
function printSmsAlerts(alertRec, config) {
let alerts = []
if (config.balance) {
alerts = _.concat(alerts, alertRec.general)
}
_.keys(alertRec.devices).forEach(function (device) {
if (config.balance) {
alerts = _.concat(alerts, alertRec.devices[device].balanceAlerts)
}
if (config.errors) {
alerts = _.concat(alerts, alertRec.devices[device].deviceAlerts)
}
})
if (alerts.length === 0) return null
const alertsMap = _.groupBy('code', alerts)
const alertTypes = _.map(entry => {
const code = entry[0]
const machineNames = _.filter(
_.negate(_.isEmpty),
_.map('machineName', entry[1]),
)
const cryptoCodes = _.filter(
_.negate(_.isEmpty),
_.map('cryptoCode', entry[1]),
)
return {
codeDisplay: utils.codeDisplay(code),
machineNames,
cryptoCodes
}
}, _.toPairs(alertsMap))
const mapByCodeDisplay = _.map(it => {
if(_.isEmpty(it.machineNames) && _.isEmpty(it.cryptoCodes)) {
return it.codeDisplay
}
if(_.isEmpty(it.machineNames)) {
return `${it.codeDisplay} (${it.cryptoCodes.join(', ')})`
}
else return `${it.codeDisplay} (${it.machineNames.join(', ')})`
})
const displayAlertTypes = _.compose(
_.uniq,
mapByCodeDisplay,
_.sortBy('codeDisplay')
)(alertTypes)
return '[Lamassu] Errors reported: ' + displayAlertTypes.join(', ')
}
const sendMessage = sms.sendMessage
module.exports = { printSmsAlerts, sendMessage }