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 customRequestQueries = require('../new-admin/services/customInfoRequests')
|
||||||
const state = require('../middlewares/state')
|
const state = require('../middlewares/state')
|
||||||
|
|
||||||
|
const VERSION = require('../../package.json').version
|
||||||
|
|
||||||
const urlsToPing = [
|
const urlsToPing = [
|
||||||
`us.archive.ubuntu.com`,
|
`us.archive.ubuntu.com`,
|
||||||
`uk.archive.ubuntu.com`,
|
`uk.archive.ubuntu.com`,
|
||||||
|
|
@ -78,9 +80,31 @@ const massageTerms = terms => (terms.active && terms.text) ? ({
|
||||||
cancel: terms.cancelButtonText,
|
cancel: terms.cancelButtonText,
|
||||||
}) : null
|
}) : null
|
||||||
|
|
||||||
const staticConfig = (parent, { currentConfigVersion }, { deviceId, deviceName, settings }, info) =>
|
const staticConfig = ({ currentConfigVersion, deviceId, deviceName, pq, settings, }) => {
|
||||||
Promise.all([
|
const massageCoins = _.map(_.pick([
|
||||||
plugins(settings, deviceId).staticConfigQueries(),
|
'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.getCompliance(settings.config).enablePaperWalletOnly,
|
||||||
configManager.getTriggersAutomation(settings.config),
|
configManager.getTriggersAutomation(settings.config),
|
||||||
buildTriggers(configManager.getTriggers(settings.config)),
|
buildTriggers(configManager.getTriggers(settings.config)),
|
||||||
|
|
@ -92,7 +116,6 @@ const staticConfig = (parent, { currentConfigVersion }, { deviceId, deviceName,
|
||||||
!!configManager.getCashOut(deviceId, settings.config).active,
|
!!configManager.getCashOut(deviceId, settings.config).active,
|
||||||
])
|
])
|
||||||
.then(([
|
.then(([
|
||||||
staticConf,
|
|
||||||
enablePaperWalletOnly,
|
enablePaperWalletOnly,
|
||||||
triggersAutomation,
|
triggersAutomation,
|
||||||
triggers,
|
triggers,
|
||||||
|
|
@ -126,6 +149,7 @@ const staticConfig = (parent, { currentConfigVersion }, { deviceId, deviceName,
|
||||||
addOperatorInfo(operatorInfo),
|
addOperatorInfo(operatorInfo),
|
||||||
addReceiptInfo(receiptInfo)
|
addReceiptInfo(receiptInfo)
|
||||||
)(staticConf))
|
)(staticConf))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const setZeroConfLimit = config => coin =>
|
const setZeroConfLimit = config => coin =>
|
||||||
|
|
@ -135,22 +159,81 @@ const setZeroConfLimit = config => coin =>
|
||||||
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)
|
state.pids = _.update(operatorId, _.set(deviceId, { pid, ts: Date.now() }), state.pids)
|
||||||
return plugins(settings, deviceId)
|
|
||||||
.dynamicConfigQueries()
|
return _.flow(
|
||||||
.then(_.flow(
|
_.pick(['balances', 'cassettes', 'coins', 'rates']),
|
||||||
_.update('coins', _.map(setZeroConfLimit(settings.config))),
|
|
||||||
_.set('reboot', !!pid && state.reboots?.[operatorId]?.[deviceId] === pid),
|
_.update('cassettes', massageCassettes),
|
||||||
_.set('shutdown', !!pid && state.shutdowns?.[operatorId]?.[deviceId] === pid),
|
|
||||||
_.set('restartServices', !!pid && state.restartServicesMap?.[operatorId]?.[deviceId] === pid),
|
/* [{ 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 = {
|
module.exports = {
|
||||||
Query: {
|
Query: {
|
||||||
staticConfig,
|
configs
|
||||||
dynamicConfig
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -140,8 +140,12 @@ type DynamicConfig {
|
||||||
restartServices: Boolean!
|
restartServices: Boolean!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Configs {
|
||||||
|
static: StaticConfig
|
||||||
|
dynamic: DynamicConfig!
|
||||||
|
}
|
||||||
|
|
||||||
type Query {
|
type Query {
|
||||||
staticConfig(currentConfigVersion: Int): StaticConfig
|
configs(currentConfigVersion: Int): Configs!
|
||||||
dynamicConfig: DynamicConfig!
|
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,6 @@ const loyalty = require('./loyalty')
|
||||||
const transactionBatching = require('./tx-batching')
|
const transactionBatching = require('./tx-batching')
|
||||||
const state = require('./middlewares/state')
|
const state = require('./middlewares/state')
|
||||||
|
|
||||||
const VERSION = require('../package.json').version
|
|
||||||
|
|
||||||
const { CASSETTE_MAX_CAPACITY, CASH_OUT_DISPENSE_READY, CONFIRMATION_CODE } = require('./constants')
|
const { CASSETTE_MAX_CAPACITY, CASH_OUT_DISPENSE_READY, CONFIRMATION_CODE } = require('./constants')
|
||||||
|
|
||||||
const notifier = require('./notifier')
|
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) {
|
function sendCoins (tx) {
|
||||||
return wallet.supportsBatching(settings, tx.cryptoCode)
|
return wallet.supportsBatching(settings, tx.cryptoCode)
|
||||||
.then(supportsBatching => {
|
.then(supportsBatching => {
|
||||||
|
|
@ -929,8 +855,6 @@ function plugins (settings, deviceId) {
|
||||||
getRawRates,
|
getRawRates,
|
||||||
buildRatesNoCommission,
|
buildRatesNoCommission,
|
||||||
pollQueries,
|
pollQueries,
|
||||||
staticConfigQueries,
|
|
||||||
dynamicConfigQueries,
|
|
||||||
sendCoins,
|
sendCoins,
|
||||||
newAddress,
|
newAddress,
|
||||||
isHd,
|
isHd,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue