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

@ -568,6 +568,7 @@ exports.addDispenseRequest = function addDispenseRequest (session, tx) {
'dispense', 'pending')
], function (err) {
done()
if (err) return reject(err)
resolve()
})
@ -582,7 +583,8 @@ function updateDispense (client, session, dispensed, cb) {
var values = [dispensed, 'initial_request', 'pending', session.fingerprint, session.id, true]
query(client, sql, values, function (err, results) {
if (err) return cb(err)
if (results.rowCount === 0) return cb(new Error('No pending tx'))
// DEBUG10
// if (results.rowCount === 0) return cb(new Error('No pending tx'))
cb()
})
}
@ -768,6 +770,50 @@ exports.updateNotify = function updateNotify (tx) {
})
}
function insertCachedRequest (session, path, method, body) {
const fields = [
'device_fingerprint',
'session_id',
'path',
'method',
'body'
]
const sql = getInsertQuery('cached_requests', fields)
return pquery(sql, [session.fingerprint, session.id, path, method, body])
}
exports.cachedResponse = function (session, path, method) {
const sql = `select body from cached_requests
where device_fingerprint=$1
and session_id=$2
and path=$3
and method=$4`
const values = [session.fingerprint, session.id, path, method]
return insertCachedRequest(session, path, method, {pendingRequest: true})
.then(() => ({}))
.catch(err => {
if (!isUniqueViolation(err)) throw err
return pquery(sql, values)
.then(r => ({body: r.rows[0].body}))
})
}
exports.cacheResponse = function (session, path, method, body) {
const sql = `update cached_requests
set body=$1
where device_fingerprint=$2
and session_id=$3
and path=$4
and method=$5`
const values = [body, session.fingerprint, session.id, path, method]
return pquery(sql, values)
}
/*
exports.init('postgres://lamassu:lamassu@localhost/lamassu')
connect(function(err, client, done) {