From 44b46bd5ee6956cf15d74ce0213f556203f3a70a Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Mon, 27 Jun 2016 00:31:50 +0100 Subject: [PATCH] WIP --- lib/plugins.js | 11 ++++++----- lib/postgresql_interface.js | 12 +++++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/plugins.js b/lib/plugins.js index 73d01813..856ce539 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -298,14 +298,16 @@ function _sendCoinsCb (toAddress, cryptoAtoms, cryptoCode, cb) { } } -function executeTx (session, tx) { - return db.addOutgoingTx(session, tx) +// NOTE: This will fail if we have already sent coins because there will be +// a db unique db record in the table already. +function executeTx (fingerprint, tx) { + return db.addOutgoingTx(fingerprint, tx) .then(() => _sendCoins(tx.toAddress, tx.cryptoAtoms, tx.cryptoCode)) .then(txHash => { const fee = null // Need to fill this out in plugins const toSend = {cryptoAtoms: tx.cryptoAtoms, fiat: tx.fiat} - return db.sentCoins(session, tx, toSend, fee, null, txHash) + return db.sentCoins(tx, toSend, fee, null, txHash) .then(() => pollBalance(tx.cryptoCode)) .then(() => ({ statusCode: 201, // Created @@ -358,8 +360,7 @@ exports.recordPing = function recordPing (session, rec, cb) { } exports.sendCoins = function sendCoins (session, rawTx) { - const _session = {id: rawTx.sessionId || session.id, fingerprint: session.fingerprint} - return executeTx(_session, rawTx) + return executeTx(session.fingerprint, rawTx) } exports.cashOut = function cashOut (session, tx) { diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index 21284af2..fd321a95 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -81,15 +81,17 @@ exports.recordDeviceEvent = function recordDeviceEvent (session, event) { return db.none(sql, values) } -exports.addOutgoingTx = function addOutgoingTx (session, tx) { +// NOTE: This will fail if we have already sent coins because there will be +// a unique cash_in_txs record in the table already keyed by sessionId. +exports.addOutgoingTx = function addOutgoingTx (fingerprint, tx) { const fields = ['session_id', 'device_fingerprint', 'to_address', 'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash', 'fee', 'phone', 'error' ] const values = [ - session.id, - session.fingerprint, + tx.sessionId, + fingerprint, tx.toAddress, tx.cryptoAtoms.toString(), tx.cryptoCode, @@ -104,9 +106,9 @@ exports.addOutgoingTx = function addOutgoingTx (session, tx) { return db.none(getInsertQuery('cash_in_txs', fields), values) } -exports.sentCoins = function sentCoins (session, tx, toSend, fee, error, txHash) { +exports.sentCoins = function sentCoins (tx, toSend, fee, error, txHash) { const sql = `update cash_in_txs set tx_hash=$1, error=$2 where session_id=$3` - return db.none(sql, [txHash, error, session.id]) + return db.none(sql, [txHash, error, tx.sessionId]) } exports.addInitialIncoming = function addInitialIncoming (session, tx) {