From 80cbebecc44ee6a1e40c0f9a3b4ba39975f28b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Mon, 15 Feb 2021 18:54:55 +0000 Subject: [PATCH] feat: server allows to save any amount of accounts --- lib/new-settings-loader.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/new-settings-loader.js b/lib/new-settings-loader.js index 8ae488f5..a1cbd243 100644 --- a/lib/new-settings-loader.js +++ b/lib/new-settings-loader.js @@ -21,11 +21,16 @@ const SECRET_FIELDS = [ 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)` -function saveAccounts (accountsToSave) { +function saveAccounts (accounts) { return loadAccounts() .then(currentAccounts => { - const serviceCode = _.keys(accountsToSave)[0] - accountsToSave[serviceCode] = _.omitBy(_.isEmpty, accountsToSave[serviceCode]) + const serviceCodes = _.keys(accounts) + const filledAccountInfo = _.map(code => { return { [code]: _.omitBy(_.isEmpty, accounts[code]) } }, serviceCodes) + const accountsToSave = _.reduce((result, item) => { + var key = Object.keys(item)[0] + result[key] = item[key] + return result + }, {}, filledAccountInfo) const newAccounts = _.merge(currentAccounts, accountsToSave) return db.none(accountsSql, ['accounts', { accounts: newAccounts }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION]) })