diff --git a/lib/admin/config.js b/lib/admin/config.js index 1979bb68..0e6ace73 100644 --- a/lib/admin/config.js +++ b/lib/admin/config.js @@ -1,7 +1,6 @@ const pify = require('pify') const fs = pify(require('fs')) const path = require('path') -const R = require('ramda') const _ = require('lodash/fp') const currencies = require('../../currencies.json') @@ -51,17 +50,17 @@ function allMachineScopes (machineList, machineScope) { function getCryptos (config, machineList) { const scopes = allScopes(['global'], allMachineScopes(machineList, 'both')) 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) { - 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) { if (!group) group = getGroup(schema, 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() @@ -72,7 +71,7 @@ function validateCurrentConfig () { .then(configValidate.validateRequires) } -function decorateEnabledIf (schemaFields, schemaField) { +const decorateEnabledIf = _.curry((schemaFields, schemaField) => { const code = schemaField.fieldLocator.code const field = _.find(f => f.code === code, schemaFields) @@ -80,10 +79,10 @@ function decorateEnabledIf (schemaFields, schemaField) { fieldEnabledIfAny: field.enabledIfAny || [], fieldEnabledIfAll: field.enabledIfAll || [] }) -} +}) function fetchConfigGroup (code) { - const fieldLocatorCodeEq = R.pathEq(['fieldLocator', 'code']) + const fieldLocatorCodeEq = _.matchesProperty(['fieldLocator', 'code']) return Promise.all([fetchSchema(), fetchData(), fetchConfig(), fetchMachines()]) .then(([schema, data, config, machineList]) => { 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) const schemaFields = groupSchema.fields - .map(R.curry(getField)(schema, groupSchema)) + .map(_.curry(getField)(schema, groupSchema)) .map(f => _.assign(f, { fieldEnabledIfAny: f.enabledIfAny || [], fieldEnabledIfAll: f.enabledIfAll || [] })) const candidateFields = [ - schemaFields.map(R.prop('requiredIf')), - schemaFields.map(R.prop('enabledIfAny')), - schemaFields.map(R.prop('enabledIfAll')), + schemaFields.map(_.get('requiredIf')), + schemaFields.map(_.get('enabledIfAny')), + schemaFields.map(_.get('enabledIfAll')), groupSchema.fields, '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) => { 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.entries = schemaFields + const selectedCryptos = _.defaultTo([], getCryptos(config, machineList)) + return { schema: groupSchema, values, - selectedCryptos: getCryptos(config, machineList), + selectedCryptos, data } }) @@ -130,10 +137,10 @@ function massageCurrencies (currencies) { display: r['Currency'] }) const top5Codes = ['USD', 'EUR', 'GBP', 'CAD', 'AUD'] - const mapped = R.map(convert, currencies) - const codeToRec = code => R.find(R.propEq('code', code), mapped) - const top5 = R.map(codeToRec, top5Codes) - const raw = R.uniqBy(R.prop('code'), R.concat(top5, mapped)) + const mapped = _.map(convert, currencies) + const codeToRec = code => _.find(_.matchesProperty('code', code), mapped) + const top5 = _.map(codeToRec, top5Codes) + const raw = _.uniqBy(_.get('code'), _.concat(top5, mapped)) return raw.filter(r => r.code[0] !== 'X' && r.display.indexOf('(') === -1) }