chore: server code formatting
This commit is contained in:
parent
aedabcbdee
commit
68517170e2
234 changed files with 9824 additions and 6195 deletions
|
|
@ -13,48 +13,52 @@ const smsFuncs = require('./sms')
|
|||
const webhookFuncs = require('./webhook')
|
||||
const { STALE, STALE_STATE } = require('./codes')
|
||||
|
||||
function buildMessage (alerts, notifications) {
|
||||
function buildMessage(alerts, notifications) {
|
||||
const smsEnabled = utils.isActive(notifications.sms)
|
||||
const emailEnabled = utils.isActive(notifications.email)
|
||||
|
||||
let rec = {}
|
||||
if (smsEnabled) {
|
||||
rec = _.set(['sms', 'body'])(
|
||||
smsFuncs.printSmsAlerts(alerts, notifications.sms)
|
||||
smsFuncs.printSmsAlerts(alerts, notifications.sms),
|
||||
)(rec)
|
||||
}
|
||||
if (emailEnabled) {
|
||||
rec = _.set(['email', 'subject'])(
|
||||
emailFuncs.alertSubject(alerts, notifications.email)
|
||||
emailFuncs.alertSubject(alerts, notifications.email),
|
||||
)(rec)
|
||||
rec = _.set(['email', 'body'])(
|
||||
emailFuncs.printEmailAlerts(alerts, notifications.email)
|
||||
emailFuncs.printEmailAlerts(alerts, notifications.email),
|
||||
)(rec)
|
||||
}
|
||||
|
||||
return rec
|
||||
}
|
||||
|
||||
function checkNotification (plugins) {
|
||||
function checkNotification(plugins) {
|
||||
const notifications = plugins.getNotificationConfig()
|
||||
const smsEnabled = utils.isActive(notifications.sms)
|
||||
const emailEnabled = utils.isActive(notifications.email)
|
||||
const notificationCenterEnabled = utils.isActive(notifications.notificationCenter)
|
||||
const notificationCenterEnabled = utils.isActive(
|
||||
notifications.notificationCenter,
|
||||
)
|
||||
|
||||
if (!(notificationCenterEnabled || smsEnabled || emailEnabled)) return Promise.resolve()
|
||||
if (!(notificationCenterEnabled || smsEnabled || emailEnabled))
|
||||
return Promise.resolve()
|
||||
|
||||
return getAlerts(plugins)
|
||||
.then(alerts => {
|
||||
notifyIfActive('errors', 'errorAlertsNotify', alerts)
|
||||
const currentAlertFingerprint = utils.buildAlertFingerprint(
|
||||
alerts,
|
||||
notifications
|
||||
notifications,
|
||||
)
|
||||
if (!currentAlertFingerprint) {
|
||||
const inAlert = !!utils.getAlertFingerprint()
|
||||
// variables for setAlertFingerprint: (fingerprint = null, lastAlertTime = null)
|
||||
utils.setAlertFingerprint(null, null)
|
||||
if (inAlert) return utils.sendNoAlerts(plugins, smsEnabled, emailEnabled)
|
||||
if (inAlert)
|
||||
return utils.sendNoAlerts(plugins, smsEnabled, emailEnabled)
|
||||
}
|
||||
if (utils.shouldNotAlert(currentAlertFingerprint)) return
|
||||
|
||||
|
|
@ -70,18 +74,18 @@ function checkNotification (plugins) {
|
|||
.catch(logger.error)
|
||||
}
|
||||
|
||||
function getAlerts (plugins) {
|
||||
function getAlerts(plugins) {
|
||||
return Promise.all([
|
||||
plugins.checkBalances(),
|
||||
queries.machineEvents(),
|
||||
plugins.getMachineNames()
|
||||
plugins.getMachineNames(),
|
||||
]).then(([balances, events, devices]) => {
|
||||
notifyIfActive('balance', 'balancesNotify', balances)
|
||||
return buildAlerts(checkPings(devices), balances, events, devices)
|
||||
})
|
||||
}
|
||||
|
||||
function buildAlerts (pings, balances, events, devices) {
|
||||
function buildAlerts(pings, balances, events, devices) {
|
||||
const alerts = { devices: {}, deviceNames: {} }
|
||||
alerts.general = _.filter(r => !r.deviceId, balances)
|
||||
_.forEach(device => {
|
||||
|
|
@ -89,10 +93,11 @@ function buildAlerts (pings, balances, events, devices) {
|
|||
const ping = pings[deviceId] || []
|
||||
const stuckScreen = checkStuckScreen(events, device)
|
||||
|
||||
alerts.devices = _.set([deviceId, 'balanceAlerts'], _.filter(
|
||||
['deviceId', deviceId],
|
||||
balances
|
||||
), alerts.devices)
|
||||
alerts.devices = _.set(
|
||||
[deviceId, 'balanceAlerts'],
|
||||
_.filter(['deviceId', deviceId], balances),
|
||||
alerts.devices,
|
||||
)
|
||||
alerts.devices[deviceId].deviceAlerts = _.isEmpty(ping) ? stuckScreen : ping
|
||||
|
||||
alerts.deviceNames[deviceId] = device.name
|
||||
|
|
@ -101,18 +106,18 @@ function buildAlerts (pings, balances, events, devices) {
|
|||
return alerts
|
||||
}
|
||||
|
||||
function checkPings (devices) {
|
||||
function checkPings(devices) {
|
||||
const deviceIds = _.map('deviceId', devices)
|
||||
const pings = _.map(utils.checkPing, devices)
|
||||
return _.zipObject(deviceIds)(pings)
|
||||
}
|
||||
|
||||
function checkStuckScreen (deviceEvents, machine) {
|
||||
function checkStuckScreen(deviceEvents, machine) {
|
||||
const lastEvent = _.pipe(
|
||||
_.filter(e => e.device_id === machine.deviceId),
|
||||
_.sortBy(utils.getDeviceTime),
|
||||
_.map(utils.parseEventNote),
|
||||
_.last
|
||||
_.last,
|
||||
)(deviceEvents)
|
||||
|
||||
if (!lastEvent) return []
|
||||
|
|
@ -129,115 +134,152 @@ function checkStuckScreen (deviceEvents, machine) {
|
|||
return []
|
||||
}
|
||||
|
||||
function transactionNotify (tx, rec) {
|
||||
function transactionNotify(tx, rec) {
|
||||
return settingsLoader.loadLatestConfig().then(config => {
|
||||
const notifSettings = configManager.getGlobalNotifications(config)
|
||||
const highValueTx = tx.fiat.gt(notifSettings.highValueTransaction || Infinity)
|
||||
const highValueTx = tx.fiat.gt(
|
||||
notifSettings.highValueTransaction || Infinity,
|
||||
)
|
||||
const isCashOut = tx.direction === 'cashOut'
|
||||
|
||||
// for notification center
|
||||
const directionDisplay = isCashOut ? 'cash-out' : 'cash-in'
|
||||
const readyToNotify = !isCashOut || (tx.direction === 'cashOut' && rec.isRedemption)
|
||||
const readyToNotify =
|
||||
!isCashOut || (tx.direction === 'cashOut' && rec.isRedemption)
|
||||
// awaiting for redesign. notification should not be sent if toggle in the settings table is disabled,
|
||||
// but currently we're sending notifications of high value tx even with the toggle disabled
|
||||
if (readyToNotify && !highValueTx) {
|
||||
notifyIfActive('transactions', 'notifCenterTransactionNotify', highValueTx, directionDisplay, tx.fiat, tx.fiatCode, tx.deviceId, tx.toAddress)
|
||||
notifyIfActive(
|
||||
'transactions',
|
||||
'notifCenterTransactionNotify',
|
||||
highValueTx,
|
||||
directionDisplay,
|
||||
tx.fiat,
|
||||
tx.fiatCode,
|
||||
tx.deviceId,
|
||||
tx.toAddress,
|
||||
)
|
||||
} else if (readyToNotify && highValueTx) {
|
||||
notificationCenter.notifCenterTransactionNotify(highValueTx, directionDisplay, tx.fiat, tx.fiatCode, tx.deviceId, tx.toAddress)
|
||||
notificationCenter.notifCenterTransactionNotify(
|
||||
highValueTx,
|
||||
directionDisplay,
|
||||
tx.fiat,
|
||||
tx.fiatCode,
|
||||
tx.deviceId,
|
||||
tx.toAddress,
|
||||
)
|
||||
}
|
||||
|
||||
// alert through sms or email any transaction or high value transaction, if SMS || email alerts are enabled
|
||||
const walletSettings = configManager.getWalletSettings(tx.cryptoCode, config)
|
||||
const walletSettings = configManager.getWalletSettings(
|
||||
tx.cryptoCode,
|
||||
config,
|
||||
)
|
||||
const zeroConfLimit = walletSettings.zeroConfLimit || 0
|
||||
const zeroConf = isCashOut && tx.fiat.lte(zeroConfLimit)
|
||||
const notificationsEnabled = notifSettings.sms.transactions || notifSettings.email.transactions
|
||||
const customerPromise = tx.customerId ? customers.getById(tx.customerId) : Promise.resolve({})
|
||||
const notificationsEnabled =
|
||||
notifSettings.sms.transactions || notifSettings.email.transactions
|
||||
const customerPromise = tx.customerId
|
||||
? customers.getById(tx.customerId)
|
||||
: Promise.resolve({})
|
||||
|
||||
if (!notificationsEnabled && !highValueTx) return Promise.resolve()
|
||||
if (zeroConf && isCashOut && !rec.isRedemption && !rec.error) return Promise.resolve()
|
||||
if (!zeroConf && rec.isRedemption) return sendRedemptionMessage(tx.id, rec.error)
|
||||
if (zeroConf && isCashOut && !rec.isRedemption && !rec.error)
|
||||
return Promise.resolve()
|
||||
if (!zeroConf && rec.isRedemption)
|
||||
return sendRedemptionMessage(tx.id, rec.error)
|
||||
|
||||
return Promise.all([
|
||||
queries.getMachineName(tx.deviceId),
|
||||
customerPromise
|
||||
]).then(([machineName, customer]) => {
|
||||
return utils.buildTransactionMessage(tx, rec, highValueTx, machineName, customer)
|
||||
}).then(([msg, highValueTx]) => sendTransactionMessage(msg, highValueTx))
|
||||
return Promise.all([queries.getMachineName(tx.deviceId), customerPromise])
|
||||
.then(([machineName, customer]) => {
|
||||
return utils.buildTransactionMessage(
|
||||
tx,
|
||||
rec,
|
||||
highValueTx,
|
||||
machineName,
|
||||
customer,
|
||||
)
|
||||
})
|
||||
.then(([msg, highValueTx]) => sendTransactionMessage(msg, highValueTx))
|
||||
})
|
||||
}
|
||||
|
||||
function complianceNotify (settings, customer, deviceId, action, period) {
|
||||
const timestamp = (new Date()).toLocaleString()
|
||||
return queries.getMachineName(deviceId)
|
||||
.then(machineName => {
|
||||
const notifications = configManager.getGlobalNotifications(settings.config)
|
||||
function complianceNotify(settings, customer, deviceId, action, period) {
|
||||
const timestamp = new Date().toLocaleString()
|
||||
return queries.getMachineName(deviceId).then(machineName => {
|
||||
const notifications = configManager.getGlobalNotifications(settings.config)
|
||||
|
||||
const msgCore = {
|
||||
BLOCKED: `was blocked`,
|
||||
SUSPENDED: `was suspended for ${!!period && period} days`,
|
||||
PENDING_COMPLIANCE: `is waiting for your manual approval`,
|
||||
}
|
||||
const msgCore = {
|
||||
BLOCKED: `was blocked`,
|
||||
SUSPENDED: `was suspended for ${!!period && period} days`,
|
||||
PENDING_COMPLIANCE: `is waiting for your manual approval`,
|
||||
}
|
||||
|
||||
const rec = {
|
||||
sms: {
|
||||
body: `Customer ${customer.phone} ${msgCore[action]} - ${machineName}. ${timestamp}`
|
||||
},
|
||||
email: {
|
||||
subject: `Customer compliance`,
|
||||
body: `Customer ${customer.phone} ${msgCore[action]} in machine ${machineName}. ${timestamp}`
|
||||
},
|
||||
webhook: {
|
||||
topic: `Customer compliance`,
|
||||
content: `Customer ${customer.phone} ${msgCore[action]} in machine ${machineName}. ${timestamp}`
|
||||
}
|
||||
}
|
||||
const rec = {
|
||||
sms: {
|
||||
body: `Customer ${customer.phone} ${msgCore[action]} - ${machineName}. ${timestamp}`,
|
||||
},
|
||||
email: {
|
||||
subject: `Customer compliance`,
|
||||
body: `Customer ${customer.phone} ${msgCore[action]} in machine ${machineName}. ${timestamp}`,
|
||||
},
|
||||
webhook: {
|
||||
topic: `Customer compliance`,
|
||||
content: `Customer ${customer.phone} ${msgCore[action]} in machine ${machineName}. ${timestamp}`,
|
||||
},
|
||||
}
|
||||
|
||||
const promises = []
|
||||
const promises = []
|
||||
|
||||
const emailActive =
|
||||
notifications.email.active &&
|
||||
notifications.email.compliance
|
||||
const emailActive =
|
||||
notifications.email.active && notifications.email.compliance
|
||||
|
||||
const smsActive =
|
||||
notifications.sms.active &&
|
||||
notifications.sms.compliance
|
||||
const smsActive = notifications.sms.active && notifications.sms.compliance
|
||||
|
||||
const webhookActive = true
|
||||
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))
|
||||
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, machineName, period)
|
||||
notifyIfActive(
|
||||
'compliance',
|
||||
'customerComplianceNotify',
|
||||
customer,
|
||||
deviceId,
|
||||
action,
|
||||
machineName,
|
||||
period,
|
||||
)
|
||||
|
||||
return Promise.all(promises)
|
||||
.catch(err => console.error(`An error occurred when sending a notification. Please check your notification preferences and 3rd party account configuration: ${err.stack}`))
|
||||
})
|
||||
return Promise.all(promises).catch(err =>
|
||||
console.error(
|
||||
`An error occurred when sending a notification. Please check your notification preferences and 3rd party account configuration: ${err.stack}`,
|
||||
),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
function sendRedemptionMessage (txId, error) {
|
||||
function sendRedemptionMessage(txId, error) {
|
||||
const subject = `Here's an update on transaction ${txId}`
|
||||
const body = error
|
||||
? `Error: ${error}`
|
||||
: 'It was just dispensed successfully'
|
||||
const body = error ? `Error: ${error}` : 'It was just dispensed successfully'
|
||||
|
||||
const rec = {
|
||||
sms: {
|
||||
body: `${subject} - ${body}`
|
||||
body: `${subject} - ${body}`,
|
||||
},
|
||||
email: {
|
||||
subject,
|
||||
body
|
||||
body,
|
||||
},
|
||||
webhook: {
|
||||
topic: `Transaction update`,
|
||||
content: body
|
||||
}
|
||||
content: body,
|
||||
},
|
||||
}
|
||||
return sendTransactionMessage(rec)
|
||||
}
|
||||
|
||||
function sendTransactionMessage (rec, isHighValueTx) {
|
||||
function sendTransactionMessage(rec, isHighValueTx) {
|
||||
return settingsLoader.loadLatest().then(settings => {
|
||||
const notifications = configManager.getGlobalNotifications(settings.config)
|
||||
|
||||
|
|
@ -258,62 +300,74 @@ function sendTransactionMessage (rec, isHighValueTx) {
|
|||
const webhookActive = true
|
||||
if (webhookActive) promises.push(webhookFuncs.sendMessage(settings, rec))
|
||||
|
||||
return Promise.all(promises)
|
||||
.catch(err => console.error(`An error occurred when sending a notification. Please check your notification preferences and 3rd party account configuration: ${err.stack}`))
|
||||
return Promise.all(promises).catch(err =>
|
||||
console.error(
|
||||
`An error occurred when sending a notification. Please check your notification preferences and 3rd party account configuration: ${err.stack}`,
|
||||
),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
function cashboxNotify (deviceId) {
|
||||
function cashboxNotify(deviceId) {
|
||||
return Promise.all([
|
||||
settingsLoader.loadLatest(),
|
||||
queries.getMachineName(deviceId)
|
||||
])
|
||||
.then(([settings, machineName]) => {
|
||||
const notifications = configManager.getGlobalNotifications(settings.config)
|
||||
const rec = {
|
||||
sms: {
|
||||
body: `Cashbox removed - ${machineName}`
|
||||
},
|
||||
email: {
|
||||
subject: `Cashbox removal`,
|
||||
body: `Cashbox removed in machine ${machineName}`
|
||||
},
|
||||
webhook: {
|
||||
topic: `Cashbox removal`,
|
||||
content: `Cashbox removed in machine ${machineName}`
|
||||
}
|
||||
}
|
||||
queries.getMachineName(deviceId),
|
||||
]).then(([settings, machineName]) => {
|
||||
const notifications = configManager.getGlobalNotifications(settings.config)
|
||||
const rec = {
|
||||
sms: {
|
||||
body: `Cashbox removed - ${machineName}`,
|
||||
},
|
||||
email: {
|
||||
subject: `Cashbox removal`,
|
||||
body: `Cashbox removed in machine ${machineName}`,
|
||||
},
|
||||
webhook: {
|
||||
topic: `Cashbox removal`,
|
||||
content: `Cashbox removed in machine ${machineName}`,
|
||||
},
|
||||
}
|
||||
|
||||
const promises = []
|
||||
const promises = []
|
||||
|
||||
const emailActive =
|
||||
notifications.email.active &&
|
||||
notifications.email.security
|
||||
const emailActive =
|
||||
notifications.email.active && notifications.email.security
|
||||
|
||||
const smsActive =
|
||||
notifications.sms.active &&
|
||||
notifications.sms.security
|
||||
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)
|
||||
const webhookActive = true
|
||||
|
||||
return Promise.all(promises)
|
||||
.catch(err => console.error(`An error occurred when sending a notification. Please check your notification preferences and 3rd party account configuration: ${err.stack}`))
|
||||
})
|
||||
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).catch(err =>
|
||||
console.error(
|
||||
`An error occurred when sending a notification. Please check your notification preferences and 3rd party account configuration: ${err.stack}`,
|
||||
),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// for notification center, check if type of notification is active before calling the respective notify function
|
||||
const notifyIfActive = (type, fnName, ...args) => {
|
||||
return settingsLoader.loadLatestConfig().then(config => {
|
||||
const notificationSettings = configManager.getGlobalNotifications(config).notificationCenter
|
||||
if (!notificationCenter[fnName]) return Promise.reject(new Error(`Notification function ${fnName} for type ${type} does not exist`))
|
||||
if (!(notificationSettings.active && notificationSettings[type])) return Promise.resolve()
|
||||
return notificationCenter[fnName](...args)
|
||||
}).catch(logger.error)
|
||||
return settingsLoader
|
||||
.loadLatestConfig()
|
||||
.then(config => {
|
||||
const notificationSettings =
|
||||
configManager.getGlobalNotifications(config).notificationCenter
|
||||
if (!notificationCenter[fnName])
|
||||
return Promise.reject(
|
||||
new Error(
|
||||
`Notification function ${fnName} for type ${type} does not exist`,
|
||||
),
|
||||
)
|
||||
if (!(notificationSettings.active && notificationSettings[type]))
|
||||
return Promise.resolve()
|
||||
return notificationCenter[fnName](...args)
|
||||
})
|
||||
.catch(logger.error)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
@ -324,5 +378,5 @@ module.exports = {
|
|||
checkStuckScreen,
|
||||
sendRedemptionMessage,
|
||||
cashboxNotify,
|
||||
notifyIfActive
|
||||
notifyIfActive,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue