This commit is contained in:
Josh Harvey 2016-12-08 21:46:35 +02:00
parent a375adb8b9
commit c3261bc61a
5 changed files with 101 additions and 106 deletions

View file

@ -13,8 +13,7 @@ const db = require('./db')
const dbm = require('./postgresql_interface')
const pairing = require('./pairing')
const settingsLoader = require('./settings-loader')
let plugins
const plugins = require('./plugins')
module.exports = {
init
@ -30,12 +29,13 @@ function poll (req, res, next) {
const deviceId = req.deviceId
const deviceTime = req.deviceTime
const pid = req.query.pid
const settings = settingsLoader.settings()
const settings = req.settings
const config = configManager.machineScoped(deviceId, settings.config)
const pi = plugins(settings)
pids[deviceId] = {pid, ts: Date.now()}
plugins.pollQueries(deviceTime, deviceId, req.query)
pi.pollQueries(deviceTime, deviceId, req.query)
.then(results => {
const cartridges = results.cartridges
@ -77,24 +77,28 @@ function poll (req, res, next) {
function trade (req, res, next) {
const tx = req.body
const pi = plugins(req.settings)
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
plugins.trade(req.deviceId, tx)
pi.trade(req.deviceId, tx)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
function stateChange (req, res, next) {
plugins.stateChange(req.deviceId, req.deviceTime, req.body)
const pi = plugins(req.settings)
pi.stateChange(req.deviceId, req.deviceTime, req.body)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
function send (req, res, next) {
const pi = plugins(req.settings)
const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
return plugins.sendCoins(req.deviceId, tx)
return pi.sendCoins(req.deviceId, tx)
.then(status => {
const body = {txId: status && status.txId}
return cacheAndRespond(req, res, body)
@ -103,35 +107,40 @@ function send (req, res, next) {
}
function cashOut (req, res, next) {
const pi = plugins(req.settings)
logger.info({tx: req.body, cmd: 'cashOut'})
const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
return plugins.cashOut(req.deviceId, tx)
return pi.cashOut(req.deviceId, tx)
.then(cryptoAddress => cacheAndRespond(req, res, {toAddress: cryptoAddress}))
.catch(next)
}
function dispenseAck (req, res, next) {
plugins.dispenseAck(req.deviceId, req.body.tx)
const pi = plugins(req.settings)
pi.dispenseAck(req.deviceId, req.body.tx)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
function deviceEvent (req, res, next) {
plugins.logEvent(req.deviceId, req.body)
const pi = plugins(req.settings)
pi.logEvent(req.deviceId, req.body)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
function verifyUser (req, res, next) {
plugins.verifyUser(req.body)
const pi = plugins(req.settings)
pi.verifyUser(req.body)
.then(idResult => cacheAndRespond(req, res, idResult))
.catch(next)
}
function verifyTx (req, res, next) {
plugins.verifyTransaction(req.body)
const pi = plugins(req.settings)
pi.verifyTransaction(req.body)
.then(idResult => cacheAndRespond(req, res, idResult))
.catch(next)
}
@ -159,9 +168,10 @@ function pair (req, res, next) {
}
function phoneCode (req, res, next) {
const pi = plugins(req.settings)
const phone = req.body.phone
return plugins.getPhoneCode(phone)
return pi.getPhoneCode(phone)
.then(code => cacheAndRespond(req, res, {code}))
.catch(err => {
if (err.name === 'BadNumberError') throw httpError('Bad number', 410)
@ -180,7 +190,8 @@ function updatePhone (req, res, next) {
}
function fetchPhoneTx (req, res, next) {
return plugins.fetchPhoneTx(req.query.phone)
const pi = plugins(req.settings)
return pi.fetchPhoneTx(req.query.phone)
.then(r => res.json(r))
.catch(next)
}
@ -315,8 +326,6 @@ function authorize (req, res, next) {
}
function init (opts) {
plugins = opts.plugins
const skip = options.logLevel === 'debug'
? () => false
: (req, res) => _.includes(req.path, ['/poll', '/state']) && res.statusCode === 200