Feat: crypto balance notifications saving in DB

Chore: add new column "detail" to transactions table migration

Feat: check if older notification is valid before sending new one

Feat: error saving to database

Fix: fix error when invalidating notification on
clearCryptoBalanceNotifications
Chre: code refactor in new-settings-loader for simplicity
Chore: refactor code on notifier and merge similar functions
This commit is contained in:
Cesar 2020-12-10 18:26:13 +00:00 committed by Josh Harvey
parent 196a05549f
commit 3b3bdf839b
9 changed files with 224 additions and 34 deletions

View file

@ -12,21 +12,62 @@ error - notifications related to errors
*/
const addHighValueTx = (tx) => {
const sql = `INSERT INTO notifications (id, type, device_id, message, created) values($1, $2, $3, $4, CURRENT_TIMESTAMP)`
const sql = `INSERT INTO notifications (id, type, device_id, message, created) values ($1, $2, $3, $4, CURRENT_TIMESTAMP)`
const direction = tx.direction === "cashOut" ? 'cash-out' : 'cash-in'
const message = `${tx.fiat} ${tx.fiatCode} ${direction} transaction`
return db.oneOrNone(sql, [uuidv4(), 'highValueTransaction', tx.deviceId, message])
}
const addCashCassetteWarning = (cassetteNumber, deviceId) => {
const sql = `INSERT INTO notifications (id, type, detail, device_id, message, created) values($1, $2, $3, $4, $5, CURRENT_TIMESTAMP)`
const sql = `INSERT INTO notifications (id, type, detail, device_id, message, created) values ($1, $2, $3, $4, $5, CURRENT_TIMESTAMP)`
const message = `Cash-out cassette ${cassetteNumber} almost empty!`
return db.oneOrNone(sql, [uuidv4(), 'fiatBalance', cassetteNumber, deviceId, message])
}
const getUnreadCassetteNotifications = (cassetteNumber) => {
const sql = `SELECT * FROM notifications WHERE read = 'f' AND TYPE = 'fiatBalance' AND detail = '$1'`
return db.any(sql, [cassetteNumber])
const getUnreadCassetteNotifications = (cassetteNumber, deviceId) => {
const sql = `SELECT * FROM notifications WHERE read = 'f' AND device_id = $1 AND TYPE = 'fiatBalance' AND detail = '$2'`
return db.any(sql, [deviceId, cassetteNumber])
}
module.exports = { machineEvents: dbm.machineEvents, addHighValueTx, addCashCassetteWarning, getUnreadCassetteNotifications }
const addCryptoBalanceWarning = (detail, message) => {
const sql = `INSERT INTO notifications (id, type, detail, message, created) values ($1, $2, $3, $4, CURRENT_TIMESTAMP)`
return db.oneOrNone(sql, [uuidv4(), 'cryptoBalance', detail, message])
}
const getAllValidNotifications = (type) => {
const sql = `SELECT * FROM notifications WHERE type = $1 AND valid = 't'`
return db.any(sql, [type])
}
const addErrorNotification = (detail, message, deviceId) => {
const sql = `INSERT INTO notifications (id, type, detail, device_id, message, created) values ($1, $2, $3, $4, $5, CURRENT_TIMESTAMP)`
return db.oneOrNone(sql, [uuidv4(), 'error', detail, deviceId, message])
}
const getValidNotifications = (type, detail, deviceId = null) => {
let sql;
if(!deviceId) {
sql = `SELECT * FROM notifications WHERE type = $1 AND valid = 't' AND detail LIKE $2`
}
else {
sql = `SELECT * FROM notifications WHERE type = $1 AND valid = 't' AND detail LIKE $2 AND device_id = $3`
}
return db.any(sql, [type, `%${detail}%`, deviceId])
}
const invalidateNotification = (id) => {
const sql = `UPDATE notifications SET valid = 'f', read = 't' WHERE id = $1`
return db.none(sql, [id])
}
module.exports = {
machineEvents: dbm.machineEvents,
addHighValueTx,
addCashCassetteWarning,
addCryptoBalanceWarning,
addErrorNotification,
getUnreadCassetteNotifications,
getAllValidNotifications,
getValidNotifications,
invalidateNotification,
}