load latest *good* config
This commit is contained in:
parent
dd7d06f38b
commit
5f0b70ca42
4 changed files with 31 additions and 13 deletions
|
|
@ -6,6 +6,7 @@ const R = require('ramda')
|
|||
const currencies = require('../../currencies.json')
|
||||
const languageRec = require('../../languages.json')
|
||||
const countries = require('../../countries.json')
|
||||
const settingsLoader = require('../settings-loader')
|
||||
|
||||
const db = require('../db')
|
||||
const options = require('../options')
|
||||
|
|
@ -254,11 +255,6 @@ function fetchData () {
|
|||
}))
|
||||
}
|
||||
|
||||
function dbSaveConfig (config) {
|
||||
const sql = 'insert into user_config (type, data) values ($1, $2)'
|
||||
return db.none(sql, ['config', {config}])
|
||||
}
|
||||
|
||||
function saveConfigGroup (results) {
|
||||
if (results.values.length === 0) return fetchConfigGroup(results.groupCode)
|
||||
|
||||
|
|
@ -289,7 +285,7 @@ function saveConfigGroup (results) {
|
|||
if (!R.isNil(newValue.fieldValue)) oldValues.push(newValue)
|
||||
})
|
||||
|
||||
return dbSaveConfig(oldValues)
|
||||
return settingsLoader.save(oldValues)
|
||||
.then(() => fetchConfigGroup(results.groupCode))
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ function updateMachineDefaults (deviceId) {
|
|||
|
||||
return settingsLoader.loadLatest()
|
||||
.then(settings => {
|
||||
return settingsLoader.save({config: settingsLoader.mergeValues(settings.config, newFields)})
|
||||
return settingsLoader.save(settingsLoader.mergeValues(settings.config, newFields))
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -82,12 +82,20 @@ function loadLatestConfig () {
|
|||
const sql = `select data
|
||||
from user_config
|
||||
where type=$1
|
||||
and valid
|
||||
order by id desc
|
||||
limit 1`
|
||||
|
||||
return db.oneOrNone(sql, ['config'])
|
||||
.then(row => row ? row.data.config : [])
|
||||
return db.one(sql, ['config'])
|
||||
.then(row => row.data.config)
|
||||
.then(validate)
|
||||
.catch(err => {
|
||||
if (err.name === 'QueryResultError') {
|
||||
throw new Error('lamassu-server is not configured')
|
||||
}
|
||||
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
function checkConstraint (entry, constraint) {
|
||||
|
|
@ -114,9 +122,11 @@ function validateEntry (entry) {
|
|||
return _.every(constraint => validateConstraint(entry, constraint), validations)
|
||||
}
|
||||
|
||||
function isValid (config) {
|
||||
return _.every(validateEntry, config)
|
||||
}
|
||||
function validate (config) {
|
||||
const isValid = _.every(validateEntry, config)
|
||||
if (!isValid) throw new Error('Invalid config')
|
||||
if (!isValid(config)) throw new Error('Invalid config')
|
||||
return config
|
||||
}
|
||||
|
||||
|
|
@ -136,8 +146,8 @@ function settings () {
|
|||
}
|
||||
|
||||
function save (config) {
|
||||
const sql = 'insert into user_config (type, data) values ($1, $2)'
|
||||
return db.none(sql, ['config', config])
|
||||
const sql = 'insert into user_config (type, data, valid) values ($1, $2, $3)'
|
||||
return db.none(sql, ['config', {config}, isValid(config)])
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
12
migrations/029-add_valid_to_user_config.js
Normal file
12
migrations/029-add_valid_to_user_config.js
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
var db = require('./db')
|
||||
|
||||
exports.up = function (next) {
|
||||
var sql = [
|
||||
'alter table user_config add column valid boolean not null'
|
||||
]
|
||||
db.multi(sql, next)
|
||||
}
|
||||
|
||||
exports.down = function (next) {
|
||||
next()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue