refactor: join static and dynamic configs to reuse pollQueries()
This commit is contained in:
parent
df2213566e
commit
d20f4968f6
3 changed files with 104 additions and 93 deletions
|
|
@ -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(
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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!
|
||||
}
|
||||
`
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue