Chore: fiatBalancesNotify refactor

Chore: removed now-unused code in some files
This commit is contained in:
Cesar 2020-12-17 23:30:58 +00:00 committed by Josh Harvey
parent 016bd12113
commit 82916388a0
2 changed files with 47 additions and 48 deletions

View file

@ -44,18 +44,6 @@ const configSql = 'insert into user_config (type, data, valid, schema_version) v
function saveConfig (config) {
return loadLatestConfigOrNone()
.then(currentConfig => {
if(config.notifications_cryptoHighBalance || config.notifications_cryptoLowBalance) {
// clearCryptoBalanceNotifications(currentConfig, config, false)
}
if(config.notifications_cryptoBalanceOverrides) {
// clearCryptoBalanceNotifications(currentConfig.notifications_cryptoBalanceOverrides, config.notifications_cryptoBalanceOverrides, true)
}
if(config.notifications_fiatBalanceCassette1 || config.notifications_fiatBalanceCassette2) {
clearCassetteNotifications(currentConfig, config, false)
}
if(config.notifications_fiatBalanceOverrides) {
clearCassetteNotifications(currentConfig.notifications_fiatBalanceOverrides, config.notifications_fiatBalanceOverrides, true)
}
const newConfig = _.assign(currentConfig, config)
return db.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
})

View file

@ -356,35 +356,6 @@ const customerComplianceNotify = (customer, deviceId, code, days = null) => {
.catch(console.error)
}
const cashCassettesNotify = (cassettes, deviceId) => {
settingsLoader.loadLatest()
.then(settings =>
[
configManager.getNotifications(null, deviceId, settings.config),
configManager.getCashOut(deviceId,settings.config).active
])
.then(([notifications, cashOutEnabled]) => {
const cassette1Count = cassettes.cassette1
const cassette2Count = cassettes.cassette2
const cassette1Threshold = notifications.fiatBalanceCassette1
const cassette2Threshold = notifications.fiatBalanceCassette2
if(cashOutEnabled) {
// we only want to add this notification if there isn't one already set and unread in the database
Promise.all([queries.getUnreadCassetteNotifications(1, deviceId), queries.getUnreadCassetteNotifications(2, deviceId)]).then(res => {
if(res[0].length === 0 && cassette1Count < cassette1Threshold) {
console.log("Adding fiatBalance alert for cashbox 1 in database - count & threshold: ", cassette1Count, cassette1Threshold )
return queries.addCashCassetteWarning(1, deviceId)
}
if(res[1].length === 0 && cassette2Count < cassette2Threshold) {
console.log("Adding fiatBalance alert for cashbox 2 in database - count & threshold: ", cassette2Count, cassette2Threshold )
return queries.addCashCassetteWarning(2, deviceId)
}
})
}
})
}
const clearOldCryptoNotifications = (balances) => {
// get valid crypto notifications from DB
// first, for each DB notification, if it doesn't exist in balances then it is old and should not be valid anymore
@ -418,12 +389,9 @@ const clearOldCryptoNotifications = (balances) => {
})
}
const balancesNotify = (balances) => {
const balancesFilter = o => o.code === 'HIGH_CRYPTO_BALANCE' || o.code === 'LOW_CRYPTO_BALANCE'
const balanceWarnings = _.filter(balancesFilter, balances)
return clearOldCryptoNotifications(balanceWarnings).then(duplicateIndexes => {
return balanceWarnings.forEach((balance, idx) => {
const cryptoBalancesNotify = (cryptoWarnings) => {
return clearOldCryptoNotifications(cryptoWarnings).then(duplicateIndexes => {
return cryptoWarnings.forEach((balance, idx) => {
if(duplicateIndexes.includes(idx)) {
return
}
@ -432,7 +400,50 @@ const balancesNotify = (balances) => {
console.log(`Adding ${balance.code === 'HIGH_CRYPTO_BALANCE' ? 'high' : 'low'} balance notification for ${balance.cryptoCode}`)
return queries.addCryptoBalanceWarning(`${balance.cryptoCode}_${balance.code}`, message)
})
}).catch(console.error)
})
}
const clearOldFiatNotifications = (balances) => {
return queries.getAllValidNotifications('fiatBalance').then(notifications => {
const duplicateIndexes = []
const idsToInvalidate = []
_.forEach(notification => {
const idx = _.findIndex(balance => {
return notification.device_id === balance.deviceId && notification.detail === `${balance.cassette}`
}, balances)
if (idx === -1) {
// if notification in DB doesnt exist in balances anymore then it is invalid now
idsToInvalidate.push(notification.id)
}
else {
// if it exists then it is a duplicate, add it to array
duplicateIndexes.push(idx)
}
}, notifications)
return (idsToInvalidate.length > 0 ? queries.batchInvalidate(idsToInvalidate) : Promise.resolve()).then(() => duplicateIndexes)
})
}
const fiatBalancesNotify = (fiatWarnings) => {
return clearOldFiatNotifications(fiatWarnings).then(duplicateIndexes => {
return fiatWarnings.forEach((balance, idx) => {
if(duplicateIndexes.includes(idx)) {
return
}
console.log(`Adding low cash balance notification for cassette ${balance.cassette} at ${balance.machineName}`)
const message = `Cash-out cassette ${balance.cassette} almost empty!`
return queries.addCashCassetteWarning(balance.cassette, balance.deviceId, message)
})
})
}
const balancesNotify = (balances) => {
const cryptoFilter = o => o.code === 'HIGH_CRYPTO_BALANCE' || o.code === 'LOW_CRYPTO_BALANCE'
const fiatFilter = o => o.code === 'LOW_CASH_OUT'
const cryptoWarnings = _.filter(cryptoFilter, balances)
const fiatWarnings = _.filter(fiatFilter, balances)
return Promise.all([cryptoBalancesNotify(cryptoWarnings), fiatBalancesNotify(fiatWarnings)]).catch(console.error)
}
const clearOldErrorNotifications = (alerts) => {