From 2646948fbf03f6f4881a8d6c0872053d22bb3f79 Mon Sep 17 00:00:00 2001 From: siiky Date: Wed, 14 Feb 2024 13:51:48 +0000 Subject: [PATCH 01/10] refactor: ignore invalid notifications `lib/notifier/queries.js:getNotifications()` is only used in `lib/new-admin/graphql/resolvers/notification.resolver.js`. In the UI, the `valid` field is only used together with the `read` field, with a true value. --- lib/notifier/queries.js | 2 +- .../src/components/NotificationCenter/NotificationCenter.js | 2 -- .../src/components/NotificationCenter/NotificationRow.js | 3 +-- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/notifier/queries.js b/lib/notifier/queries.js index d89e74fb..60b8e0d8 100644 --- a/lib/notifier/queries.js +++ b/lib/notifier/queries.js @@ -54,7 +54,7 @@ const getValidNotifications = (type, detail) => { } const getNotifications = () => { - const sql = `SELECT * FROM notifications ORDER BY created DESC` + const sql = `SELECT * FROM notifications WHERE valid ORDER BY created DESC` return db.any(sql).catch(logger.error) } const setRead = (id, read) => { diff --git a/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js b/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js index 67095851..d4aa8be8 100644 --- a/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js +++ b/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js @@ -25,7 +25,6 @@ const GET_NOTIFICATIONS = gql` message created read - valid } hasUnreadNotifications machines { @@ -106,7 +105,6 @@ const NotificationCenter = ({ deviceName={machines[n.detail.deviceId]} created={n.created} read={n.read} - valid={n.valid} toggleClear={() => toggleClearNotification({ variables: { id: n.id, read: !n.read } diff --git a/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js b/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js index f5cdea17..87682409 100644 --- a/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js +++ b/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js @@ -44,7 +44,6 @@ const NotificationRow = ({ deviceName, created, read, - valid, toggleClear }) => { const classes = useStyles() @@ -72,7 +71,7 @@ const NotificationRow = ({
{icon}
From f27f85b41831376a940d32d48bef984eeb4b0748 Mon Sep 17 00:00:00 2001 From: siiky Date: Wed, 14 Feb 2024 14:23:17 +0000 Subject: [PATCH 02/10] feat: show only notifications of the past week --- lib/notifier/queries.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/notifier/queries.js b/lib/notifier/queries.js index 60b8e0d8..e6fa0844 100644 --- a/lib/notifier/queries.js +++ b/lib/notifier/queries.js @@ -54,7 +54,11 @@ const getValidNotifications = (type, detail) => { } const getNotifications = () => { - const sql = `SELECT * FROM notifications WHERE valid ORDER BY created DESC` + const sql = ` + SELECT * FROM notifications + WHERE valid AND created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY) + ORDER BY created DESC + ` return db.any(sql).catch(logger.error) } const setRead = (id, read) => { From ab1593078006e43fd0b1dd9c48e856905a7f0e02 Mon Sep 17 00:00:00 2001 From: siiky Date: Wed, 14 Feb 2024 14:24:58 +0000 Subject: [PATCH 03/10] feat: do not consider notifications older than 1 week for unread sign --- lib/notifier/queries.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/notifier/queries.js b/lib/notifier/queries.js index e6fa0844..eb3cc500 100644 --- a/lib/notifier/queries.js +++ b/lib/notifier/queries.js @@ -72,7 +72,11 @@ const markAllAsRead = () => { } const hasUnreadNotifications = () => { - const sql = `SELECT EXISTS (SELECT 1 FROM notifications WHERE read = 'f' LIMIT 1)` + const sql = ` + SELECT EXISTS + (SELECT * FROM notifications + WHERE valid AND NOT read AND created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY)) + ` return db.oneOrNone(sql).then(res => res.exists).catch(logger.error) } From 02490053148c08c1e6014ac36337c1a6743e66df Mon Sep 17 00:00:00 2001 From: siiky Date: Wed, 14 Feb 2024 14:36:43 +0000 Subject: [PATCH 04/10] feat: ignore alerts older than 1 week --- lib/notifier/queries.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/notifier/queries.js b/lib/notifier/queries.js index eb3cc500..42337877 100644 --- a/lib/notifier/queries.js +++ b/lib/notifier/queries.js @@ -82,7 +82,7 @@ const hasUnreadNotifications = () => { const getAlerts = () => { 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 created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY) AND type IN ($1:list) ORDER BY created DESC` return db.any(sql, [types]).catch(logger.error) } From 374a4272a5050865d0cfa043a7b168f5bae2806e Mon Sep 17 00:00:00 2001 From: siiky Date: Wed, 14 Feb 2024 14:39:07 +0000 Subject: [PATCH 05/10] refactor: extract the `WITHIN_PAST_WEEK` condition --- lib/notifier/queries.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/notifier/queries.js b/lib/notifier/queries.js index 42337877..b1e92ab0 100644 --- a/lib/notifier/queries.js +++ b/lib/notifier/queries.js @@ -53,10 +53,12 @@ const getValidNotifications = (type, detail) => { return db.any(sql, [type, detail]).catch(logger.error) } +const WITHIN_PAST_WEEK = `created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY)` + const getNotifications = () => { const sql = ` SELECT * FROM notifications - WHERE valid AND created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY) + WHERE valid AND ${WITHIN_PAST_WEEK} ORDER BY created DESC ` return db.any(sql).catch(logger.error) @@ -75,14 +77,18 @@ const hasUnreadNotifications = () => { const sql = ` SELECT EXISTS (SELECT * FROM notifications - WHERE valid AND NOT read AND created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY)) + WHERE valid AND NOT read AND ${WITHIN_PAST_WEEK}) ` return db.oneOrNone(sql).then(res => res.exists).catch(logger.error) } const getAlerts = () => { const types = ['fiatBalance', 'cryptoBalance', 'error'] - const sql = `SELECT * FROM notifications WHERE valid = 't' AND created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY) AND type IN ($1:list) ORDER BY created DESC` + const sql = ` + SELECT * FROM notifications + WHERE valid AND ${WITHIN_PAST_WEEK} AND type IN ($1:list) + ORDER BY created DESC + ` return db.any(sql, [types]).catch(logger.error) } From d2cdca701aa0e388860feba82fb0fe94010e1d61 Mon Sep 17 00:00:00 2001 From: siiky Date: Wed, 14 Feb 2024 14:46:23 +0000 Subject: [PATCH 06/10] refactor: drop `getMachineName()` from `notifier` --- lib/notifier/index.js | 8 +++++--- lib/notifier/queries.js | 7 ------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/notifier/index.js b/lib/notifier/index.js index ab75d84e..3178f5ab 100644 --- a/lib/notifier/index.js +++ b/lib/notifier/index.js @@ -13,6 +13,8 @@ const smsFuncs = require('./sms') const webhookFuncs = require('./webhook') const { STALE, STALE_STATE } = require('./codes') +const { getMachineName } = require('../machine-loader') + function buildMessage (alerts, notifications) { const smsEnabled = utils.isActive(notifications.sms) const emailEnabled = utils.isActive(notifications.email) @@ -158,7 +160,7 @@ function transactionNotify (tx, rec) { if (!zeroConf && rec.isRedemption) return sendRedemptionMessage(tx.id, rec.error) return Promise.all([ - queries.getMachineName(tx.deviceId), + getMachineName(tx.deviceId), customerPromise ]).then(([machineName, customer]) => { return utils.buildTransactionMessage(tx, rec, highValueTx, machineName, customer) @@ -169,7 +171,7 @@ function transactionNotify (tx, rec) { function complianceNotify (customer, deviceId, action, period) { return Promise.all([ settingsLoader.loadLatest(), - queries.getMachineName(deviceId) + getMachineName(deviceId) ]) .then(([settings, machineName]) => { const notifications = configManager.getGlobalNotifications(settings.config) @@ -267,7 +269,7 @@ function sendTransactionMessage (rec, isHighValueTx) { function cashboxNotify (deviceId) { return Promise.all([ settingsLoader.loadLatest(), - queries.getMachineName(deviceId) + getMachineName(deviceId) ]) .then(([settings, machineName]) => { const notifications = configManager.getGlobalNotifications(settings.config) diff --git a/lib/notifier/queries.js b/lib/notifier/queries.js index b1e92ab0..2d24eeb2 100644 --- a/lib/notifier/queries.js +++ b/lib/notifier/queries.js @@ -15,12 +15,6 @@ compliance - notifications related to warnings triggered by compliance settings 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).catch(logger.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]).catch(logger.error) @@ -105,5 +99,4 @@ module.exports = { markAllAsRead, hasUnreadNotifications, getAlerts, - getMachineName } From ef7f3d519da2882907e12838c1b0a564be080efa Mon Sep 17 00:00:00 2001 From: siiky Date: Wed, 14 Feb 2024 14:53:24 +0000 Subject: [PATCH 07/10] refactor: deduplicate `addNotification()` use --- lib/notifier/notificationCenter.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/notifier/notificationCenter.js b/lib/notifier/notificationCenter.js index 08007852..c5ac36fd 100644 --- a/lib/notifier/notificationCenter.js +++ b/lib/notifier/notificationCenter.js @@ -20,12 +20,10 @@ const { STALE, PING } = codes const sanctionsNotify = (customer, phone) => { const code = 'SANCTIONS' const detailB = utils.buildDetail({ customerId: customer.id, code }) - + const addNotif = phone => + queries.addNotification(COMPLIANCE, `Blocked customer with phone ${phone} for being on the OFAC sanctions list`, detailB) // if it's a new customer then phone comes as undefined - if (phone) { - return queries.addNotification(COMPLIANCE, `Blocked customer with phone ${phone} for being on the OFAC sanctions list`, detailB) - } - return customers.getById(customer.id).then(c => queries.addNotification(COMPLIANCE, `Blocked customer with phone ${c.phone} for being on the OFAC sanctions list`, detailB)) + return phone ? addNotif(phone) : customers.getById(customer.id).then(c => addNotif(c.phone)) } const clearOldCustomerSuspendedNotifications = (customerId, deviceId) => { From a3b240262afb368694715a0dc004bc7978a93e8b Mon Sep 17 00:00:00 2001 From: siiky Date: Mon, 19 Feb 2024 12:37:09 +0000 Subject: [PATCH 08/10] refactor: drop unused `require`s --- lib/plugins.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/plugins.js b/lib/plugins.js index e10532ba..18aaee5f 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -19,11 +19,9 @@ const sms = require('./sms') const email = require('./email') const cashOutHelper = require('./cash-out/cash-out-helper') const machineLoader = require('./machine-loader') -const customers = require('./customers') const commissionMath = require('./commission-math') const loyalty = require('./loyalty') const transactionBatching = require('./tx-batching') -const state = require('./middlewares/state') const { CASH_UNIT_CAPACITY, CASH_OUT_DISPENSE_READY, CONFIRMATION_CODE } = require('./constants') From f220c890a46574bbde00da52c79cba8560e7f7f8 Mon Sep 17 00:00:00 2001 From: siiky Date: Mon, 19 Feb 2024 12:45:56 +0000 Subject: [PATCH 09/10] Revert "refactor: drop `getMachineName()` from `notifier`" This reverts commit d2cdca701aa0e388860feba82fb0fe94010e1d61. --- lib/notifier/index.js | 8 +++----- lib/notifier/queries.js | 7 +++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/notifier/index.js b/lib/notifier/index.js index 3178f5ab..ab75d84e 100644 --- a/lib/notifier/index.js +++ b/lib/notifier/index.js @@ -13,8 +13,6 @@ const smsFuncs = require('./sms') const webhookFuncs = require('./webhook') const { STALE, STALE_STATE } = require('./codes') -const { getMachineName } = require('../machine-loader') - function buildMessage (alerts, notifications) { const smsEnabled = utils.isActive(notifications.sms) const emailEnabled = utils.isActive(notifications.email) @@ -160,7 +158,7 @@ function transactionNotify (tx, rec) { if (!zeroConf && rec.isRedemption) return sendRedemptionMessage(tx.id, rec.error) return Promise.all([ - getMachineName(tx.deviceId), + queries.getMachineName(tx.deviceId), customerPromise ]).then(([machineName, customer]) => { return utils.buildTransactionMessage(tx, rec, highValueTx, machineName, customer) @@ -171,7 +169,7 @@ function transactionNotify (tx, rec) { function complianceNotify (customer, deviceId, action, period) { return Promise.all([ settingsLoader.loadLatest(), - getMachineName(deviceId) + queries.getMachineName(deviceId) ]) .then(([settings, machineName]) => { const notifications = configManager.getGlobalNotifications(settings.config) @@ -269,7 +267,7 @@ function sendTransactionMessage (rec, isHighValueTx) { function cashboxNotify (deviceId) { return Promise.all([ settingsLoader.loadLatest(), - getMachineName(deviceId) + queries.getMachineName(deviceId) ]) .then(([settings, machineName]) => { const notifications = configManager.getGlobalNotifications(settings.config) diff --git a/lib/notifier/queries.js b/lib/notifier/queries.js index 2d24eeb2..b1e92ab0 100644 --- a/lib/notifier/queries.js +++ b/lib/notifier/queries.js @@ -15,6 +15,12 @@ compliance - notifications related to warnings triggered by compliance settings 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).catch(logger.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]).catch(logger.error) @@ -99,4 +105,5 @@ module.exports = { markAllAsRead, hasUnreadNotifications, getAlerts, + getMachineName } From a18d924ffbb7ad48b25813442dfcbfb91bf48451 Mon Sep 17 00:00:00 2001 From: siiky Date: Wed, 20 Mar 2024 12:29:49 +0000 Subject: [PATCH 10/10] refactor: show invalid notifs again --- lib/notifier/queries.js | 6 +++--- .../src/components/NotificationCenter/NotificationCenter.js | 2 ++ .../src/components/NotificationCenter/NotificationRow.js | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/notifier/queries.js b/lib/notifier/queries.js index b1e92ab0..5a51e695 100644 --- a/lib/notifier/queries.js +++ b/lib/notifier/queries.js @@ -58,7 +58,7 @@ const WITHIN_PAST_WEEK = `created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY)` const getNotifications = () => { const sql = ` SELECT * FROM notifications - WHERE valid AND ${WITHIN_PAST_WEEK} + WHERE ${WITHIN_PAST_WEEK} ORDER BY created DESC ` return db.any(sql).catch(logger.error) @@ -77,7 +77,7 @@ const hasUnreadNotifications = () => { const sql = ` SELECT EXISTS (SELECT * FROM notifications - WHERE valid AND NOT read AND ${WITHIN_PAST_WEEK}) + WHERE NOT read AND ${WITHIN_PAST_WEEK}) ` return db.oneOrNone(sql).then(res => res.exists).catch(logger.error) } @@ -86,7 +86,7 @@ const getAlerts = () => { const types = ['fiatBalance', 'cryptoBalance', 'error'] const sql = ` SELECT * FROM notifications - WHERE valid AND ${WITHIN_PAST_WEEK} AND type IN ($1:list) + WHERE ${WITHIN_PAST_WEEK} AND type IN ($1:list) ORDER BY created DESC ` return db.any(sql, [types]).catch(logger.error) diff --git a/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js b/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js index d4aa8be8..67095851 100644 --- a/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js +++ b/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js @@ -25,6 +25,7 @@ const GET_NOTIFICATIONS = gql` message created read + valid } hasUnreadNotifications machines { @@ -105,6 +106,7 @@ const NotificationCenter = ({ deviceName={machines[n.detail.deviceId]} created={n.created} read={n.read} + valid={n.valid} toggleClear={() => toggleClearNotification({ variables: { id: n.id, read: !n.read } diff --git a/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js b/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js index 87682409..f5cdea17 100644 --- a/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js +++ b/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js @@ -44,6 +44,7 @@ const NotificationRow = ({ deviceName, created, read, + valid, toggleClear }) => { const classes = useStyles() @@ -71,7 +72,7 @@ const NotificationRow = ({
{icon}