fix crypto defaults
This commit is contained in:
parent
b6fce47069
commit
6759bec826
3 changed files with 27 additions and 14 deletions
|
|
@ -8,6 +8,7 @@ const pify = require('pify')
|
|||
const pgp = require('pg-promise')()
|
||||
const db = require('./db')
|
||||
const configValidate = require('./config-validate')
|
||||
const schema = require('../lamassu-schema.json')
|
||||
|
||||
let settingsCache
|
||||
|
||||
|
|
@ -167,21 +168,39 @@ function configDeleteField (scope, fieldCode) {
|
|||
}
|
||||
}
|
||||
|
||||
function cryptoCodeDefaults (cryptoCode) {
|
||||
const scope = {crypto: cryptoCode, machine: 'global'}
|
||||
const fieldCode = 'exchange'
|
||||
const fieldType = 'account'
|
||||
const fieldClass = 'exchange'
|
||||
const value = 'no-exchange'
|
||||
function populateScopes (schema) {
|
||||
const scopeLookup = {}
|
||||
_.forEach(r => {
|
||||
const scope = {
|
||||
cryptoScope: r.cryptoScope,
|
||||
machineScope: r.machineScope
|
||||
}
|
||||
|
||||
return configAddField(scope, fieldCode, fieldType, fieldClass, value)
|
||||
_.forEach(field => { scopeLookup[field] = scope }, r.fields)
|
||||
}, schema.groups)
|
||||
|
||||
return _.map(r => _.assign(scopeLookup[r.code], r), schema.fields)
|
||||
}
|
||||
|
||||
function cryptoCodeDefaults (schema, cryptoCode) {
|
||||
const scope = {crypto: cryptoCode, machine: 'global'}
|
||||
|
||||
const schemaEntries = populateScopes(schema)
|
||||
const hasCryptoSpecificDefault = r => r.cryptoScope === 'specific' && !_.isNil(r.default)
|
||||
const cryptoSpecificFields = _.filter(hasCryptoSpecificDefault, schemaEntries)
|
||||
|
||||
return _.map(r => {
|
||||
return configAddField(scope, r.code, r.fieldType, r.fieldClass, r.default)
|
||||
}, cryptoSpecificFields)
|
||||
}
|
||||
|
||||
function addCryptoDefaults (oldConfig, newFields) {
|
||||
const cryptoCodeEntries = _.filter(v => v.fieldLocator.code === 'cryptoCurrencies', newFields)
|
||||
const cryptoCodes = _.map(v => v.fieldValue.value, cryptoCodeEntries)
|
||||
const uniqueCryptoCodes = _.uniq(_.flatten(cryptoCodes))
|
||||
const defaults = _.map(cryptoCodeDefaults, uniqueCryptoCodes)
|
||||
|
||||
const mapDefaults = cryptoCode => cryptoCodeDefaults(schema, cryptoCode)
|
||||
const defaults = _.flatten(_.map(mapDefaults, uniqueCryptoCodes))
|
||||
|
||||
return mergeValues(defaults, oldConfig)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue