feat: server allows to save any amount of accounts

This commit is contained in:
José Oliveira 2021-02-15 18:54:55 +00:00 committed by Josh Harvey
parent 351d2a078b
commit 80cbebecc4

View file

@ -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])
})