refactor: extract duplicate code into its own function
This commit is contained in:
parent
47692f4456
commit
6cf170303a
1 changed files with 13 additions and 8 deletions
|
|
@ -106,15 +106,20 @@ function showAccounts (schemaVersion) {
|
|||
})
|
||||
}
|
||||
|
||||
const configSql = 'INSERT INTO user_config (type, data, valid, schema_version) VALUES ($1, $2, $3, $4)'
|
||||
const insertConfigRow = (dbOrTx, data) =>
|
||||
dbOrTx.none(
|
||||
"INSERT INTO user_config (type, data, valid, schema_version) VALUES ('config', $1, TRUE, $2)",
|
||||
[data, NEW_SETTINGS_LOADER_SCHEMA_VERSION]
|
||||
)
|
||||
|
||||
function saveConfig (config) {
|
||||
return Promise.all([loadLatestConfigOrNone(), getOperatorId('middleware')])
|
||||
.then(([currentConfig, operatorId]) => {
|
||||
const newConfig = addTermsHash(_.assign(currentConfig, config))
|
||||
return db.tx(t => {
|
||||
return t.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
return db.tx(t =>
|
||||
insertConfigRow(t, { config: newConfig })
|
||||
.then(() => t.none('NOTIFY $1:name, $2', ['reload', JSON.stringify({ schema: asyncLocalStorage.getStore().get('schema'), operatorId })]))
|
||||
}).catch(console.error)
|
||||
).catch(console.error)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -122,10 +127,10 @@ function removeFromConfig (fields) {
|
|||
return Promise.all([loadLatestConfigOrNone(), getOperatorId('middleware')])
|
||||
.then(([currentConfig, operatorId]) => {
|
||||
const newConfig = _.omit(fields, currentConfig)
|
||||
return db.tx(t => {
|
||||
return t.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
return db.tx(t =>
|
||||
insertConfigRow(t, { config: newConfig })
|
||||
.then(() => t.none('NOTIFY $1:name, $2', ['reload', JSON.stringify({ schema: asyncLocalStorage.getStore().get('schema'), operatorId })]))
|
||||
}).catch(console.error)
|
||||
).catch(console.error)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -133,7 +138,7 @@ function migrationSaveConfig (config) {
|
|||
return loadLatestConfigOrNone()
|
||||
.then(currentConfig => {
|
||||
const newConfig = _.assign(currentConfig, config)
|
||||
return db.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
return insertConfigRow(db, { config: newConfig })
|
||||
.catch(console.error)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue