improve config loading; remove debug
This commit is contained in:
parent
5f0b70ca42
commit
614c64646a
15 changed files with 198 additions and 193 deletions
|
|
@ -6,11 +6,7 @@ const argv = require('minimist')(process.argv.slice(2))
|
|||
const pify = require('pify')
|
||||
|
||||
const db = require('./db')
|
||||
const options = require('./options')
|
||||
const logger = require('./logger')
|
||||
|
||||
const schemaPath = path.resolve(options.lamassuServerPath, 'lamassu-schema.json')
|
||||
const schema = require(schemaPath)
|
||||
const configValidate = require('./config-validate')
|
||||
|
||||
let settingsCache
|
||||
|
||||
|
|
@ -69,17 +65,25 @@ function loadConfig (versionId) {
|
|||
|
||||
const sql = `select data
|
||||
from user_config
|
||||
where id=$1 and type=$2`
|
||||
where id=$1 and type=$2
|
||||
and valid`
|
||||
|
||||
return db.oneOrNone(sql, [versionId, 'config'])
|
||||
.then(row => row ? row.data.config : [])
|
||||
.then(validate)
|
||||
return db.one(sql, [versionId, 'config'])
|
||||
.then(row => row.data.config)
|
||||
.then(configValidate.validate)
|
||||
.catch(err => {
|
||||
if (err.name === 'QueryResultError') {
|
||||
throw new Error('No such config version: ' + versionId)
|
||||
}
|
||||
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
function loadLatestConfig () {
|
||||
if (argv.fixture) return loadFixture()
|
||||
|
||||
const sql = `select data
|
||||
const sql = `select id, valid, data
|
||||
from user_config
|
||||
where type=$1
|
||||
and valid
|
||||
|
|
@ -88,7 +92,7 @@ function loadLatestConfig () {
|
|||
|
||||
return db.one(sql, ['config'])
|
||||
.then(row => row.data.config)
|
||||
.then(validate)
|
||||
.then(configValidate.validate)
|
||||
.catch(err => {
|
||||
if (err.name === 'QueryResultError') {
|
||||
throw new Error('lamassu-server is not configured')
|
||||
|
|
@ -98,38 +102,6 @@ function loadLatestConfig () {
|
|||
})
|
||||
}
|
||||
|
||||
function checkConstraint (entry, constraint) {
|
||||
switch (constraint.code) {
|
||||
case 'min':
|
||||
return entry.fieldValue.value >= constraint.min
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
function validateConstraint (entry, constraint) {
|
||||
const isValid = checkConstraint(entry, constraint)
|
||||
if (!isValid) logger.error(`Validation error: ${entry.fieldLocator.code} [${constraint.code}]`)
|
||||
return isValid
|
||||
}
|
||||
|
||||
function validateEntry (entry) {
|
||||
const fieldCode = entry.fieldLocator.code
|
||||
const schemaEntry = _.find(_.matchesProperty('code', fieldCode), schema.fields)
|
||||
if (!schemaEntry) throw new Error(`Unsupported field: ${fieldCode}`)
|
||||
|
||||
const validations = schemaEntry.fieldValidation
|
||||
return _.every(constraint => validateConstraint(entry, constraint), validations)
|
||||
}
|
||||
|
||||
function isValid (config) {
|
||||
return _.every(validateEntry, config)
|
||||
}
|
||||
function validate (config) {
|
||||
if (!isValid(config)) throw new Error('Invalid config')
|
||||
return config
|
||||
}
|
||||
|
||||
function loadAccounts () {
|
||||
const toFields = fieldArr => _.fromPairs(_.map(r => [r.code, r.value], fieldArr))
|
||||
const toPairs = r => [r.code, toFields(r.fields)]
|
||||
|
|
@ -147,7 +119,10 @@ function settings () {
|
|||
|
||||
function save (config) {
|
||||
const sql = 'insert into user_config (type, data, valid) values ($1, $2, $3)'
|
||||
return db.none(sql, ['config', {config}, isValid(config)])
|
||||
|
||||
return configValidate.validate(config)
|
||||
.then(() => db.none(sql, ['config', {config}, true]))
|
||||
.catch(() => db.none(sql, ['config', {config}, false]))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue