refactor: extract duplicate code into its own function

This commit is contained in:
siiky 2024-09-12 17:24:24 +01:00
parent 6cf170303a
commit 3a548fea6f

View file

@ -56,6 +56,12 @@ const addTermsHash = configs => {
)(terms)
}
const notifyReload = (dbOrTx, operatorId) =>
dbOrTx.none(
'NOTIFY $1:name, $2',
['reload', JSON.stringify({ schema: asyncLocalStorage.getStore().get('schema'), operatorId })]
)
const accountsSql = `UPDATE user_config SET data = $2, valid = $3, schema_version = $4 WHERE type = $1;
INSERT INTO user_config (type, data, valid, schema_version)
SELECT $1, $2, $3, $4 WHERE $1 NOT IN (SELECT type FROM user_config)`
@ -76,7 +82,7 @@ function saveAccounts (accounts) {
return db.tx(t => {
return t.none(accountsSql, ['accounts', { accounts: newAccounts }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
.then(() => t.none('NOTIFY $1:name, $2', ['reload', JSON.stringify({ schema: asyncLocalStorage.getStore().get('schema'), operatorId })]))
.then(() => notifyReload(t, operatorId))
}).catch(console.error)
})
}
@ -118,7 +124,7 @@ function saveConfig (config) {
const newConfig = addTermsHash(_.assign(currentConfig, config))
return db.tx(t =>
insertConfigRow(t, { config: newConfig })
.then(() => t.none('NOTIFY $1:name, $2', ['reload', JSON.stringify({ schema: asyncLocalStorage.getStore().get('schema'), operatorId })]))
.then(() => notifyReload(t, operatorId))
).catch(console.error)
})
}
@ -129,7 +135,7 @@ function removeFromConfig (fields) {
const newConfig = _.omit(fields, currentConfig)
return db.tx(t =>
insertConfigRow(t, { config: newConfig })
.then(() => t.none('NOTIFY $1:name, $2', ['reload', JSON.stringify({ schema: asyncLocalStorage.getStore().get('schema'), operatorId })]))
.then(() => notifyReload(t, operatorId))
).catch(console.error)
})
}