Remove Ramda from config.js; clean invalid crypto scopes

This commit is contained in:
Josh Harvey 2018-03-11 14:08:26 +00:00
parent 3b35743bb5
commit 1aaabac004

View file

@ -1,7 +1,6 @@
const pify = require('pify') const pify = require('pify')
const fs = pify(require('fs')) const fs = pify(require('fs'))
const path = require('path') const path = require('path')
const R = require('ramda')
const _ = require('lodash/fp') const _ = require('lodash/fp')
const currencies = require('../../currencies.json') const currencies = require('../../currencies.json')
@ -51,17 +50,17 @@ function allMachineScopes (machineList, machineScope) {
function getCryptos (config, machineList) { function getCryptos (config, machineList) {
const scopes = allScopes(['global'], allMachineScopes(machineList, 'both')) const scopes = allScopes(['global'], allMachineScopes(machineList, 'both'))
const scoped = scope => configManager.scopedValue(scope[0], scope[1], 'cryptoCurrencies', config) const scoped = scope => configManager.scopedValue(scope[0], scope[1], 'cryptoCurrencies', config)
return scopes.reduce((acc, scope) => R.union(acc, scoped(scope)), []) return scopes.reduce((acc, scope) => _.union(acc, scoped(scope)), [])
} }
function getGroup (schema, fieldCode) { function getGroup (schema, fieldCode) {
return schema.groups.find(group => group.fields.find(R.equals(fieldCode))) return schema.groups.find(group => group.fields.find(_.isEqual(fieldCode)))
} }
function getField (schema, group, fieldCode) { function getField (schema, group, fieldCode) {
if (!group) group = getGroup(schema, fieldCode) if (!group) group = getGroup(schema, fieldCode)
const field = schema.fields.find(r => r.code === fieldCode) const field = schema.fields.find(r => r.code === fieldCode)
return R.merge(R.pick(['cryptoScope', 'machineScope'], group), field) return _.merge(_.pick(['cryptoScope', 'machineScope'], group), field)
} }
const fetchMachines = () => machineLoader.getMachines() const fetchMachines = () => machineLoader.getMachines()
@ -72,7 +71,7 @@ function validateCurrentConfig () {
.then(configValidate.validateRequires) .then(configValidate.validateRequires)
} }
function decorateEnabledIf (schemaFields, schemaField) { const decorateEnabledIf = _.curry((schemaFields, schemaField) => {
const code = schemaField.fieldLocator.code const code = schemaField.fieldLocator.code
const field = _.find(f => f.code === code, schemaFields) const field = _.find(f => f.code === code, schemaFields)
@ -80,10 +79,10 @@ function decorateEnabledIf (schemaFields, schemaField) {
fieldEnabledIfAny: field.enabledIfAny || [], fieldEnabledIfAny: field.enabledIfAny || [],
fieldEnabledIfAll: field.enabledIfAll || [] fieldEnabledIfAll: field.enabledIfAll || []
}) })
} })
function fetchConfigGroup (code) { function fetchConfigGroup (code) {
const fieldLocatorCodeEq = R.pathEq(['fieldLocator', 'code']) const fieldLocatorCodeEq = _.matchesProperty(['fieldLocator', 'code'])
return Promise.all([fetchSchema(), fetchData(), fetchConfig(), fetchMachines()]) return Promise.all([fetchSchema(), fetchData(), fetchConfig(), fetchMachines()])
.then(([schema, data, config, machineList]) => { .then(([schema, data, config, machineList]) => {
const groupSchema = schema.groups.find(r => r.code === code) const groupSchema = schema.groups.find(r => r.code === code)
@ -91,34 +90,42 @@ function fetchConfigGroup (code) {
if (!groupSchema) throw new Error('No such group schema: ' + code) if (!groupSchema) throw new Error('No such group schema: ' + code)
const schemaFields = groupSchema.fields const schemaFields = groupSchema.fields
.map(R.curry(getField)(schema, groupSchema)) .map(_.curry(getField)(schema, groupSchema))
.map(f => _.assign(f, { .map(f => _.assign(f, {
fieldEnabledIfAny: f.enabledIfAny || [], fieldEnabledIfAny: f.enabledIfAny || [],
fieldEnabledIfAll: f.enabledIfAll || [] fieldEnabledIfAll: f.enabledIfAll || []
})) }))
const candidateFields = [ const candidateFields = [
schemaFields.map(R.prop('requiredIf')), schemaFields.map(_.get('requiredIf')),
schemaFields.map(R.prop('enabledIfAny')), schemaFields.map(_.get('enabledIfAny')),
schemaFields.map(R.prop('enabledIfAll')), schemaFields.map(_.get('enabledIfAll')),
groupSchema.fields, groupSchema.fields,
'fiatCurrency' 'fiatCurrency'
] ]
const configFields = R.uniq(R.flatten(candidateFields)).filter(R.identity)
const smush = _.flow(_.flatten, _.compact, _.uniq)
const configFields = smush(candidateFields)
// Expand this to check against full schema
const fieldValidator = field => !_.isNil(_.get('fieldLocator.fieldScope.crypto', field))
const reducer = (acc, configField) => { const reducer = (acc, configField) => {
return acc.concat(config.filter(fieldLocatorCodeEq(configField))) return acc.concat(config.filter(fieldLocatorCodeEq(configField)))
} }
const values = _.map(f => decorateEnabledIf(schema.fields, f), configFields.reduce(reducer, [])) const reducedFields = _.filter(fieldValidator, configFields.reduce(reducer, []))
const values = _.map(decorateEnabledIf(schema.fields), reducedFields)
groupSchema.fields = undefined groupSchema.fields = undefined
groupSchema.entries = schemaFields groupSchema.entries = schemaFields
const selectedCryptos = _.defaultTo([], getCryptos(config, machineList))
return { return {
schema: groupSchema, schema: groupSchema,
values, values,
selectedCryptos: getCryptos(config, machineList), selectedCryptos,
data data
} }
}) })
@ -130,10 +137,10 @@ function massageCurrencies (currencies) {
display: r['Currency'] display: r['Currency']
}) })
const top5Codes = ['USD', 'EUR', 'GBP', 'CAD', 'AUD'] const top5Codes = ['USD', 'EUR', 'GBP', 'CAD', 'AUD']
const mapped = R.map(convert, currencies) const mapped = _.map(convert, currencies)
const codeToRec = code => R.find(R.propEq('code', code), mapped) const codeToRec = code => _.find(_.matchesProperty('code', code), mapped)
const top5 = R.map(codeToRec, top5Codes) const top5 = _.map(codeToRec, top5Codes)
const raw = R.uniqBy(R.prop('code'), R.concat(top5, mapped)) const raw = _.uniqBy(_.get('code'), _.concat(top5, mapped))
return raw.filter(r => r.code[0] !== 'X' && r.display.indexOf('(') === -1) return raw.filter(r => r.code[0] !== 'X' && r.display.indexOf('(') === -1)
} }