From ef60b15d823c7f627b5ed29e25b9faa22d9ad69e Mon Sep 17 00:00:00 2001 From: csrapr <26280794+csrapr@users.noreply.github.com> Date: Fri, 5 Feb 2021 00:05:50 +0000 Subject: [PATCH] Chore: post-rebase fixes --- lib/new-admin/graphql/schema.js | 10 +- lib/notifier/index.js | 201 ------------------ lib/notifier/queries.js | 20 +- lib/notifier/sms.js | 5 - lib/plugins.js | 57 ++--- .../NotificationCenter/NotificationCenter.js | 1 + .../NotificationCenter/NotificationRow.js | 1 + .../src/components/layout/Header.js | 1 + .../src/pages/Dashboard/Alerts/Alerts.js | 114 ++++------ .../pages/Dashboard/Alerts/Alerts.styles.js | 26 +-- .../src/pages/Dashboard/Alerts/AlertsTable.js | 42 ++-- .../src/pages/Dashboard/Dashboard.js | 1 + .../src/pages/Dashboard/Dashboard.styles.js | 8 +- .../src/pages/Dashboard/Footer/Footer.js | 1 + .../src/pages/Dashboard/RightSide.js | 47 ++-- .../Graphs/PercentageChart.js | 63 +++--- .../SystemPerformance/Graphs/RefLineChart.js | 1 + .../Graphs/RefScatterplot.js | 1 + .../pages/Dashboard/SystemPerformance/Nav.js | 1 + .../SystemPerformance/SystemPerformance.js | 57 +++-- .../SystemPerformance.styles.js | 26 ++- .../Dashboard/SystemStatus/MachinesTable.js | 1 + .../Dashboard/SystemStatus/SystemStatus.js | 1 + .../MachineComponents/Cassettes/Cassettes.js | 3 +- .../Commissions/Commissions.js | 1 + .../Machines/MachineComponents/Details.js | 1 + .../Transactions/DataTable.js | 1 + .../Transactions/Transactions.js | 1 + .../src/pages/Machines/Machines.js | 1 + 29 files changed, 224 insertions(+), 470 deletions(-) diff --git a/lib/new-admin/graphql/schema.js b/lib/new-admin/graphql/schema.js index d3eae0bf..67412e19 100644 --- a/lib/new-admin/graphql/schema.js +++ b/lib/new-admin/graphql/schema.js @@ -248,12 +248,6 @@ const typeDefs = gql` rate: Float } - type Rate { - code: String - name: String - rate: Float - } - type Notification { id: ID! type: String @@ -297,6 +291,7 @@ const typeDefs = gql` cryptoRates: JSONObject fiatRates: [Rate] notifications: [Notification] + alerts: [Notification] hasUnreadNotifications: Boolean } @@ -396,7 +391,8 @@ const resolvers = { }), fiatRates: () => forex.getFiatRates(), notifications: () => notifierQueries.getNotifications(), - hasUnreadNotifications: () => notifierQueries.hasUnreadNotifications() + hasUnreadNotifications: () => notifierQueries.hasUnreadNotifications(), + alerts: () => notifierQueries.getAlerts() }, Mutation: { machineAction: (...[, { deviceId, action, cashbox, cassette1, cassette2, newName }]) => machineAction({ deviceId, action, cashbox, cassette1, cassette2, newName }), diff --git a/lib/notifier/index.js b/lib/notifier/index.js index ce4af396..0466b4dd 100644 --- a/lib/notifier/index.js +++ b/lib/notifier/index.js @@ -356,207 +356,6 @@ const customerComplianceNotify = (customer, deviceId, code, days = null) => { .catch(console.error) } -const clearOldCryptoNotifications = (balances) => { - // get valid crypto notifications from DB - // first, for each DB notification, if it doesn't exist in balances then it is old and should not be valid anymore - // if it exists in balances, add the index of it in balances to the array of duplicates - // return the array of duplicates so that balancesNotify doesn't add them - return queries.getAllValidNotifications(CRYPTO_BALANCE).then(res => { - const notifications = _.map(it => { - return { - id: it.id, - cryptoCode: it.detail.cryptoCode, - code: it.detail.code - } - }, res) - const duplicateIndexes = [] - const idsToInvalidate = [] - _.forEach(notification => { - const idx = _.findIndex(balance => { - return balance.code === notification.code && balance.cryptoCode === notification.cryptoCode - }, balances) - - if (idx === -1) { - // if notification in DB doesnt exist in balances anymore then it is invalid now - idsToInvalidate.push(notification.id) - } - else { - // if it exists then it is a duplicate, add it to array - duplicateIndexes.push(idx) - } - }, notifications) - return (idsToInvalidate.length > 0 ? queries.batchInvalidate(idsToInvalidate) : Promise.resolve()).then(() => duplicateIndexes) - }) -} - -const cryptoBalancesNotify = (cryptoWarnings) => { - return clearOldCryptoNotifications(cryptoWarnings).then(duplicateIndexes => { - return cryptoWarnings.forEach((balance, idx) => { - if(duplicateIndexes.includes(idx)) { - return - } - const fiat = utils.formatCurrency(balance.fiatBalance.balance, balance.fiatCode) - const message = `${balance.code === 'HIGH_CRYPTO_BALANCE' ? 'High' : 'Low'} balance in ${balance.cryptoCode} [${fiat}]` - console.log(`Adding ${balance.code === 'HIGH_CRYPTO_BALANCE' ? 'high' : 'low'} balance notification for ${balance.cryptoCode}`) - const detailB = utils.buildDetail({cryptoCode: balance.cryptoCode, code: balance.code}) - return queries.addNotification(CRYPTO_BALANCE, message, detailB) - }) - }) -} - -const clearOldFiatNotifications = (balances) => { - return queries.getAllValidNotifications(FIAT_BALANCE).then(notifications => { - const duplicateIndexes = [] - const idsToInvalidate = [] - _.forEach(notification => { - const idx = _.findIndex(balance => { - return notification.detail.deviceId === balance.deviceId && notification.detail.cassette === balance.cassette - }, balances) - - if (idx === -1) { - // if notification in DB doesnt exist in balances anymore then it is invalid now - idsToInvalidate.push(notification.id) - } - else { - // if it exists then it is a duplicate, add it to array - duplicateIndexes.push(idx) - } - }, notifications) - return (idsToInvalidate.length > 0 ? queries.batchInvalidate(idsToInvalidate) : Promise.resolve()).then(() => duplicateIndexes) - }) -} - -const fiatBalancesNotify = (fiatWarnings) => { - return clearOldFiatNotifications(fiatWarnings).then(duplicateIndexes => { - return fiatWarnings.forEach((balance, idx) => { - if(duplicateIndexes.includes(idx)) { - return - } - console.log(`Adding low cash balance notification for cassette ${balance.cassette} at ${balance.machineName}`) - const message = `Cash-out cassette ${balance.cassette} almost empty!` - const detailB = utils.buildDetail({deviceId: balance.deviceId, cassette: balance.cassette}) - return queries.addNotification(FIAT_BALANCE, message, detailB) - }) - }) -} - -const balancesNotify = (balances) => { - const cryptoFilter = o => o.code === 'HIGH_CRYPTO_BALANCE' || o.code === 'LOW_CRYPTO_BALANCE' - const fiatFilter = o => o.code === 'LOW_CASH_OUT' - const cryptoWarnings = _.filter(cryptoFilter, balances) - const fiatWarnings = _.filter(fiatFilter, balances) - return Promise.all([cryptoBalancesNotify(cryptoWarnings), fiatBalancesNotify(fiatWarnings)]).catch(console.error) -} - - -const clearOldErrorNotifications = (alerts) => { - return queries.getAllValidNotifications(ERROR).then(res => { - const indexesToInvalidate = [] - _.forEach(notification => { - const idx = _.findIndex(alert => { - return alert.code === notification.detail.code && alert.deviceId === notification.detail.deviceId - }, alerts) - if(idx !== -1) { - return - } - // if the notification doesn't exist, then it is outdated and is not valid anymore - indexesToInvalidate.push(notification.id) - }, res) - return indexesToInvalidate.length > 0 ? queries.batchInvalidate(indexesToInvalidate) : null - }).catch(console.log) -} - -const errorAlertsNotify = (alertRec) => { - let alerts = [] - _.keys(alertRec.devices).forEach(function (device) { - // embed device ID in each alert object inside the deviceAlerts array - alertRec.devices[device].deviceAlerts = _.map(alert => { - return {...alert, deviceId: device} - }, alertRec.devices[device].deviceAlerts) - // concat every array into one - alerts = _.concat(alerts, alertRec.devices[device].deviceAlerts) - }) - - // now that we have all the alerts, we want to add PING and STALE alerts to the DB - // if there is a valid alert on the DB that doesn't exist on the new alerts array, - // that alert should be considered invalid - // after that, for the alerts array, we have to see if there is a valid alert of - // the sorts already on the DB - return clearOldErrorNotifications(alerts).then(() => { - _.forEach(alert => { - switch(alert.code) { - case PING: { - const detailB = utils.buildDetail({code: PING, age: alert.age ? alert.age : -1, deviceId: alert.deviceId}) - return queries.getValidNotifications(ERROR, _.omit(['age'], detailB)).then(res => { - if(res.length > 0) { - return Promise.resolve() - } - console.log("Adding PING alert on database for " + alert.machineName) - const message = `Machine down` - return queries.addNotification(ERROR, message, detailB) - }) - } - case STALE: { - const detailB = utils.buildDetail({code: STALE, deviceId: alert.deviceId}) - return queries.getValidNotifications(ERROR, detailB).then(res => { - if(res.length > 0) { - return Promise.resolve() - } - console.log("Adding STALE alert on database for " + alert.machineName) - const message = `Machine is stuck on ${alert.state} screen` - return queries.addNotification(ERROR, message, detailB) - }) - } - default: - return - } - }, alerts) - }).catch(console.error) -} - -const blacklistNotify = (tx, isAddressReuse) => { - let message = '' - let detailB = {} - if(isAddressReuse) { - detail = `${tx.cryptoCode}_REUSED_${tx.toAddress}` - detailB = utils.buildDetail({cryptoCode: tx.cryptoCode, code: 'REUSED', cryptoAddress: tx.toAddress}) - message = `Blocked reused address: ${tx.cryptoCode} ${tx.toAddress.substr(0,10)}...` - } else { - detail = `${tx.cryptoCode}_BLOCKED_${tx.toAddress}` - detailB = utils.buildDetail({cryptoCode: tx.cryptoCode, code: 'BLOCKED', cryptoAddress: tx.toAddress}) - message = `Blocked blacklisted address: ${tx.cryptoCode} ${tx.toAddress.substr(0,10)}...` - } - return queries.addNotification(COMPLIANCE, message, detailB) -} - -const clearBlacklistNotification = (cryptoCode, cryptoAddress) => { - return queries.clearBlacklistNotification(cryptoCode, cryptoAddress).catch(console.error) -} - -const clearOldCustomerSuspendedNotifications = (customerId, deviceId) => { - const detailB = utils.buildDetail({code: 'SUSPENDED', customerId, deviceId}) - return queries.invalidateNotification(detailB, 'compliance') -} - -const customerComplianceNotify = (customer, deviceId, code, days = null) => { - // code for now can be "BLOCKED", "SUSPENDED" - const detailB = utils.buildDetail({customerId: customer.id, code, deviceId}) - const date = new Date() - if (days) { - date.setDate(date.getDate() + days) - } - const message = code === "SUSPENDED" ? `Customer suspended until ${date.toLocaleString()}` : `Customer blocked` - - return clearOldCustomerSuspendedNotifications(customer.id, deviceId).then(() => { - return queries.getValidNotifications(COMPLIANCE, detailB) - }).then(res => { - if (res.length > 0) { - return Promise.resolve() - } - return queries.addNotification(COMPLIANCE, message, detailB) - }).catch(console.error) -} - module.exports = { transactionNotify, checkNotification, diff --git a/lib/notifier/queries.js b/lib/notifier/queries.js index bca51fa7..39f1bd1c 100644 --- a/lib/notifier/queries.js +++ b/lib/notifier/queries.js @@ -46,27 +46,27 @@ const getValidNotifications = (type, detail) => { return db.any(sql, [type, detail]) } -const getNotificationsGql = () => { +const getNotifications = () => { const sql = `SELECT * FROM notifications ORDER BY created DESC` return db.any(sql) } -const markAsReadGql = (id) => { +const markAsRead = (id) => { const sql = `UPDATE notifications SET read = 't' WHERE id = $1` return db.none(sql, [id]) } -const markAllAsReadGql = () => { +const markAllAsRead = () => { const sql = `UPDATE notifications SET read = 't'` return db.none(sql) } -const hasUnreadNotificationsGql = () => { +const hasUnreadNotifications = () => { const sql = `SELECT EXISTS (SELECT 1 FROM notifications WHERE read = 'f' LIMIT 1)` return db.oneOrNone(sql).then(res => res.exists) } -const getAlertsGql = () => { +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]) @@ -80,9 +80,9 @@ module.exports = { batchInvalidate, clearBlacklistNotification, getValidNotifications, - getNotificationsGql, - markAsReadGql, - markAllAsReadGql, - hasUnreadNotificationsGql, - getAlertsGql + getNotifications, + markAsRead, + markAllAsRead, + hasUnreadNotifications, + getAlerts } diff --git a/lib/notifier/sms.js b/lib/notifier/sms.js index 592a8a45..fd088394 100644 --- a/lib/notifier/sms.js +++ b/lib/notifier/sms.js @@ -29,11 +29,6 @@ function printSmsAlerts (alertRec, config) { _.map('cryptoCode', entry[1]), ) - const cryptoCodes = _.filter( - _.negate(_.isEmpty), - _.map('cryptoCode', entry[1]) - ) - return { codeDisplay: utils.codeDisplay(code), machineNames, diff --git a/lib/plugins.js b/lib/plugins.js index 8da6fa99..f1a91bcd 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -25,8 +25,6 @@ const promoCodes = require('./promo-codes') const notifier = require('./notifier') -const notifier = require('./notifier/index') - const mapValuesWithKey = _.mapValues.convert({ cap: false }) @@ -164,41 +162,30 @@ function plugins (settings, deviceId) { ? argv.cassettes.split(',') : rec.counts - return Promise.all([ - dbm.cassetteCounts(deviceId), - cashOutHelper.redeemableTxs(deviceId, excludeTxId) - ]).then(([rec, _redeemableTxs]) => { - const redeemableTxs = _.reject( - _.matchesProperty('id', excludeTxId), - _redeemableTxs - ) + const cassettes = [ + { + denomination: parseInt(denominations[0], 10), + count: parseInt(counts[0], 10) + }, + { + denomination: parseInt(denominations[1], 10), + count: parseInt(counts[1], 10) + } + ] - const counts = argv.cassettes ? argv.cassettes.split(',') : rec.counts - - const cassettes = [ - { - denomination: parseInt(denominations[0], 10), - count: parseInt(counts[0], 10) - }, - { - denomination: parseInt(denominations[1], 10), - count: parseInt(counts[1], 10) + try { + return { + cassettes: computeAvailableCassettes(cassettes, redeemableTxs), + virtualCassettes + } + } catch (err) { + logger.error(err) + return { + cassettes, + virtualCassettes + } } - ] - - try { - return { - cassettes: computeAvailableCassettes(cassettes, redeemableTxs), - virtualCassettes - } - } catch (err) { - logger.error(err) - return { - cassettes, - virtualCassettes - } - } - }) + }) } function fetchCurrentConfigVersion () { diff --git a/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js b/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js index c165f765..b2d78f5a 100644 --- a/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js +++ b/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js @@ -3,6 +3,7 @@ import { makeStyles } from '@material-ui/core/styles' import gql from 'graphql-tag' import * as R from 'ramda' import React, { useState } from 'react' + import ActionButton from 'src/components/buttons/ActionButton' import { H5 } from 'src/components/typography' import { ReactComponent as NotificationIconZodiac } from 'src/styling/icons/menu/notification-zodiac.svg' diff --git a/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js b/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js index c62d8077..b01312ce 100644 --- a/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js +++ b/new-lamassu-admin/src/components/NotificationCenter/NotificationRow.js @@ -3,6 +3,7 @@ import { makeStyles } from '@material-ui/core/styles' import classnames from 'classnames' import prettyMs from 'pretty-ms' import React from 'react' + import { Label1, Label2, TL2 } from 'src/components/typography' import { ReactComponent as Wrench } from 'src/styling/icons/action/wrench/zodiac.svg' import { ReactComponent as Transaction } from 'src/styling/icons/arrow/transaction.svg' diff --git a/new-lamassu-admin/src/components/layout/Header.js b/new-lamassu-admin/src/components/layout/Header.js index b69f9cd4..98601869 100644 --- a/new-lamassu-admin/src/components/layout/Header.js +++ b/new-lamassu-admin/src/components/layout/Header.js @@ -6,6 +6,7 @@ import classnames from 'classnames' import gql from 'graphql-tag' import React, { memo, useState } from 'react' import { NavLink, useHistory } from 'react-router-dom' + import NotificationCenter from 'src/components/NotificationCenter' import ActionButton from 'src/components/buttons/ActionButton' import { H4 } from 'src/components/typography' diff --git a/new-lamassu-admin/src/pages/Dashboard/Alerts/Alerts.js b/new-lamassu-admin/src/pages/Dashboard/Alerts/Alerts.js index d0ad8adb..28486e24 100644 --- a/new-lamassu-admin/src/pages/Dashboard/Alerts/Alerts.js +++ b/new-lamassu-admin/src/pages/Dashboard/Alerts/Alerts.js @@ -4,7 +4,9 @@ import Grid from '@material-ui/core/Grid' import { makeStyles } from '@material-ui/core/styles' import gql from 'graphql-tag' import * as R from 'ramda' -import React, { useState, useEffect } from 'react' +import React from 'react' + +import { cardState as cardState_ } from 'src/components/CollapsibleCard' import { Label1, H4 } from 'src/components/typography' import styles from '../Dashboard.styles' @@ -33,98 +35,58 @@ const GET_ALERTS = gql` const useStyles = makeStyles(styles) -const Alerts = ({ cardState, setRightSideState }) => { +const Alerts = ({ onReset, onExpand, size }) => { const classes = useStyles() - const [showAllItems, setShowAllItems] = useState(false) + const showAllItems = size === cardState_.EXPANDED const { data } = useQuery(GET_ALERTS) - const alerts = R.path(['alerts'])(data) ?? [] const machines = R.compose( R.map(R.prop('name')), R.indexBy(R.prop('deviceId')) )(data?.machines ?? []) - - const showExpandButton = alerts.length > NUM_TO_RENDER && !showAllItems - - useEffect(() => { - if (cardState.cardSize === 'small' || cardState.cardSize === 'default') { - setShowAllItems(false) - } - }, [cardState.cardSize]) - - const reset = () => { - setRightSideState({ - systemStatus: { cardSize: 'default', buttonName: 'Show less' }, - alerts: { cardSize: 'default', buttonName: 'Show less' } - }) - setShowAllItems(false) - } - - const showAllClick = () => { - setShowAllItems(true) - setRightSideState({ - systemStatus: { cardSize: 'small', buttonName: 'Show machines' }, - alerts: { cardSize: 'big', buttonName: 'Show less' } - }) - } + const alertsLength = alerts.length return ( <> -
{`${alert.message}${alert - .detail.deviceId && - ' - ' + machines[alert.detail.deviceId]}`}
-{`${alert.message}${alert.detail + .deviceId && ' - ' + machines[alert.detail.deviceId]}`}
+In
-