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

@ -17,65 +17,65 @@ error - notifications related to errors
function getMachineName (machineId) {
const sql = 'SELECT * FROM devices WHERE device_id=$1'
return db.oneOrNone(sql, [machineId])
.then(it => it.name)
.then(it => it.name).catch(console.error)
}
const addNotification = (type, message, detail) => {
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 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) => {
detail = _.omitBy(_.isEmpty, detail)
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 formattedIds = _.map(pgp.as.text, ids).join(',')
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 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 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 sql = `SELECT * FROM notifications ORDER BY created DESC`
return db.any(sql)
return db.any(sql).catch(console.error)
}
const markAsRead = (id) => {
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 sql = `UPDATE notifications SET read = 't'`
return db.none(sql)
return db.none(sql).catch(console.error)
}
const hasUnreadNotifications = () => {
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 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])
return db.any(sql, [types]).catch(console.error)
}
module.exports = {