Feat: add "detail" row in notifications table migration This makes searching for specific notifications in the code like cashbox number easier because we don't need to scrub the notification message column anymore to search for notifications relating the cashbox 1 for example. Feat: clear notifications on cash cassette balance update
32 lines
1.6 KiB
JavaScript
32 lines
1.6 KiB
JavaScript
const dbm = require('../postgresql_interface')
|
|
const db = require('../db')
|
|
const { v4: uuidv4 } = require('uuid')
|
|
|
|
// types of notifications able to be inserted into db:
|
|
/*
|
|
highValueTransaction - for transactions of value higher than threshold
|
|
fiatBalance - when the number of notes in cash cassettes falls below threshold
|
|
cryptoBalance - when ammount of crypto balance in fiat falls below or above low/high threshold
|
|
compliance - notifications related to warnings triggered by compliance settings
|
|
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 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 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])
|
|
}
|
|
|
|
module.exports = { machineEvents: dbm.machineEvents, addHighValueTx, addCashCassetteWarning, getUnreadCassetteNotifications }
|