Chore: balancesNotify refactor

This commit is contained in:
Cesar 2020-12-17 22:18:35 +00:00 committed by Josh Harvey
parent bf8a40026e
commit 016bd12113
4 changed files with 35 additions and 48 deletions

View file

@ -385,72 +385,54 @@ const cashCassettesNotify = (cassettes, deviceId) => {
})
}
/*
Notes for new "valid" column on notifications table:
- We only want to add a new notification if it is present in the high or low warning consts.
- Before we add the notification we need to see if there is no "valid" notification in the database.
- Since the poller runs every few seconds, if the user marks it as read, this code would add a new notification
immediately. This new column helps us decide if a new notification should be added.
- "Valid" is defaulted to "true". When the cryptobalance goes over the low threshold or under the high threshold,
the notification will be marked as invalid. This will allow a new one to be sent. If the cryptobalance never goes
into the middle of the high and low thresholds, the old, "read" notification will still be relevant so we won't add
a new one.
*/
const clearOldCryptoNotifications = (balances) => {
// get valid crypto notifications from DB
// if that notification doesn't exist in balances, then make it invalid on the DB
queries.getAllValidNotifications('cryptoBalance').then(res => {
// first, for each DB notification, if it doesn't exist in balances then it is old and should not be valid anymore
// if it exists in balances, add the index of it in balances to the array of duplicates
// return the array of duplicates so that balancesNotify doesn't add them
return queries.getAllValidNotifications('cryptoBalance').then(res => {
const notifications = _.map(it => {
return {
id: it.id,
cryptoCode: it.detail.split('_')[0],
code: it.detail.split('_').splice(1).join('_')
}
}, res)
const duplicateIndexes = []
const idsToInvalidate = []
_.forEach(notification => {
const idx = _.findIndex(balance => {
return balance.code === notification.code && balance.cryptoCode === notification.cryptoCode
}, balances)
if(idx !== -1) {
return
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)
}
// if the notification doesn't exist in the new balances object, then it is outdated and is not valid anymore
return queries.invalidateNotification(notification.id)
}, notifications)
return (idsToInvalidate.length > 0 ? queries.batchInvalidate(idsToInvalidate) : Promise.resolve()).then(() => duplicateIndexes)
})
}
const balancesNotify = (balances) => {
const highFilter = o => o.code === 'HIGH_CRYPTO_BALANCE'
const lowFilter = o => o.code === 'LOW_CRYPTO_BALANCE'
const highWarnings = _.filter(highFilter, balances)
const lowWarnings = _.filter(lowFilter, balances)
const balancesFilter = o => o.code === 'HIGH_CRYPTO_BALANCE' || o.code === 'LOW_CRYPTO_BALANCE'
const balanceWarnings = _.filter(balancesFilter, balances)
clearOldCryptoNotifications(balances)
highWarnings.forEach(warning => {
queries.getValidNotifications('cryptoBalance', `${warning.cryptoCode}_${warning.code}`).then(res => {
if (res.length > 0) {
return Promise.resolve()
return clearOldCryptoNotifications(balanceWarnings).then(duplicateIndexes => {
return balanceWarnings.forEach((balance, idx) => {
if(duplicateIndexes.includes(idx)) {
return
}
console.log("Adding high balance alert for " + warning.cryptoCode + " - " + warning.fiatBalance.balance)
const balance = utils.formatCurrency(warning.fiatBalance.balance, warning.fiatCode)
return queries.addCryptoBalanceWarning(`${warning.cryptoCode}_${warning.code}`, `High balance in ${warning.cryptoCode} [${balance}]`)
const fiat = utils.formatCurrency(balance.fiatBalance.balance, balance.fiatCode)
const message = `${balance.code === 'HIGH_CRYPTO_BALANCE' ? 'High' : 'Low'} balance in ${balance.cryptoCode} [${fiat}]`
console.log(`Adding ${balance.code === 'HIGH_CRYPTO_BALANCE' ? 'high' : 'low'} balance notification for ${balance.cryptoCode}`)
return queries.addCryptoBalanceWarning(`${balance.cryptoCode}_${balance.code}`, message)
})
})
lowWarnings.forEach(warning => {
queries.getValidNotifications('cryptoBalance', `${warning.cryptoCode}_${warning.code}`).then(res => {
if (res.length > 0) {
return Promise.resolve()
}
console.log("Adding low balance alert for " + warning.cryptoCode + " - " + warning.fiatBalance.balance)
const balance = utils.formatCurrency(warning.fiatBalance.balance, warning.fiatCode)
return queries.addCryptoBalanceWarning(`${warning.cryptoCode}_${warning.code}`, `Low balance in ${warning.cryptoCode} [${balance}]`)
})
})
}).catch(console.error)
}
const clearOldErrorNotifications = (alerts) => {