This commit is contained in:
Josh Harvey 2016-05-06 14:11:57 +03:00
parent f91992e20d
commit 11b5958c60
4 changed files with 77 additions and 19 deletions

View file

@ -735,15 +735,28 @@ exports.fetchPhoneTx = function fetchPhoneTx (phone) {
.then(txs => { .then(txs => {
const authorizedTxs = txs.filter(tx => tx.authorized) const authorizedTxs = txs.filter(tx => tx.authorized)
if (authorizedTxs.length > 0) { if (authorizedTxs.length > 0) {
return R.reduce((acc, val) => { const maxTx = R.reduce((acc, val) => {
!acc || val.cryptoAtoms.gt(acc.cryptoAtoms) ? val : acc !acc || val.cryptoAtoms.gt(acc.cryptoAtoms) ? val : acc
}, null, authorizedTxs) }, null, authorizedTxs)
return {tx: maxTx}
} }
if (txs.length > 0) { if (txs.length > 0) return {pending: true}
// pending txs return {}
}
// no txs for this number
}) })
} }
exports.fetchTx = function fetchTx (session, status) {
return db.fetchTx(session)
.then(txRecs => {
const deposits = txRecs.filter(t => t.stage === 'deposit')
const deposit = R.last(deposits)
const status = deposit ? deposit.authority : 'notSeen'
const tx = txRecs.find(t => t.stage === 'initial_request')
if (!tx) return null
return R.assoc('status', status, tx)
})
}
exports.fetchTx = function fetchTx (session, status) {

View file

@ -500,6 +500,36 @@ exports.addIncomingPhone = function addIncomingPhone (session, tx, cb) {
}) })
} }
exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout, cb) {
var sql = 'SELECT * FROM transactions ' +
'WHERE phone=$1 AND dispensed=$2 ' +
'AND (EXTRACT(EPOCH FROM (now() - created))) * 1000 < $1'
connect(function (cerr, client, done) {
if (cerr) return cb(cerr)
query(client, sql, [phone, false, dispenseTimeout], function (err, results) {
done()
if (err) return cb(err)
cb(null, results.rows)
})
})
}
exports.fetchTx = function fetchTx (session, cb) {
var sql = 'SELECT * FROM transactions ' +
'WHERE device_fingerprint=$1 AND session_id=$2 ' +
'ORDER BY created'
connect(function (cerr, client, done) {
if (cerr) return cb(cerr)
query(client, sql, [session.fingerprint, session.id], function (err, results) {
done()
if (err) return cb(err)
cb(null, results.rows)
})
})
}
function updateDispense (client, session, dispensed, cb) { function updateDispense (client, session, dispensed, cb) {
var sql = 'UPDATE transactions SET dispense=$1 ' + var sql = 'UPDATE transactions SET dispense=$1 ' +
'WHERE stage=$2 AND authority=$3 AND device_fingerprint=$4 AND session_id=$5' 'WHERE stage=$2 AND authority=$3 AND device_fingerprint=$4 AND session_id=$5'

View file

@ -234,8 +234,31 @@ function updatePhone (req, res) {
} }
function fetchPhoneTx (req, res) { function fetchPhoneTx (req, res) {
return plugins.updatePhone(req.query.phone) return plugins.fetchPhoneTx(req.query.phone)
.then(code => res.send(200)) .then(r => res.json(r))
.catch(err => {
logger.error(err)
res.send(500)
})
}
function waitForDispense (req, res) {
return plugins.fetchTx(session(req), req.query.status)
.then(r => {
if (!r) return res.send(404)
if (r.status === req.query.status) return res.send(304)
res.json(r)
})
.catch(err => {
logger.error(err)
res.send(500)
})
}
function dispense (req, res) {
const originalSessionId = req.query['original-session-id']
return plugins.addDispense(session(req).fingerprint, originalSessionId)
.then(() => res.send(200))
.catch(err => { .catch(err => {
logger.error(err) logger.error(err)
res.send(500) res.send(500)
@ -268,6 +291,8 @@ function init (localConfig) {
app.post('/phone_code', authMiddleware, phoneCode) app.post('/phone_code', authMiddleware, phoneCode)
app.post('/update-phone', authMiddleware, updatePhone) app.post('/update-phone', authMiddleware, updatePhone)
app.get('/phone-tx', authMiddleware, fetchPhoneTx) app.get('/phone-tx', authMiddleware, fetchPhoneTx)
app.get('/await-dispense', authMiddleware, waitForDispense)
app.post('/dispense', authMiddleware, dispense)
localApp.get('/pid', function (req, res) { localApp.get('/pid', function (req, res) {
var machineFingerprint = req.query.fingerprint var machineFingerprint = req.query.fingerprint

View file

@ -1,11 +1 @@
- need a new table for undispensed cash-out txs - update tx statuses in background
- or add dispensed flag to txs
- need to update cash-out tx status based on authorization and confirmation
- add dispensed and authorized booleans to txs
- for fetching phones: fetch all non-dispensed rows for phone
- get max by satoshis of all authorized (why not consolidate all authorized? next version)
- if no authorized, check if any non-dispensed and non-authorized
- index by phone + dispensed?