fix: make accounts a object

This commit is contained in:
Taranto 2020-04-22 17:28:11 +01:00
parent ec73f0b022
commit 6b3db134e7
7 changed files with 26 additions and 38 deletions

View file

@ -196,7 +196,7 @@ const typeDefs = gql`
uptime: [ProcessStatus]
serverLogs: [ServerLog]
transactions: [Transaction]
accounts: [JSONObject]
accounts: JSONObject
config: JSONObject
}
@ -221,8 +221,7 @@ const typeDefs = gql`
setCustomer(customerId: ID!, customerInput: CustomerInput): Customer
saveConfig(config: JSONObject): JSONObject
createPairingTotem(name: String!): String
saveAccount(account: JSONObject): [JSONObject]
saveAccounts(accounts: [JSONObject]): [JSONObject]
saveAccounts(accounts: JSONObject): JSONObject
}
`
@ -259,7 +258,6 @@ const resolvers = {
machineSupportLogs: (...[, { deviceId }]) => supportLogs.insert(deviceId),
createPairingTotem: (...[, { name }]) => pairing.totem(name),
serverSupportLogs: () => serverLogs.insert(),
saveAccount: (...[, { account }]) => settingsLoader.saveAccounts([account]),
saveAccounts: (...[, { accounts }]) => settingsLoader.saveAccounts(accounts),
setCustomer: (...[, { customerId, customerInput } ]) => customers.updateCustomer(customerId, customerInput),
saveConfig: (...[, { config }]) => settingsLoader.saveConfig(config)

View file

@ -9,20 +9,11 @@ low(adapter).then(it => {
db = it
})
function replace (array, index, value) {
return array.slice(0, index).concat([value]).concat(array.slice(index + 1))
}
function replaceOrAdd (accounts, account) {
const index = _.findIndex(['code', account.code], accounts)
return index !== -1 ? replace(accounts, index, account) : _.concat(accounts)(account)
}
function saveAccounts (accountsToSave) {
const currentState = db.getState() || {}
const accounts = currentState.accounts || []
const accounts = currentState.accounts || {}
const newAccounts = _.reduce(replaceOrAdd)(accounts)(accountsToSave)
const newAccounts = _.assign(accounts)(accountsToSave)
const newState = _.set('accounts', newAccounts, currentState)
db.setState(newState)