From 35bada360dbf6a34d5b9ab829e26a4c380a5f188 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Fri, 30 Aug 2024 07:57:43 +0000 Subject: [PATCH] refactor(update-config): move logic to config model --- .../api/v1/admin/config/update.ee.js | 29 +------------- packages/backend/src/models/config.js | 40 +++++++++++++++++-- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/packages/backend/src/controllers/api/v1/admin/config/update.ee.js b/packages/backend/src/controllers/api/v1/admin/config/update.ee.js index a9e1f1b2..9c3e7801 100644 --- a/packages/backend/src/controllers/api/v1/admin/config/update.ee.js +++ b/packages/backend/src/controllers/api/v1/admin/config/update.ee.js @@ -4,35 +4,8 @@ import Config from '../../../../../models/config.js'; export default async (request, response) => { const config = configParams(request); - const configKeys = Object.keys(config); - const updates = []; - for (const key of configKeys) { - const newValue = config[key]; - - if (newValue) { - const entryUpdate = Config.query() - .insert({ - key, - value: { - data: newValue, - }, - }) - .onConflict('key') - .merge({ - value: { - data: newValue, - }, - }); - - updates.push(entryUpdate); - } else { - const entryUpdate = Config.query().findOne({ key }).delete(); - updates.push(entryUpdate); - } - } - - await Promise.all(updates); + await Config.batchUpdate(config); renderObject(response, config); }; diff --git a/packages/backend/src/models/config.js b/packages/backend/src/models/config.js index b65b6ece..71ee614a 100644 --- a/packages/backend/src/models/config.js +++ b/packages/backend/src/models/config.js @@ -15,14 +15,14 @@ class Config extends Base { }; static async isInstallationCompleted() { - const installationCompletedEntry = await this - .query() + const installationCompletedEntry = await this.query() .where({ - key: 'installation.completed' + key: 'installation.completed', }) .first(); - const installationCompleted = installationCompletedEntry?.value?.data === true; + const installationCompleted = + installationCompletedEntry?.value?.data === true; return installationCompleted; } @@ -35,6 +35,38 @@ class Config extends Base { }, }); } + + static async batchUpdate(config) { + const configKeys = Object.keys(config); + const updates = []; + + for (const key of configKeys) { + const newValue = config[key]; + + if (newValue) { + const entryUpdate = Config.query() + .insert({ + key, + value: { + data: newValue, + }, + }) + .onConflict('key') + .merge({ + value: { + data: newValue, + }, + }); + + updates.push(entryUpdate); + } else { + const entryUpdate = Config.query().findOne({ key }).delete(); + updates.push(entryUpdate); + } + } + + return await Promise.all(updates); + } } export default Config;