From d20f4968f650bebbc698bbd95747e3ab4cb22d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1?= Date: Fri, 22 Apr 2022 16:06:33 +0100 Subject: [PATCH] refactor: join static and dynamic configs to reuse `pollQueries()` --- lib/graphql/resolvers.js | 113 +++++++++++++++++++++++++++++++++------ lib/graphql/types.js | 8 ++- lib/plugins.js | 76 -------------------------- 3 files changed, 104 insertions(+), 93 deletions(-) diff --git a/lib/graphql/resolvers.js b/lib/graphql/resolvers.js index e831f4d3..5d457c64 100644 --- a/lib/graphql/resolvers.js +++ b/lib/graphql/resolvers.js @@ -7,6 +7,8 @@ const configManager = require('../new-config-manager') const customRequestQueries = require('../new-admin/services/customInfoRequests') const state = require('../middlewares/state') +const VERSION = require('../../package.json').version + const urlsToPing = [ `us.archive.ubuntu.com`, `uk.archive.ubuntu.com`, @@ -78,9 +80,31 @@ const massageTerms = terms => (terms.active && terms.text) ? ({ cancel: terms.cancelButtonText, }) : null -const staticConfig = (parent, { currentConfigVersion }, { deviceId, deviceName, settings }, info) => - Promise.all([ - plugins(settings, deviceId).staticConfigQueries(), +const staticConfig = ({ currentConfigVersion, deviceId, deviceName, pq, settings, }) => { + const massageCoins = _.map(_.pick([ + 'batchable', + 'cashInCommission', + 'cashInFee', + 'cashOutCommission', + 'cryptoCode', + 'cryptoNetwork', + 'cryptoUnits', + 'display', + 'minimumTx' + ])) + + const staticConf = _.flow( + _.pick([ + 'areThereAvailablePromoCodes', + 'coins', + 'configVersion', + 'timezone' + ]), + _.update('coins', massageCoins), + _.set('serverVersion', VERSION), + )(pq) + + return Promise.all([ !!configManager.getCompliance(settings.config).enablePaperWalletOnly, configManager.getTriggersAutomation(settings.config), buildTriggers(configManager.getTriggers(settings.config)), @@ -92,7 +116,6 @@ const staticConfig = (parent, { currentConfigVersion }, { deviceId, deviceName, !!configManager.getCashOut(deviceId, settings.config).active, ]) .then(([ - staticConf, enablePaperWalletOnly, triggersAutomation, triggers, @@ -126,6 +149,7 @@ const staticConfig = (parent, { currentConfigVersion }, { deviceId, deviceName, addOperatorInfo(operatorInfo), addReceiptInfo(receiptInfo) )(staticConf)) +} const setZeroConfLimit = config => coin => @@ -135,22 +159,81 @@ const setZeroConfLimit = config => coin => coin ) -const dynamicConfig = (parent, variables, { deviceId, operatorId, pid, settings }, info) => { +const dynamicConfig = ({ deviceId, operatorId, pid, pq, settings, }) => { + const massageCassettes = cassettes => + cassettes ? + _.flow( + cassettes => _.set('physical', _.get('cassettes', cassettes), cassettes), + cassettes => _.set('virtual', _.get('virtualCassettes', cassettes), cassettes), + _.unset('cassettes'), + _.unset('virtualCassettes') + )(cassettes) : + null + state.pids = _.update(operatorId, _.set(deviceId, { pid, ts: Date.now() }), state.pids) - return plugins(settings, deviceId) - .dynamicConfigQueries() - .then(_.flow( - _.update('coins', _.map(setZeroConfLimit(settings.config))), - _.set('reboot', !!pid && state.reboots?.[operatorId]?.[deviceId] === pid), - _.set('shutdown', !!pid && state.shutdowns?.[operatorId]?.[deviceId] === pid), - _.set('restartServices', !!pid && state.restartServicesMap?.[operatorId]?.[deviceId] === pid), - )) + + return _.flow( + _.pick(['balances', 'cassettes', 'coins', 'rates']), + + _.update('cassettes', massageCassettes), + + /* [{ cryptoCode, rates }, ...] => [[cryptoCode, rates], ...] */ + _.update('coins', _.map(({ cryptoCode, rates }) => [cryptoCode, rates])), + + /* [{ cryptoCode: balance }, ...] => [[cryptoCode, { balance }], ...] */ + _.update('balances', _.flow( + _.toPairs, + _.map(([cryptoCode, balance]) => [cryptoCode, { balance }]) + )), + + /* Group the separate objects by cryptoCode */ + /* { balances, coins, rates } => { cryptoCode: { balance, ask, bid, cashIn, cashOut }, ... } */ + ({ balances, cassettes, coins, rates }) => ({ + cassettes, + coins: _.flow( + _.reduce( + (ret, [cryptoCode, obj]) => _.update(cryptoCode, _.assign(obj), ret), + rates + ), + + /* { cryptoCode: { balance, ask, bid, cashIn, cashOut }, ... } => [[cryptoCode, { balance, ask, bid, cashIn, cashOut }], ...] */ + _.toPairs, + + /* [[cryptoCode, { balance, ask, bid, cashIn, cashOut }], ...] => [{ cryptoCode, balance, ask, bid, cashIn, cashOut }, ...] */ + _.map(([cryptoCode, obj]) => _.set('cryptoCode', cryptoCode, obj)) + )(_.concat(balances, coins)) + }), + + _.update('coins', _.map(setZeroConfLimit(settings.config))), + _.set('reboot', !!pid && state.reboots?.[operatorId]?.[deviceId] === pid), + _.set('shutdown', !!pid && state.shutdowns?.[operatorId]?.[deviceId] === pid), + _.set('restartServices', !!pid && state.restartServicesMap?.[operatorId]?.[deviceId] === pid), + )(pq) } +const configs = (parent, { currentConfigVersion }, { deviceId, deviceName, operatorId, pid, settings }, info) => + plugins(settings, deviceId) + .pollQueries() + .then(pq => ({ + static: staticConfig({ + currentConfigVersion, + deviceId, + deviceName, + pq, + settings, + }), + dynamic: dynamicConfig({ + deviceId, + operatorId, + pid, + pq, + settings, + }), + })) + module.exports = { Query: { - staticConfig, - dynamicConfig + configs } } diff --git a/lib/graphql/types.js b/lib/graphql/types.js index 7e504182..a9318978 100644 --- a/lib/graphql/types.js +++ b/lib/graphql/types.js @@ -140,8 +140,12 @@ type DynamicConfig { restartServices: Boolean! } +type Configs { + static: StaticConfig + dynamic: DynamicConfig! +} + type Query { - staticConfig(currentConfigVersion: Int): StaticConfig - dynamicConfig: DynamicConfig! + configs(currentConfigVersion: Int): Configs! } ` diff --git a/lib/plugins.js b/lib/plugins.js index 4f3f172c..0fbff479 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -27,8 +27,6 @@ const loyalty = require('./loyalty') const transactionBatching = require('./tx-batching') const state = require('./middlewares/state') -const VERSION = require('../package.json').version - const { CASSETTE_MAX_CAPACITY, CASH_OUT_DISPENSE_READY, CONFIRMATION_CODE } = require('./constants') const notifier = require('./notifier') @@ -284,78 +282,6 @@ function plugins (settings, deviceId) { }) } - function staticConfigQueries () { - const massageCoins = _.map(_.pick([ - 'batchable', - 'cashInCommission', - 'cashInFee', - 'cashOutCommission', - 'cryptoCode', - 'cryptoNetwork', - 'cryptoUnits', - 'display', - 'minimumTx' - ])) - - return pollQueries() - .then(_.flow( - _.pick([ - 'areThereAvailablePromoCodes', - 'coins', - 'configVersion', - 'timezone' - ]), - _.update('coins', massageCoins), - _.set('serverVersion', VERSION) - )) - } - - function dynamicConfigQueries () { - const massageCassettes = cassettes => - cassettes ? - _.flow( - cassettes => _.set('physical', _.get('cassettes', cassettes), cassettes), - cassettes => _.set('virtual', _.get('virtualCassettes', cassettes), cassettes), - _.unset('cassettes'), - _.unset('virtualCassettes') - )(cassettes) : - null - - return pollQueries() - .then(_.flow( - _.pick(['balances', 'cassettes', 'coins', 'rates']), - - _.update('cassettes', massageCassettes), - - /* [{ cryptoCode, rates }, ...] => [[cryptoCode, rates], ...] */ - _.update('coins', _.map(({ cryptoCode, rates }) => [cryptoCode, rates])), - - /* [{ cryptoCode: balance }, ...] => [[cryptoCode, { balance }], ...] */ - _.update('balances', _.flow( - _.toPairs, - _.map(([cryptoCode, balance]) => [cryptoCode, { balance }]) - )), - - /* Group the separate objects by cryptoCode */ - /* { balances, coins, rates } => { cryptoCode: { balance, ask, bid, cashIn, cashOut }, ... } */ - ({ balances, cassettes, coins, rates }) => ({ - cassettes, - coins: _.flow( - _.reduce( - (ret, [cryptoCode, obj]) => _.update(cryptoCode, _.assign(obj), ret), - rates - ), - - /* { cryptoCode: { balance, ask, bid, cashIn, cashOut }, ... } => [[cryptoCode, { balance, ask, bid, cashIn, cashOut }], ...] */ - _.toPairs, - - /* [[cryptoCode, { balance, ask, bid, cashIn, cashOut }], ...] => [{ cryptoCode, balance, ask, bid, cashIn, cashOut }, ...] */ - _.map(([cryptoCode, obj]) => _.set('cryptoCode', cryptoCode, obj)) - )(_.concat(balances, coins)) - }) - )) - } - function sendCoins (tx) { return wallet.supportsBatching(settings, tx.cryptoCode) .then(supportsBatching => { @@ -929,8 +855,6 @@ function plugins (settings, deviceId) { getRawRates, buildRatesNoCommission, pollQueries, - staticConfigQueries, - dynamicConfigQueries, sendCoins, newAddress, isHd,