From 11b5958c600000b0f15ccf87158a196ee42800ce Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Fri, 6 May 2016 14:11:57 +0300 Subject: [PATCH] WIP --- lib/plugins.js | 25 +++++++++++++++++++------ lib/postgresql_interface.js | 30 ++++++++++++++++++++++++++++++ lib/routes.js | 29 +++++++++++++++++++++++++++-- todo.txt | 12 +----------- 4 files changed, 77 insertions(+), 19 deletions(-) diff --git a/lib/plugins.js b/lib/plugins.js index 78033fd9..1d242a57 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -735,15 +735,28 @@ exports.fetchPhoneTx = function fetchPhoneTx (phone) { .then(txs => { const authorizedTxs = txs.filter(tx => tx.authorized) if (authorizedTxs.length > 0) { - return R.reduce((acc, val) => { + const maxTx = R.reduce((acc, val) => { !acc || val.cryptoAtoms.gt(acc.cryptoAtoms) ? val : acc }, null, authorizedTxs) + + return {tx: maxTx} } - if (txs.length > 0) { - // pending txs - } - - // no txs for this number + if (txs.length > 0) return {pending: true} + return {} }) } + +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) { diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index cf3e06b6..27f0298f 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -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) { var sql = 'UPDATE transactions SET dispense=$1 ' + 'WHERE stage=$2 AND authority=$3 AND device_fingerprint=$4 AND session_id=$5' diff --git a/lib/routes.js b/lib/routes.js index 60eec98e..377cfa6a 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -234,8 +234,31 @@ function updatePhone (req, res) { } function fetchPhoneTx (req, res) { - return plugins.updatePhone(req.query.phone) - .then(code => res.send(200)) + return plugins.fetchPhoneTx(req.query.phone) + .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 => { logger.error(err) res.send(500) @@ -268,6 +291,8 @@ function init (localConfig) { app.post('/phone_code', authMiddleware, phoneCode) app.post('/update-phone', authMiddleware, updatePhone) app.get('/phone-tx', authMiddleware, fetchPhoneTx) + app.get('/await-dispense', authMiddleware, waitForDispense) + app.post('/dispense', authMiddleware, dispense) localApp.get('/pid', function (req, res) { var machineFingerprint = req.query.fingerprint diff --git a/todo.txt b/todo.txt index 21baab50..2e4394cf 100644 --- a/todo.txt +++ b/todo.txt @@ -1,11 +1 @@ -- need a new table for undispensed cash-out txs -- 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? +- update tx statuses in background