handle double dispense WIP

This commit is contained in:
Josh Harvey 2016-05-19 01:23:08 +03:00
parent adbd60b5a5
commit 1d323b0647
5 changed files with 107 additions and 3 deletions

View file

@ -283,8 +283,11 @@ function waitForDispense (req, res) {
function dispense (req, res) {
const tx = req.body.tx
const body = {dispense: true}
return plugins.requestDispense(session(req), tx)
.then(() => res.json(200))
.then(() => cacheResponse(req, body))
.then(() => res.json(body))
.catch(err => {
logger.error(err)
res.sendStatus(500)
@ -318,7 +321,7 @@ function init (localConfig) {
app.get('/phone_tx', authMiddleware, fetchPhoneTx)
app.post('/register_redeem', authMiddleware, registerRedeem)
app.get('/await_dispense', authMiddleware, waitForDispense)
app.post('/dispense', authMiddleware, dispense)
app.post('/dispense', authMiddleware, cachedResponse, dispense)
localApp.get('/pid', function (req, res) {
var machineFingerprint = req.query.fingerprint
@ -354,3 +357,12 @@ function getFingerprint (req) {
return (typeof req.connection.getPeerCertificate === 'function' &&
req.connection.getPeerCertificate().fingerprint) || 'unknown'
}
function cachedResponse (req, res, next) {
return plugins.cachedResponse(session(req), req.path, req.method)
.then(r => r.body ? res.json(r.body) : next())
}
function cacheResponse (req, body) {
return plugins.cacheResponse(session(req), req.path, req.method, body)
}