Chore: move catch into queries file

This commit is contained in:
csrapr 2021-02-15 13:50:54 +00:00 committed by Josh Harvey
parent 05373e83db
commit 82912b728a
7 changed files with 26 additions and 28 deletions

View file

@ -15,7 +15,7 @@ const getBlacklist = () => {
// Delete row from blacklist table by crypto code and address // Delete row from blacklist table by crypto code and address
const deleteFromBlacklist = (cryptoCode, address) => { const deleteFromBlacklist = (cryptoCode, address) => {
const sql = `DELETE FROM blacklist WHERE crypto_code = $1 AND address = $2` const sql = `DELETE FROM blacklist WHERE crypto_code = $1 AND address = $2`
notifierQueries.clearBlacklistNotification(cryptoCode, address).catch(console.error) notifierQueries.clearBlacklistNotification(cryptoCode, address)
return db.none(sql, [cryptoCode, address]) return db.none(sql, [cryptoCode, address])
} }

View file

@ -31,9 +31,9 @@ function post (machineTx, pi) {
if (_.some(it => it.created_by_operator)(blacklistItems)) { if (_.some(it => it.created_by_operator)(blacklistItems)) {
blacklisted = true blacklisted = true
notifier.notifyIfActive('compliance', 'blacklistNotify', r.tx, false).catch(console.error) notifier.notifyIfActive('compliance', 'blacklistNotify', r.tx, false)
} else if (_.some(it => !it.created_by_operator)(blacklistItems) && rejectAddressReuseActive) { } else if (_.some(it => !it.created_by_operator)(blacklistItems) && rejectAddressReuseActive) {
notifier.notifyIfActive('compliance', 'blacklistNotify', r.tx, true).catch(console.error) notifier.notifyIfActive('compliance', 'blacklistNotify', r.tx, true)
addressReuse = true addressReuse = true
} }
return postProcess(r, pi, blacklisted, addressReuse) return postProcess(r, pi, blacklisted, addressReuse)

View file

@ -116,7 +116,7 @@ async function updateCustomer (id, data, userToken) {
const sql = Pgp.helpers.update(updateData, _.keys(updateData), 'customers') + const sql = Pgp.helpers.update(updateData, _.keys(updateData), 'customers') +
' where id=$1' ' where id=$1'
invalidateCustomerNotifications(id, formattedData).catch(console.error) invalidateCustomerNotifications(id, formattedData)
await db.none(sql, [id]) await db.none(sql, [id])
@ -127,7 +127,7 @@ const invalidateCustomerNotifications = (id, data) => {
if (data.authorized_override !== 'verified') return Promise.resolve() if (data.authorized_override !== 'verified') return Promise.resolve()
const detailB = notifierUtils.buildDetail({ code: 'BLOCKED', customerId: id }) const detailB = notifierUtils.buildDetail({ code: 'BLOCKED', customerId: id })
return notifierQueries.invalidateNotification(detailB, 'compliance').catch(console.error) return notifierQueries.invalidateNotification(detailB, 'compliance')
} }
/** /**

View file

@ -43,7 +43,7 @@ function checkNotification (plugins) {
return getAlerts(plugins) return getAlerts(plugins)
.then(alerts => { .then(alerts => {
notifyIfActive('errors', 'errorAlertsNotify', alerts).catch(console.error) notifyIfActive('errors', 'errorAlertsNotify', alerts)
const currentAlertFingerprint = utils.buildAlertFingerprint( const currentAlertFingerprint = utils.buildAlertFingerprint(
alerts, alerts,
notifications notifications
@ -74,7 +74,7 @@ function getAlerts (plugins) {
queries.machineEvents(), queries.machineEvents(),
plugins.getMachineNames() plugins.getMachineNames()
]).then(([balances, events, devices]) => { ]).then(([balances, events, devices]) => {
notifyIfActive('balance', 'balancesNotify', balances).catch(console.error) notifyIfActive('balance', 'balancesNotify', balances)
return buildAlerts(checkPings(devices), balances, events, devices) return buildAlerts(checkPings(devices), balances, events, devices)
}) })
} }
@ -141,9 +141,9 @@ function transactionNotify (tx, rec) {
// awaiting for redesign. notification should not be sent if toggle in the settings table is disabled, // 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 // but currently we're sending notifications of high value tx even with the toggle disabled
if (readyToNotify && !highValueTx) { if (readyToNotify && !highValueTx) {
notifyIfActive('transactions', 'notifCenterTransactionNotify', highValueTx, directionDisplay, tx.fiat, tx.fiatCode, tx.deviceId, tx.toAddress).catch(console.error) notifyIfActive('transactions', 'notifCenterTransactionNotify', highValueTx, directionDisplay, tx.fiat, tx.fiatCode, tx.deviceId, tx.toAddress)
} else if (readyToNotify && highValueTx) { } else if (readyToNotify && highValueTx) {
notificationCenter.notifCenterTransactionNotify(highValueTx, directionDisplay, tx.fiat, tx.fiatCode, tx.deviceId, tx.toAddress).catch(console.error) 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 // alert through sms or email any transaction or high value transaction, if SMS || email alerts are enabled

View file

@ -47,7 +47,6 @@ const customerComplianceNotify = (customer, deviceId, code, days = null) => {
if (res.length > 0) return Promise.resolve() if (res.length > 0) return Promise.resolve()
return queries.addNotification(COMPLIANCE, message, detailB) return queries.addNotification(COMPLIANCE, message, detailB)
}) })
.catch(console.error)
} }
const clearOldFiatNotifications = (balances) => { const clearOldFiatNotifications = (balances) => {
@ -115,7 +114,7 @@ const balancesNotify = (balances) => {
const fiatFilter = o => o.code === 'LOW_CASH_OUT' const fiatFilter = o => o.code === 'LOW_CASH_OUT'
const cryptoWarnings = _.filter(cryptoFilter, balances) const cryptoWarnings = _.filter(cryptoFilter, balances)
const fiatWarnings = _.filter(fiatFilter, balances) const fiatWarnings = _.filter(fiatFilter, balances)
return Promise.all([cryptoBalancesNotify(cryptoWarnings), fiatBalancesNotify(fiatWarnings)]).catch(console.error) return Promise.all([cryptoBalancesNotify(cryptoWarnings), fiatBalancesNotify(fiatWarnings)])
} }
const clearOldErrorNotifications = alerts => { const clearOldErrorNotifications = alerts => {
@ -131,7 +130,6 @@ const clearOldErrorNotifications = alerts => {
if (!indexesToInvalidate.length) return Promise.resolve() if (!indexesToInvalidate.length) return Promise.resolve()
return queries.batchInvalidate(indexesToInvalidate) return queries.batchInvalidate(indexesToInvalidate)
}) })
.catch(console.error)
} }
const errorAlertsNotify = (alertRec) => { const errorAlertsNotify = (alertRec) => {
@ -160,7 +158,7 @@ const errorAlertsNotify = (alertRec) => {
} }
} }
}, alerts) }, alerts)
}).catch(console.error) })
} }
function notifCenterTransactionNotify (isHighValue, direction, fiat, fiatCode, deviceId, cryptoAddress) { function notifCenterTransactionNotify (isHighValue, direction, fiat, fiatCode, deviceId, cryptoAddress) {

View file

@ -17,65 +17,65 @@ error - notifications related to errors
function getMachineName (machineId) { function getMachineName (machineId) {
const sql = 'SELECT * FROM devices WHERE device_id=$1' const sql = 'SELECT * FROM devices WHERE device_id=$1'
return db.oneOrNone(sql, [machineId]) return db.oneOrNone(sql, [machineId])
.then(it => it.name) .then(it => it.name).catch(console.error)
} }
const addNotification = (type, message, detail) => { const addNotification = (type, message, detail) => {
const sql = `INSERT INTO notifications (id, type, message, detail) VALUES ($1, $2, $3, $4)` const sql = `INSERT INTO notifications (id, type, message, detail) VALUES ($1, $2, $3, $4)`
return db.oneOrNone(sql, [uuidv4(), type, message, detail]) return db.oneOrNone(sql, [uuidv4(), type, message, detail]).catch(console.error)
} }
const getAllValidNotifications = (type) => { const getAllValidNotifications = (type) => {
const sql = `SELECT * FROM notifications WHERE type = $1 AND valid = 't'` const sql = `SELECT * FROM notifications WHERE type = $1 AND valid = 't'`
return db.any(sql, [type]) return db.any(sql, [type]).catch(console.error)
} }
const invalidateNotification = (detail, type) => { const invalidateNotification = (detail, type) => {
detail = _.omitBy(_.isEmpty, detail) detail = _.omitBy(_.isEmpty, detail)
const sql = `UPDATE notifications SET valid = 'f', read = 't' WHERE valid = 't' AND type = $1 AND detail::jsonb @> $2::jsonb` const sql = `UPDATE notifications SET valid = 'f', read = 't' WHERE valid = 't' AND type = $1 AND detail::jsonb @> $2::jsonb`
return db.none(sql, [type, detail]) return db.none(sql, [type, detail]).catch(console.error).catch(console.error)
} }
const batchInvalidate = (ids) => { const batchInvalidate = (ids) => {
const formattedIds = _.map(pgp.as.text, ids).join(',') const formattedIds = _.map(pgp.as.text, ids).join(',')
const sql = `UPDATE notifications SET valid = 'f', read = 't' WHERE id IN ($1^)` const sql = `UPDATE notifications SET valid = 'f', read = 't' WHERE id IN ($1^)`
return db.none(sql, [formattedIds]) return db.none(sql, [formattedIds]).catch(console.error)
} }
const clearBlacklistNotification = (cryptoCode, cryptoAddress) => { const clearBlacklistNotification = (cryptoCode, cryptoAddress) => {
const sql = `UPDATE notifications SET valid = 'f', read = 't' WHERE type = 'compliance' AND detail->>'cryptoCode' = $1 AND detail->>'cryptoAddress' = $2 AND (detail->>'code' = 'BLOCKED' OR detail->>'code' = 'REUSED')` const sql = `UPDATE notifications SET valid = 'f', read = 't' WHERE type = 'compliance' AND detail->>'cryptoCode' = $1 AND detail->>'cryptoAddress' = $2 AND (detail->>'code' = 'BLOCKED' OR detail->>'code' = 'REUSED')`
return db.none(sql, [cryptoCode, cryptoAddress]) return db.none(sql, [cryptoCode, cryptoAddress]).catch(console.error)
} }
const getValidNotifications = (type, detail) => { const getValidNotifications = (type, detail) => {
const sql = `SELECT * FROM notifications WHERE type = $1 AND valid = 't' AND detail @> $2` const sql = `SELECT * FROM notifications WHERE type = $1 AND valid = 't' AND detail @> $2`
return db.any(sql, [type, detail]) return db.any(sql, [type, detail]).catch(console.error)
} }
const getNotifications = () => { const getNotifications = () => {
const sql = `SELECT * FROM notifications ORDER BY created DESC` const sql = `SELECT * FROM notifications ORDER BY created DESC`
return db.any(sql) return db.any(sql).catch(console.error)
} }
const markAsRead = (id) => { const markAsRead = (id) => {
const sql = `UPDATE notifications SET read = 't' WHERE id = $1` const sql = `UPDATE notifications SET read = 't' WHERE id = $1`
return db.none(sql, [id]) return db.none(sql, [id]).catch(console.error)
} }
const markAllAsRead = () => { const markAllAsRead = () => {
const sql = `UPDATE notifications SET read = 't'` const sql = `UPDATE notifications SET read = 't'`
return db.none(sql) return db.none(sql).catch(console.error)
} }
const hasUnreadNotifications = () => { const hasUnreadNotifications = () => {
const sql = `SELECT EXISTS (SELECT 1 FROM notifications WHERE read = 'f' LIMIT 1)` const sql = `SELECT EXISTS (SELECT 1 FROM notifications WHERE read = 'f' LIMIT 1)`
return db.oneOrNone(sql).then(res => res.exists) return db.oneOrNone(sql).then(res => res.exists).catch(console.error)
} }
const getAlerts = () => { const getAlerts = () => {
const types = ['fiatBalance', 'cryptoBalance', 'error'] const types = ['fiatBalance', 'cryptoBalance', 'error']
const sql = `SELECT * FROM notifications WHERE valid = 't' AND type IN ($1:list) ORDER BY created DESC` const sql = `SELECT * FROM notifications WHERE valid = 't' AND type IN ($1:list) ORDER BY created DESC`
return db.any(sql, [types]) return db.any(sql, [types]).catch(console.error)
} }
module.exports = { module.exports = {

View file

@ -343,7 +343,7 @@ function triggerBlock (req, res, next) {
customers.update(id, { authorizedOverride: 'blocked' }) customers.update(id, { authorizedOverride: 'blocked' })
.then(customer => { .then(customer => {
notifier.notifyIfActive('compliance', 'customerComplianceNotify', customer, req.deviceId, 'BLOCKED').catch(console.error) notifier.notifyIfActive('compliance', 'customerComplianceNotify', customer, req.deviceId, 'BLOCKED')
return respond(req, res, { customer }) return respond(req, res, { customer })
}) })
.catch(next) .catch(next)
@ -362,7 +362,7 @@ function triggerSuspend (req, res, next) {
date.setDate(date.getDate() + days); date.setDate(date.getDate() + days);
customers.update(id, { suspendedUntil: date }) customers.update(id, { suspendedUntil: date })
.then(customer => { .then(customer => {
notifier.notifyIfActive('compliance', 'customerComplianceNotify', customer, req.deviceId, 'SUSPENDED', days).catch(console.error) notifier.notifyIfActive('compliance', 'customerComplianceNotify', customer, req.deviceId, 'SUSPENDED', days)
return respond(req, res, { customer }) return respond(req, res, { customer })
}) })
.catch(next) .catch(next)
@ -432,7 +432,7 @@ function respond (req, res, _body, _status) {
const customer = _.getOr({ sanctions: true }, ['customer'], body) const customer = _.getOr({ sanctions: true }, ['customer'], body)
// sanctions can be null for new customers so we can't use falsy checks // sanctions can be null for new customers so we can't use falsy checks
if (customer.sanctions === false) { if (customer.sanctions === false) {
notifier.notifyIfActive('compliance', 'sanctionsNotify', customer, req.body.phone).catch(console.error) notifier.notifyIfActive('compliance', 'sanctionsNotify', customer, req.body.phone)
} }
return res.status(status).json(body) return res.status(status).json(body)
} }