feat: refactor the loading of the latest config so there's now two

functions, one for the server and one for the new admin web page
This commit is contained in:
Liordino Neto 2020-08-13 00:34:33 -03:00 committed by Josh Harvey
parent a0f6feeb78
commit d6b707ff7a
2 changed files with 17 additions and 8 deletions

View file

@ -258,7 +258,7 @@ const resolvers = {
serverLogs.getServerLogs(from, until, limit, offset), serverLogs.getServerLogs(from, until, limit, offset),
transactions: (...[, { from, until, limit, offset }]) => transactions: (...[, { from, until, limit, offset }]) =>
transactions.batch(from, until, limit, offset), transactions.batch(from, until, limit, offset),
config: () => settingsLoader.loadLatestConfig(), config: () => settingsLoader.loadLatestConfigOrNone(),
accounts: () => settingsLoader.loadAccounts() accounts: () => settingsLoader.loadAccounts()
}, },
Mutation: { Mutation: {

View file

@ -31,7 +31,7 @@ function loadAccounts () {
function saveConfig (config) { function saveConfig (config) {
const sql = 'insert into user_config (type, data, valid, schema_version) values ($1, $2, $3, $4)' const sql = 'insert into user_config (type, data, valid, schema_version) values ($1, $2, $3, $4)'
return loadLatestConfig() return loadLatestConfigOrNone()
.then(currentConfig => { .then(currentConfig => {
const newConfig = _.assign(currentConfig, config) const newConfig = _.assign(currentConfig, config)
return db.none(sql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION]) return db.none(sql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
@ -39,7 +39,7 @@ function saveConfig (config) {
} }
function loadLatest () { function loadLatest () {
return Promise.all([loadLatestConfig(), loadAccounts()]) return Promise.all([loadLatestConfigOrNone(), loadAccounts()])
.then(([config, accounts]) => ({ .then(([config, accounts]) => ({
config, config,
accounts accounts
@ -56,16 +56,24 @@ function loadLatestConfig () {
limit 1` limit 1`
return db.one(sql, ['config', NEW_SETTINGS_LOADER_SCHEMA_VERSION]) return db.one(sql, ['config', NEW_SETTINGS_LOADER_SCHEMA_VERSION])
.then(row => row.data.config) .then(row => row ? row.data.config : {})
.catch(err => { .catch(err => {
if (err.name === 'QueryResultError') {
throw new Error('No config was found')
}
throw err throw err
}) })
} }
function loadLatestConfigOrNone () {
const sql = `select data
from user_config
where type=$1
and schema_version=$2
order by id desc
limit 1`
return db.oneOrNone(sql, ['config', NEW_SETTINGS_LOADER_SCHEMA_VERSION])
.then(row => row ? row.data.config : {})
}
function loadConfig (versionId) { function loadConfig (versionId) {
const sql = `select data const sql = `select data
from user_config from user_config
@ -101,5 +109,6 @@ module.exports = {
loadAccounts, loadAccounts,
loadLatest, loadLatest,
loadLatestConfig, loadLatestConfig,
loadLatestConfigOrNone,
load load
} }