elide secrets
This commit is contained in:
parent
504a85b8c4
commit
ef0f1e02e9
4 changed files with 324 additions and 169 deletions
|
|
@ -1,4 +1,4 @@
|
|||
const R = require('ramda')
|
||||
const _ = require('lodash/fp')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
|
|
@ -42,11 +42,11 @@ function selectedAccounts () {
|
|||
.then(data => {
|
||||
if (!data) return []
|
||||
|
||||
const accountCodes = R.uniq(data.config.map(mapAccount)
|
||||
.filter(R.identity))
|
||||
const accountCodes = _.uniq(data.config.map(mapAccount)
|
||||
.filter(_.identity))
|
||||
|
||||
return R.sortBy(R.prop('display'), accountCodes.map(mapSchema)
|
||||
.filter(R.identity))
|
||||
return _.sortBy(_.get('display'), accountCodes.map(mapSchema)
|
||||
.filter(_.identity))
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -60,14 +60,14 @@ function mergeAccount (oldAccount, newAccount) {
|
|||
const newFields = newAccount.fields
|
||||
|
||||
const updateWithData = oldField => {
|
||||
const newField = R.find(r => r.code === oldField.code, newFields)
|
||||
const newValue = newField ? newField.value : null
|
||||
return R.assoc('value', newValue, oldField)
|
||||
const newField = _.find(r => r.code === oldField.code, newFields)
|
||||
const newValue = _.isUndefined(newField) ? oldField.value : newField.value
|
||||
return _.set('value', newValue, oldField)
|
||||
}
|
||||
|
||||
const updatedFields = oldAccount.fields.map(updateWithData)
|
||||
|
||||
return R.assoc('fields', updatedFields, oldAccount)
|
||||
return _.set('fields', updatedFields, oldAccount)
|
||||
}
|
||||
|
||||
function getAccounts (accountCode) {
|
||||
|
|
@ -76,17 +76,26 @@ function getAccounts (accountCode) {
|
|||
|
||||
return fetchAccounts()
|
||||
.then(accounts => {
|
||||
if (R.isEmpty(accounts)) return [schema]
|
||||
const account = R.find(r => r.code === accountCode, accounts)
|
||||
if (_.isEmpty(accounts)) return [schema]
|
||||
const account = _.find(r => r.code === accountCode, accounts)
|
||||
const mergedAccount = mergeAccount(schema, account)
|
||||
|
||||
return updateAccounts(mergedAccount, accounts)
|
||||
})
|
||||
}
|
||||
|
||||
function elideSecrets (account) {
|
||||
const elideSecret = field => field.fieldType === 'password'
|
||||
? _.set('value', !_.isNil(field.value), field)
|
||||
: field
|
||||
|
||||
return _.set('fields', account.fields.map(elideSecret), account)
|
||||
}
|
||||
|
||||
function getAccount (accountCode) {
|
||||
return getAccounts(accountCode)
|
||||
.then(accounts => R.find(r => r.code === accountCode, accounts))
|
||||
.then(accounts => _.find(r => r.code === accountCode, accounts))
|
||||
.then(elideSecrets)
|
||||
}
|
||||
|
||||
function save (accounts) {
|
||||
|
|
@ -95,20 +104,20 @@ function save (accounts) {
|
|||
|
||||
function updateAccounts (newAccount, accounts) {
|
||||
const accountCode = newAccount.code
|
||||
const isPresent = R.any(R.propEq('code', accountCode), accounts)
|
||||
const isPresent = _.some(_.matchesProperty('code', accountCode), accounts)
|
||||
const updateAccount = r => r.code === accountCode
|
||||
? newAccount
|
||||
: r
|
||||
|
||||
return isPresent
|
||||
? R.map(updateAccount, accounts)
|
||||
: R.append(newAccount, accounts)
|
||||
? _.map(updateAccount, accounts)
|
||||
: _.concat(accounts, newAccount)
|
||||
}
|
||||
|
||||
function updateAccount (account) {
|
||||
return getAccounts(account.code)
|
||||
.then(accounts => {
|
||||
const merged = mergeAccount(R.find(R.propEq('code', account.code), accounts), account)
|
||||
const merged = mergeAccount(_.find(_.matchesProperty('code', account.code), accounts), account)
|
||||
return save(updateAccounts(merged, accounts))
|
||||
})
|
||||
.then(() => getAccount(account.code))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue