Feat: add notification center row in notification settings table
This commit is contained in:
parent
1ab4b68168
commit
34f2b84fe2
8 changed files with 109 additions and 69 deletions
|
|
@ -2,7 +2,6 @@ const _ = require('lodash/fp')
|
|||
|
||||
const configManager = require('../new-config-manager')
|
||||
const logger = require('../logger')
|
||||
const machineLoader = require('../machine-loader')
|
||||
const queries = require('./queries')
|
||||
const settingsLoader = require('../new-settings-loader')
|
||||
const customers = require('../customers')
|
||||
|
|
@ -10,14 +9,17 @@ const customers = require('../customers')
|
|||
const utils = require('./utils')
|
||||
const emailFuncs = require('./email')
|
||||
const smsFuncs = require('./sms')
|
||||
const codes = require('./codes')
|
||||
const { STALE, STALE_STATE, PING } = require('./codes')
|
||||
|
||||
const { NOTIFICATION_TYPES: {
|
||||
HIGH_VALUE_TX,
|
||||
NORMAL_VALUE_TX,
|
||||
FIAT_BALANCE,
|
||||
CRYPTO_BALANCE,
|
||||
COMPLIANCE,
|
||||
ERROR }
|
||||
} = require('./codes')
|
||||
} = codes
|
||||
|
||||
function buildMessage (alerts, notifications) {
|
||||
const smsEnabled = utils.isActive(notifications.sms)
|
||||
|
|
@ -50,7 +52,7 @@ function checkNotification (plugins) {
|
|||
|
||||
return getAlerts(plugins)
|
||||
.then(alerts => {
|
||||
errorAlertsNotify(alerts)
|
||||
notifyIfActive('errors', alerts).catch(console.error)
|
||||
const currentAlertFingerprint = utils.buildAlertFingerprint(
|
||||
alerts,
|
||||
notifications
|
||||
|
|
@ -81,7 +83,7 @@ function getAlerts (plugins) {
|
|||
queries.machineEvents(),
|
||||
plugins.getMachineNames()
|
||||
]).then(([balances, events, devices]) => {
|
||||
balancesNotify(balances)
|
||||
notifyIfActive('balance', balances).catch(console.error)
|
||||
return buildAlerts(checkPings(devices), balances, events, devices)
|
||||
})
|
||||
}
|
||||
|
|
@ -136,17 +138,24 @@ function checkStuckScreen (deviceEvents, machineName) {
|
|||
return []
|
||||
}
|
||||
|
||||
function notifCenterTransactionNotify (isHighValue, direction, fiat, fiatCode, deviceId, cryptoAddress) {
|
||||
const messageSuffix = isHighValue ? 'High value' : ''
|
||||
const message = `${messageSuffix} ${fiat} ${fiatCode} ${direction} transaction`
|
||||
const detailB = utils.buildDetail({ deviceId: deviceId, direction, fiat, fiatCode, cryptoAddress })
|
||||
return queries.addNotification(isHighValue ? HIGH_VALUE_TX : NORMAL_VALUE_TX, message, detailB)
|
||||
}
|
||||
|
||||
function transactionNotify (tx, rec) {
|
||||
return settingsLoader.loadLatest().then(settings => {
|
||||
const notifSettings = configManager.getGlobalNotifications(settings.config)
|
||||
const highValueTx = tx.fiat.gt(notifSettings.highValueTransaction || Infinity)
|
||||
const isCashOut = tx.direction === 'cashOut'
|
||||
// high value tx on database
|
||||
if (highValueTx && (tx.direction === 'cashIn' || (tx.direction === 'cashOut' && rec.isRedemption))) {
|
||||
const direction = tx.direction === 'cashOut' ? 'cash-out' : 'cash-in'
|
||||
const message = `${tx.fiat} ${tx.fiatCode} ${direction} transaction`
|
||||
const detailB = utils.buildDetail({ deviceId: tx.deviceId, direction, fiat: tx.fiat, fiatCode: tx.fiatCode, cryptoAddress: tx.toAddress })
|
||||
queries.addNotification(HIGH_VALUE_TX, message, detailB)
|
||||
|
||||
// for notification center
|
||||
const directionDisplay = tx.direction === 'cashOut' ? 'cash-out' : 'cash-in'
|
||||
const readyToNotify = tx.direction === 'cashIn' || (tx.direction === 'cashOut' && rec.isRedemption)
|
||||
if (readyToNotify) {
|
||||
notifyIfActive('transactions', highValueTx, directionDisplay, tx.fiat, tx.fiatCode, tx.deviceId, tx.toAddress).catch(console.error)
|
||||
}
|
||||
|
||||
// alert through sms or email any transaction or high value transaction, if SMS || email alerts are enabled
|
||||
|
|
@ -161,7 +170,7 @@ function transactionNotify (tx, rec) {
|
|||
if (!zeroConf && rec.isRedemption) return sendRedemptionMessage(tx.id, rec.error)
|
||||
|
||||
return Promise.all([
|
||||
machineLoader.getMachineName(tx.deviceId),
|
||||
queries.getMachineName(tx.deviceId),
|
||||
customerPromise
|
||||
]).then(([machineName, customer]) => {
|
||||
return utils.buildTransactionMessage(tx, rec, highValueTx, machineName, customer)
|
||||
|
|
@ -356,6 +365,23 @@ const customerComplianceNotify = (customer, deviceId, code, days = null) => {
|
|||
.catch(console.error)
|
||||
}
|
||||
|
||||
const notificationCenterFunctions = {
|
||||
'compliance': customerComplianceNotify,
|
||||
'balance': balancesNotify,
|
||||
'errors': errorAlertsNotify,
|
||||
'transactions': notifCenterTransactionNotify
|
||||
}
|
||||
|
||||
// for notification center, check if type of notification is active before calling the respective notify function
|
||||
const notifyIfActive = (type, ...args) => {
|
||||
return settingsLoader.loadLatest().then(settings => {
|
||||
const notificationSettings = configManager.getGlobalNotifications(settings.config).notificationCenter
|
||||
if (!notificationCenterFunctions[type]) return Promise.reject(new Error(`Notification of type ${type} does not exist`))
|
||||
if (!(notificationSettings.active && notificationSettings[type])) return Promise.resolve()
|
||||
return notificationCenterFunctions[type](...args)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
transactionNotify,
|
||||
checkNotification,
|
||||
|
|
@ -363,6 +389,6 @@ module.exports = {
|
|||
checkStuckScreen,
|
||||
sendRedemptionMessage,
|
||||
blacklistNotify,
|
||||
customerComplianceNotify,
|
||||
clearBlacklistNotification
|
||||
clearBlacklistNotification,
|
||||
notifyIfActive
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue