improve config loading; remove debug

This commit is contained in:
Josh Harvey 2017-04-25 02:25:32 +03:00
parent 5f0b70ca42
commit 614c64646a
15 changed files with 198 additions and 193 deletions

View file

@ -11,6 +11,7 @@ const settingsLoader = require('../settings-loader')
const db = require('../db')
const options = require('../options')
const configManager = require('../config-manager')
const configValidate = require('../config-validate')
const machines = require('./machines')
@ -38,15 +39,6 @@ function allScopes (cryptoScopes, machineScopes) {
return scopes
}
function allCryptoScopes (cryptos, cryptoScope) {
const cryptoScopes = []
if (cryptoScope === 'global' || cryptoScope === 'both') cryptoScopes.push('global')
if (cryptoScope === 'specific' || cryptoScope === 'both') cryptos.forEach(r => cryptoScopes.push(r))
return cryptoScopes
}
function allMachineScopes (machineList, machineScope) {
const machineScopes = []
@ -56,43 +48,6 @@ function allMachineScopes (machineList, machineScope) {
return machineScopes
}
function satisfiesRequire (config, cryptos, machineList, field, refFields) {
const fieldCode = field.code
const scopes = allScopes(
allCryptoScopes(cryptos, field.cryptoScope),
allMachineScopes(machineList, field.machineScope)
)
return scopes.every(scope => {
const isEnabled = () => refFields.some(refField => {
return isScopeEnabled(config, cryptos, machineList, refField, scope)
})
const isBlank = () => R.isNil(configManager.scopedValue(scope[0], scope[1], fieldCode, config))
const isRequired = refFields.length === 0 || isEnabled()
return isRequired ? !isBlank() : true
})
}
function isScopeEnabled (config, cryptos, machineList, refField, scope) {
const [cryptoScope, machineScope] = scope
const candidateCryptoScopes = cryptoScope === 'global'
? allCryptoScopes(cryptos, refField.cryptoScope)
: [cryptoScope]
const candidateMachineScopes = machineScope === 'global'
? allMachineScopes(machineList, refField.machineScope)
: [ machineScope ]
const allRefCandidateScopes = allScopes(candidateCryptoScopes, candidateMachineScopes)
const getFallbackValue = scope => configManager.scopedValue(scope[0], scope[1], refField.code, config)
const values = allRefCandidateScopes.map(getFallbackValue)
return values.some(r => r)
}
function getCryptos (config, machineList) {
const scopes = allScopes(['global'], allMachineScopes(machineList, 'both'))
const scoped = scope => configManager.scopedValue(scope[0], scope[1], 'cryptoCurrencies', config)
@ -112,60 +67,9 @@ function getField (schema, group, fieldCode) {
const fetchMachines = () => machines.getMachines()
.then(machineList => machineList.map(r => r.deviceId))
function validateFieldParameter (value, validator) {
switch (validator.code) {
case 'required':
return true // We don't validate this here
case 'min':
return value >= validator.min
case 'max':
return value <= validator.max
default:
throw new Error('Unknown validation type: ' + validator.code)
}
}
// Validates specific field properties other than required property
function enforceValidConfigParameters (fieldInstances) {
return fetchSchema()
.then(schema => {
const pickField = fieldCode => schema.fields.find(r => r.code === fieldCode)
return fieldInstances.every(fieldInstance => {
const fieldCode = fieldInstance.fieldLocator.code
const field = pickField(fieldCode)
const fieldValue = fieldInstance.fieldValue
const isValid = field.fieldValidation
.every(validator => validateFieldParameter(fieldValue.value, validator))
if (isValid) return true
throw new Error('Invalid config value')
})
})
}
function validateConfig (config) {
return Promise.all([fetchSchema(), fetchMachines()])
.then(([schema, machineList]) => {
const cryptos = getCryptos(config, machineList)
return schema.groups.filter(group => {
return group.fields.some(fieldCode => {
const field = getField(schema, group, fieldCode)
if (!field.fieldValidation.find(r => r.code === 'required')) return false
const refFields = (field.enabledIf || []).map(R.curry(getField)(schema, null))
return !satisfiesRequire(config, cryptos, machineList, field, refFields)
})
})
})
.then(arr => arr.map(r => r.code))
}
function validateCurrentConfig () {
return fetchConfig()
.then(validateConfig)
.then(configValidate.validateRequires)
}
function fetchConfigGroup (code) {
@ -245,6 +149,7 @@ function fetchData () {
{code: 'bitgo', display: 'BitGo', class: 'wallet', cryptos: ['BTC']},
{code: 'geth', display: 'geth', class: 'wallet', cryptos: ['ETH']},
{code: 'mock-wallet', display: 'Mock wallet', class: 'wallet', cryptos: ['BTC', 'ETH']},
{code: 'no-exchange', display: 'No exchange', class: 'exchange', cryptos: ['BTC', 'ETH']},
{code: 'mock-exchange', display: 'Mock exchange', class: 'exchange', cryptos: ['BTC', 'ETH']},
{code: 'mock-sms', display: 'Mock SMS', class: 'sms'},
{code: 'mock-id-verify', display: 'Mock ID verifier', class: 'idVerifier'},
@ -258,7 +163,7 @@ function fetchData () {
function saveConfigGroup (results) {
if (results.values.length === 0) return fetchConfigGroup(results.groupCode)
return enforceValidConfigParameters(results.values)
return configValidate.ensureConstraints(results.values)
.then(fetchConfig)
.then(oldValues => {
results.values.forEach(newValue => {