diff --git a/lib/cash-out/cash-out-atomic.js b/lib/cash-out/cash-out-atomic.js index f3365123..ea65402f 100644 --- a/lib/cash-out/cash-out-atomic.js +++ b/lib/cash-out/cash-out-atomic.js @@ -9,6 +9,8 @@ const helper = require('./cash-out-helper') const cashOutActions = require('./cash-out-actions') const cashOutLow = require('./cash-out-low') +const notifier = require("../notifier/index") + const toObj = helper.toObj module.exports = {atomic} @@ -122,8 +124,10 @@ function updateCassettes (t, tx) { tx.deviceId ] - return t.one(sql, values) - .then(r => socket.emit(_.assign(r, {op: 'cassetteUpdate', deviceId: tx.deviceId}))) + return t.one(sql, values).then(r => { + notifier.cashCassettesNotify(r, tx.deviceId) + return socket.emit(_.assign(r, {op: 'cassetteUpdate', deviceId: tx.deviceId})) + }) } function wasJustAuthorized (oldTx, newTx, isZeroConf) { diff --git a/lib/notifier/index.js b/lib/notifier/index.js index 0466b4dd..e70ea746 100644 --- a/lib/notifier/index.js +++ b/lib/notifier/index.js @@ -356,6 +356,35 @@ const customerComplianceNotify = (customer, deviceId, code, days = null) => { .catch(console.error) } +const cashCassettesNotify = (cassettes, deviceId) => { + settingsLoader.loadLatest() + .then(settings => + [ + configManager.getNotifications(null, deviceId, settings.config), + configManager.getCashOut(deviceId,settings.config).active + ]) + .then(([notifications, cashOutEnabled]) => { + const cassette1Count = cassettes.cassette1 + const cassette2Count = cassettes.cassette2 + const cassette1Threshold = notifications.fiatBalanceCassette1 + const cassette2Threshold = notifications.fiatBalanceCassette2 + + if(cashOutEnabled) { + // we only want to add this notification if there isn't one already set and unread in the database + Promise.all([queries.getUnreadCassetteNotifications(1), queries.getUnreadCassetteNotifications(2)]).then(res => { + if(res[0].length === 0 && cassette1Count < cassette1Threshold) { + console.log("Adding fiatBalance alert for cashbox 1 in database - count & threshold: ", cassette1Count, cassette1Threshold ) + queries.addCashCassetteWarning(1, deviceId) + } + if(res[1].length === 0 && cassette2Count < cassette2Threshold) { + console.log("Adding fiatBalance alert for cashbox 2 in database - count & threshold: ", cassette2Count, cassette2Threshold ) + queries.addCashCassetteWarning(2, deviceId) + } + }) + } + }) +} + module.exports = { transactionNotify, checkNotification,