Chore: post-rebase fixes

This commit is contained in:
csrapr 2021-02-05 00:05:50 +00:00 committed by Josh Harvey
parent c0106592eb
commit ef60b15d82
29 changed files with 224 additions and 470 deletions

View file

@ -356,207 +356,6 @@ const customerComplianceNotify = (customer, deviceId, code, days = null) => {
.catch(console.error)
}
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
// 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(CRYPTO_BALANCE).then(res => {
const notifications = _.map(it => {
return {
id: it.id,
cryptoCode: it.detail.cryptoCode,
code: it.detail.code
}
}, res)
const duplicateIndexes = []
const idsToInvalidate = []
_.forEach(notification => {
const idx = _.findIndex(balance => {
return balance.code === notification.code && balance.cryptoCode === notification.cryptoCode
}, 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 cryptoBalancesNotify = (cryptoWarnings) => {
return clearOldCryptoNotifications(cryptoWarnings).then(duplicateIndexes => {
return cryptoWarnings.forEach((balance, idx) => {
if(duplicateIndexes.includes(idx)) {
return
}
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}`)
const detailB = utils.buildDetail({cryptoCode: balance.cryptoCode, code: balance.code})
return queries.addNotification(CRYPTO_BALANCE, message, detailB)
})
})
}
const clearOldFiatNotifications = (balances) => {
return queries.getAllValidNotifications(FIAT_BALANCE).then(notifications => {
const duplicateIndexes = []
const idsToInvalidate = []
_.forEach(notification => {
const idx = _.findIndex(balance => {
return notification.detail.deviceId === balance.deviceId && notification.detail.cassette === 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!`
const detailB = utils.buildDetail({deviceId: balance.deviceId, cassette: balance.cassette})
return queries.addNotification(FIAT_BALANCE, message, detailB)
})
})
}
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) => {
return queries.getAllValidNotifications(ERROR).then(res => {
const indexesToInvalidate = []
_.forEach(notification => {
const idx = _.findIndex(alert => {
return alert.code === notification.detail.code && alert.deviceId === notification.detail.deviceId
}, alerts)
if(idx !== -1) {
return
}
// if the notification doesn't exist, then it is outdated and is not valid anymore
indexesToInvalidate.push(notification.id)
}, res)
return indexesToInvalidate.length > 0 ? queries.batchInvalidate(indexesToInvalidate) : null
}).catch(console.log)
}
const errorAlertsNotify = (alertRec) => {
let alerts = []
_.keys(alertRec.devices).forEach(function (device) {
// embed device ID in each alert object inside the deviceAlerts array
alertRec.devices[device].deviceAlerts = _.map(alert => {
return {...alert, deviceId: device}
}, alertRec.devices[device].deviceAlerts)
// concat every array into one
alerts = _.concat(alerts, alertRec.devices[device].deviceAlerts)
})
// now that we have all the alerts, we want to add PING and STALE alerts to the DB
// if there is a valid alert on the DB that doesn't exist on the new alerts array,
// that alert should be considered invalid
// after that, for the alerts array, we have to see if there is a valid alert of
// the sorts already on the DB
return clearOldErrorNotifications(alerts).then(() => {
_.forEach(alert => {
switch(alert.code) {
case PING: {
const detailB = utils.buildDetail({code: PING, age: alert.age ? alert.age : -1, deviceId: alert.deviceId})
return queries.getValidNotifications(ERROR, _.omit(['age'], detailB)).then(res => {
if(res.length > 0) {
return Promise.resolve()
}
console.log("Adding PING alert on database for " + alert.machineName)
const message = `Machine down`
return queries.addNotification(ERROR, message, detailB)
})
}
case STALE: {
const detailB = utils.buildDetail({code: STALE, deviceId: alert.deviceId})
return queries.getValidNotifications(ERROR, detailB).then(res => {
if(res.length > 0) {
return Promise.resolve()
}
console.log("Adding STALE alert on database for " + alert.machineName)
const message = `Machine is stuck on ${alert.state} screen`
return queries.addNotification(ERROR, message, detailB)
})
}
default:
return
}
}, alerts)
}).catch(console.error)
}
const blacklistNotify = (tx, isAddressReuse) => {
let message = ''
let detailB = {}
if(isAddressReuse) {
detail = `${tx.cryptoCode}_REUSED_${tx.toAddress}`
detailB = utils.buildDetail({cryptoCode: tx.cryptoCode, code: 'REUSED', cryptoAddress: tx.toAddress})
message = `Blocked reused address: ${tx.cryptoCode} ${tx.toAddress.substr(0,10)}...`
} else {
detail = `${tx.cryptoCode}_BLOCKED_${tx.toAddress}`
detailB = utils.buildDetail({cryptoCode: tx.cryptoCode, code: 'BLOCKED', cryptoAddress: tx.toAddress})
message = `Blocked blacklisted address: ${tx.cryptoCode} ${tx.toAddress.substr(0,10)}...`
}
return queries.addNotification(COMPLIANCE, message, detailB)
}
const clearBlacklistNotification = (cryptoCode, cryptoAddress) => {
return queries.clearBlacklistNotification(cryptoCode, cryptoAddress).catch(console.error)
}
const clearOldCustomerSuspendedNotifications = (customerId, deviceId) => {
const detailB = utils.buildDetail({code: 'SUSPENDED', customerId, deviceId})
return queries.invalidateNotification(detailB, 'compliance')
}
const customerComplianceNotify = (customer, deviceId, code, days = null) => {
// code for now can be "BLOCKED", "SUSPENDED"
const detailB = utils.buildDetail({customerId: customer.id, code, deviceId})
const date = new Date()
if (days) {
date.setDate(date.getDate() + days)
}
const message = code === "SUSPENDED" ? `Customer suspended until ${date.toLocaleString()}` : `Customer blocked`
return clearOldCustomerSuspendedNotifications(customer.id, deviceId).then(() => {
return queries.getValidNotifications(COMPLIANCE, detailB)
}).then(res => {
if (res.length > 0) {
return Promise.resolve()
}
return queries.addNotification(COMPLIANCE, message, detailB)
}).catch(console.error)
}
module.exports = {
transactionNotify,
checkNotification,

View file

@ -46,27 +46,27 @@ const getValidNotifications = (type, detail) => {
return db.any(sql, [type, detail])
}
const getNotificationsGql = () => {
const getNotifications = () => {
const sql = `SELECT * FROM notifications ORDER BY created DESC`
return db.any(sql)
}
const markAsReadGql = (id) => {
const markAsRead = (id) => {
const sql = `UPDATE notifications SET read = 't' WHERE id = $1`
return db.none(sql, [id])
}
const markAllAsReadGql = () => {
const markAllAsRead = () => {
const sql = `UPDATE notifications SET read = 't'`
return db.none(sql)
}
const hasUnreadNotificationsGql = () => {
const hasUnreadNotifications = () => {
const sql = `SELECT EXISTS (SELECT 1 FROM notifications WHERE read = 'f' LIMIT 1)`
return db.oneOrNone(sql).then(res => res.exists)
}
const getAlertsGql = () => {
const getAlerts = () => {
const types = ['fiatBalance', 'cryptoBalance', 'error']
const sql = `SELECT * FROM notifications WHERE valid = 't' AND type IN ($1:list) ORDER BY created DESC`
return db.any(sql, [types])
@ -80,9 +80,9 @@ module.exports = {
batchInvalidate,
clearBlacklistNotification,
getValidNotifications,
getNotificationsGql,
markAsReadGql,
markAllAsReadGql,
hasUnreadNotificationsGql,
getAlertsGql
getNotifications,
markAsRead,
markAllAsRead,
hasUnreadNotifications,
getAlerts
}

View file

@ -29,11 +29,6 @@ function printSmsAlerts (alertRec, config) {
_.map('cryptoCode', entry[1]),
)
const cryptoCodes = _.filter(
_.negate(_.isEmpty),
_.map('cryptoCode', entry[1])
)
return {
codeDisplay: utils.codeDisplay(code),
machineNames,