improve config saving; fix pairing bug
This commit is contained in:
parent
67e12b19cb
commit
f3f46f7b5b
4 changed files with 81 additions and 9 deletions
|
|
@ -6,14 +6,12 @@ const baseX = require('base-x')
|
||||||
|
|
||||||
const options = require('../options')
|
const options = require('../options')
|
||||||
const db = require('../db')
|
const db = require('../db')
|
||||||
|
const pairing = require('../pairing')
|
||||||
|
|
||||||
const ALPHA_BASE = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'
|
const ALPHA_BASE = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'
|
||||||
const bsAlpha = baseX(ALPHA_BASE)
|
const bsAlpha = baseX(ALPHA_BASE)
|
||||||
|
|
||||||
function unpair (deviceId) {
|
const unpair = pairing.unpair
|
||||||
const sql = 'delete from devices where device_id=$1'
|
|
||||||
return db.none(sql, [deviceId])
|
|
||||||
}
|
|
||||||
|
|
||||||
function totem (hostname, name) {
|
function totem (hostname, name) {
|
||||||
const caPath = options.caPath
|
const caPath = options.caPath
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ const _ = require('lodash/fp')
|
||||||
const configManager = require('./config-manager')
|
const configManager = require('./config-manager')
|
||||||
const machines = require('./admin/machines')
|
const machines = require('./admin/machines')
|
||||||
const schema = require('../lamassu-schema.json')
|
const schema = require('../lamassu-schema.json')
|
||||||
|
const logger = require('./logger')
|
||||||
|
|
||||||
function allScopes (cryptoScopes, machineScopes) {
|
function allScopes (cryptoScopes, machineScopes) {
|
||||||
const scopes = []
|
const scopes = []
|
||||||
|
|
@ -47,7 +48,9 @@ function satisfiesRequire (config, cryptos, machineList, field, refFields) {
|
||||||
const isBlank = _.isNil(configManager.scopedValue(scope[0], scope[1], fieldCode, config))
|
const isBlank = _.isNil(configManager.scopedValue(scope[0], scope[1], fieldCode, config))
|
||||||
const isRequired = refFields.length === 0 || isEnabled()
|
const isRequired = refFields.length === 0 || isEnabled()
|
||||||
|
|
||||||
return isRequired ? !isBlank : true
|
const isValid = isRequired ? !isBlank : true
|
||||||
|
|
||||||
|
return isValid
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -148,6 +151,7 @@ function validate (config) {
|
||||||
.then(() => validateRequires(config))
|
.then(() => validateRequires(config))
|
||||||
.then(arr => {
|
.then(arr => {
|
||||||
if (arr.length === 0) return config
|
if (arr.length === 0) return config
|
||||||
|
logger.error('Invalid configuration: %j', arr)
|
||||||
throw new Error('Invalid configuration')
|
throw new Error('Invalid configuration')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ const readFile = pify(fs.readFile)
|
||||||
const db = require('./db')
|
const db = require('./db')
|
||||||
const options = require('./options')
|
const options = require('./options')
|
||||||
const logger = require('./logger')
|
const logger = require('./logger')
|
||||||
|
const settingsLoader = require('./settings-loader')
|
||||||
|
|
||||||
function pullToken (token) {
|
function pullToken (token) {
|
||||||
const sql = `delete from pairing_tokens
|
const sql = `delete from pairing_tokens
|
||||||
|
|
@ -12,6 +13,26 @@ function pullToken (token) {
|
||||||
return db.one(sql, [token])
|
return db.one(sql, [token])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function configureNewDevice (deviceId) {
|
||||||
|
const scope = {crypto: 'global', machine: deviceId}
|
||||||
|
const newFields = [settingsLoader.configAddField(scope, 'cashOutEnabled', false)]
|
||||||
|
|
||||||
|
return settingsLoader.modifyConfig(newFields)
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeDeviceConfig (deviceId) {
|
||||||
|
const scope = {crypto: 'global', machine: deviceId}
|
||||||
|
const newFields = [settingsLoader.configDeleteField(scope, 'cashOutEnabled', false)]
|
||||||
|
|
||||||
|
return settingsLoader.modifyConfig(newFields)
|
||||||
|
}
|
||||||
|
|
||||||
|
function unpair (deviceId) {
|
||||||
|
const sql = 'delete from devices where device_id=$1'
|
||||||
|
return db.none(sql, [deviceId])
|
||||||
|
.then(() => removeDeviceConfig(deviceId))
|
||||||
|
}
|
||||||
|
|
||||||
function pair (token, deviceId) {
|
function pair (token, deviceId) {
|
||||||
return pullToken(token)
|
return pullToken(token)
|
||||||
.then(r => {
|
.then(r => {
|
||||||
|
|
@ -21,7 +42,8 @@ function pair (token, deviceId) {
|
||||||
on conflict (device_id)
|
on conflict (device_id)
|
||||||
do update set name=$2, paired=TRUE, display=TRUE`
|
do update set name=$2, paired=TRUE, display=TRUE`
|
||||||
|
|
||||||
return db.none(insertSql, [deviceId, r.name])
|
return configureNewDevice(deviceId)
|
||||||
|
.then(() => db.none(insertSql, [deviceId, r.name]))
|
||||||
.then(() => true)
|
.then(() => true)
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
|
|
@ -47,4 +69,4 @@ function isPaired (deviceId) {
|
||||||
.then(row => row && row.device_id === deviceId)
|
.then(row => row && row.device_id === deviceId)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {pair, authorizeCaDownload, isPaired}
|
module.exports = {pair, unpair, authorizeCaDownload, isPaired}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ const _ = require('lodash/fp')
|
||||||
const argv = require('minimist')(process.argv.slice(2))
|
const argv = require('minimist')(process.argv.slice(2))
|
||||||
const pify = require('pify')
|
const pify = require('pify')
|
||||||
|
|
||||||
|
const pgp = require('pg-promise')()
|
||||||
const db = require('./db')
|
const db = require('./db')
|
||||||
const configValidate = require('./config-validate')
|
const configValidate = require('./config-validate')
|
||||||
|
|
||||||
|
|
@ -39,7 +40,7 @@ function isEquivalentField (a, b) {
|
||||||
|
|
||||||
// b overrides a
|
// b overrides a
|
||||||
function mergeValues (a, b) {
|
function mergeValues (a, b) {
|
||||||
return _.unionWith(isEquivalentField, b, a)
|
return _.reject(r => _.isNil(r.fieldValue), _.unionWith(isEquivalentField, b, a))
|
||||||
}
|
}
|
||||||
|
|
||||||
function load (versionId) {
|
function load (versionId) {
|
||||||
|
|
@ -125,6 +126,50 @@ function save (config) {
|
||||||
.catch(() => db.none(sql, ['config', {config}, false]))
|
.catch(() => db.none(sql, ['config', {config}, false]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function configAddField (scope, fieldCode, value) {
|
||||||
|
return {
|
||||||
|
fieldLocator: {
|
||||||
|
fieldScope: {
|
||||||
|
crypto: scope.crypto,
|
||||||
|
machine: scope.machine
|
||||||
|
},
|
||||||
|
code: fieldCode
|
||||||
|
},
|
||||||
|
fieldValue: {value}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function configDeleteField (scope, fieldCode) {
|
||||||
|
return {
|
||||||
|
fieldLocator: {
|
||||||
|
fieldScope: {
|
||||||
|
crypto: scope.crypto,
|
||||||
|
machine: scope.machine
|
||||||
|
},
|
||||||
|
code: fieldCode
|
||||||
|
},
|
||||||
|
fieldValue: null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function modifyConfig (newFields) {
|
||||||
|
const TransactionMode = pgp.txMode.TransactionMode
|
||||||
|
const isolationLevel = pgp.txMode.isolationLevel
|
||||||
|
const tmSRD = new TransactionMode({tiLevel: isolationLevel.serializable})
|
||||||
|
|
||||||
|
function transaction (t) {
|
||||||
|
return loadLatest()
|
||||||
|
.then(settings => {
|
||||||
|
const oldConfig = settings.config
|
||||||
|
return save(mergeValues(oldConfig, newFields))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction.txMode = tmSRD
|
||||||
|
|
||||||
|
return db.tx(transaction)
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
settings,
|
settings,
|
||||||
loadConfig,
|
loadConfig,
|
||||||
|
|
@ -132,5 +177,8 @@ module.exports = {
|
||||||
loadLatest,
|
loadLatest,
|
||||||
save,
|
save,
|
||||||
loadFixture,
|
loadFixture,
|
||||||
mergeValues
|
mergeValues,
|
||||||
|
modifyConfig,
|
||||||
|
configAddField,
|
||||||
|
configDeleteField
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue