fix blockcypher account admin

This commit is contained in:
Josh Harvey 2017-07-29 11:45:04 +03:00
parent dd2db492d6
commit 0fd1ffd620

View file

@ -9,26 +9,36 @@ const schemas = ph.loadSchemas()
function fetchAccounts () { function fetchAccounts () {
return db.oneOrNone('select data from user_config where type=$1', ['accounts']) return db.oneOrNone('select data from user_config where type=$1', ['accounts'])
.then(row => { .then(row => {
// Hard code this for now
const accounts = [{
code: 'blockcypher',
display: 'Blockcypher',
fields: [
{ code: 'confidenceFactor', display: 'Confidence Factor', fieldType: 'integer', required: true, value: 90 }
]
}]
return row return row
? Promise.resolve(row.data.accounts) ? Promise.resolve(row.data.accounts)
: db.none('insert into user_config (type, data, valid) values ($1, $2, $3)', ['accounts', {accounts: []}, true]) : db.none('insert into user_config (type, data, valid) values ($1, $2, $3)', ['accounts', {accounts}, true])
.then(fetchAccounts) .then(fetchAccounts)
}) })
} }
function selectedAccounts () { function selectedAccounts () {
const mapAccount = v => v.fieldLocator.fieldType === 'account' && const mapAccount = v => v.fieldLocator.fieldType === 'account' &&
v.fieldValue.value v.fieldValue.value
const mapSchema = code => schemas[code] const mapSchema = code => schemas[code]
return config.fetchConfig() return config.fetchConfig()
.then(conf => { .then(conf => {
const accountCodes = _.uniq(conf.map(mapAccount) const accountCodes = _.uniq(conf.map(mapAccount)
.filter(_.identity)) .filter(_.identity))
return _.sortBy(_.get('display'), accountCodes.map(mapSchema) return _.sortBy(_.get('display'), accountCodes.map(mapSchema)
.filter(_.identity)) .filter(_.identity))
}) })
} }
function fetchAccountSchema (account) { function fetchAccountSchema (account) {
@ -56,20 +66,20 @@ function getAccounts (accountCode) {
if (!schema) return Promise.reject(new Error('No schema for: ' + accountCode)) if (!schema) return Promise.reject(new Error('No schema for: ' + accountCode))
return fetchAccounts() return fetchAccounts()
.then(accounts => { .then(accounts => {
if (_.isEmpty(accounts)) return [schema] if (_.isEmpty(accounts)) return [schema]
const account = _.find(r => r.code === accountCode, accounts) const account = _.find(r => r.code === accountCode, accounts)
const mergedAccount = mergeAccount(schema, account) const mergedAccount = mergeAccount(schema, account)
return updateAccounts(mergedAccount, accounts) return updateAccounts(mergedAccount, accounts)
}) })
} }
function elideSecrets (account) { function elideSecrets (account) {
const elideSecret = field => { const elideSecret = field => {
return field.fieldType === 'password' return field.fieldType === 'password'
? _.set('value', !_.isEmpty(field.value), field) ? _.set('value', !_.isEmpty(field.value), field)
: field : field
} }
return _.set('fields', account.fields.map(elideSecret), account) return _.set('fields', account.fields.map(elideSecret), account)
@ -77,8 +87,8 @@ function elideSecrets (account) {
function getAccount (accountCode) { function getAccount (accountCode) {
return getAccounts(accountCode) return getAccounts(accountCode)
.then(accounts => _.find(r => r.code === accountCode, accounts)) .then(accounts => _.find(r => r.code === accountCode, accounts))
.then(elideSecrets) .then(elideSecrets)
} }
function save (accounts) { function save (accounts) {
@ -89,21 +99,21 @@ function updateAccounts (newAccount, accounts) {
const accountCode = newAccount.code const accountCode = newAccount.code
const isPresent = _.some(_.matchesProperty('code', accountCode), accounts) const isPresent = _.some(_.matchesProperty('code', accountCode), accounts)
const updateAccount = r => r.code === accountCode const updateAccount = r => r.code === accountCode
? newAccount ? newAccount
: r : r
return isPresent return isPresent
? _.map(updateAccount, accounts) ? _.map(updateAccount, accounts)
: _.concat(accounts, newAccount) : _.concat(accounts, newAccount)
} }
function updateAccount (account) { function updateAccount (account) {
return getAccounts(account.code) return getAccounts(account.code)
.then(accounts => { .then(accounts => {
const merged = mergeAccount(_.find(_.matchesProperty('code', account.code), accounts), account) const merged = mergeAccount(_.find(_.matchesProperty('code', account.code), accounts), account)
return save(updateAccounts(merged, accounts)) return save(updateAccounts(merged, accounts))
}) })
.then(() => getAccount(account.code)) .then(() => getAccount(account.code))
} }
module.exports = { module.exports = {