From 3a548fea6f7e82be63196892d0e475e732b47b07 Mon Sep 17 00:00:00 2001 From: siiky Date: Thu, 12 Sep 2024 17:24:24 +0100 Subject: [PATCH] refactor: extract duplicate code into its own function --- lib/new-settings-loader.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/new-settings-loader.js b/lib/new-settings-loader.js index 9d4d5d50..199c2481 100644 --- a/lib/new-settings-loader.js +++ b/lib/new-settings-loader.js @@ -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) }) }