normalize db records to server records
This commit is contained in:
parent
c4e557e86d
commit
59e93445ec
4 changed files with 59 additions and 47 deletions
|
|
@ -83,7 +83,7 @@ exports.recordBill = function recordBill (session, rec, cb) {
|
|||
rec.toAddress,
|
||||
session.id,
|
||||
rec.deviceTime,
|
||||
rec.satoshis,
|
||||
rec.cryptoAtoms,
|
||||
rec.fiat
|
||||
]
|
||||
|
||||
|
|
@ -149,7 +149,7 @@ function silentQuery (client, queryStr, values, cb) {
|
|||
})
|
||||
}
|
||||
|
||||
// OPTIMIZE: No need to query bills if tx.fiat and tx.satoshis are set
|
||||
// OPTIMIZE: No need to query bills if tx.fiat and tx.cryptoAtoms are set
|
||||
function billsAndTxs (client, session, cb) {
|
||||
var sessionId = session.id
|
||||
var fingerprint = session.fingerprint
|
||||
|
|
@ -174,9 +174,9 @@ function billsAndTxs (client, session, cb) {
|
|||
// we need to parse, since we know these won't be huge numbers.
|
||||
cb(null, {
|
||||
billsFiat: parseInt(results[0].rows[0].fiat, 10),
|
||||
billsSatoshis: new BigNumber(results[0].rows[0].satoshis),
|
||||
billsCryptoAtoms: new BigNumber(results[0].rows[0].satoshis),
|
||||
txFiat: parseInt(results[1].rows[0].fiat, 10),
|
||||
txSatoshis: new BigNumber(results[1].rows[0].satoshis)
|
||||
txCryptoAtoms: new BigNumber(results[1].rows[0].satoshis)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
@ -184,20 +184,20 @@ function billsAndTxs (client, session, cb) {
|
|||
function computeSendAmount (tx, totals) {
|
||||
var fiatRemaining = (tx.fiat || totals.billsFiat) - totals.txFiat
|
||||
|
||||
var satoshisRemaining = tx.satoshis.eq(0)
|
||||
? totals.billsSatoshis.minus(totals.txSatoshis)
|
||||
: tx.satoshis.minus(totals.txSatoshis)
|
||||
var cryptoAtomsRemaining = tx.cryptoAtoms.eq(0)
|
||||
? totals.billsCryptoAtoms.minus(totals.txCryptoAtoms)
|
||||
: tx.cryptoAtoms.minus(totals.txCryptoAtoms)
|
||||
|
||||
var result = {
|
||||
fiat: fiatRemaining,
|
||||
satoshis: satoshisRemaining
|
||||
cryptoAtoms: cryptoAtomsRemaining
|
||||
}
|
||||
if (result.fiat < 0 || result.satoshis.lt(0)) {
|
||||
if (result.fiat < 0 || result.cryptoAtoms.lt(0)) {
|
||||
logger.warn({tx: tx, totals: totals, result: result},
|
||||
"computeSendAmount result < 0, this shouldn't happen. " +
|
||||
'Maybe timeout arrived after machineSend.')
|
||||
result.fiat = 0
|
||||
result.satoshis = new BigNumber(0)
|
||||
result.cryptoAtoms = new BigNumber(0)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
@ -227,7 +227,7 @@ exports.pendingTxs = function pendingTxs (timeoutMS, cb) {
|
|||
var values = [timeoutS]
|
||||
query(client, sql, values, function (err, results) {
|
||||
done()
|
||||
cb(err, results)
|
||||
cb(err, normalizeTxs(results))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
@ -242,14 +242,14 @@ function insertOutgoingTx (client, session, tx, totals, cb) {
|
|||
var sendAmount = computeSendAmount(tx, totals)
|
||||
var stage = 'partial_request'
|
||||
var authority = tx.fiat ? 'machine' : 'timeout'
|
||||
var satoshis = sendAmount.satoshis
|
||||
var cryptoAtoms = sendAmount.cryptoAtoms
|
||||
var fiat = sendAmount.fiat
|
||||
if (satoshis.eq(0)) return cb(null, {satoshis: new BigNumber(0), fiat: 0})
|
||||
if (cryptoAtoms.eq(0)) return cb(null, {cryptoAtoms: new BigNumber(0), fiat: 0})
|
||||
|
||||
insertOutgoing(client, session, tx, satoshis, fiat, stage, authority,
|
||||
insertOutgoing(client, session, tx, cryptoAtoms, fiat, stage, authority,
|
||||
function (err) {
|
||||
if (err) return cb(err)
|
||||
cb(null, {satoshis: satoshis, fiat: fiat})
|
||||
cb(null, {cryptoAtoms: cryptoAtoms, fiat: fiat})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -259,22 +259,22 @@ function insertOutgoingCompleteTx (client, session, tx, cb) {
|
|||
|
||||
var stage = 'final_request'
|
||||
var authority = 'machine'
|
||||
var satoshis = tx.satoshis
|
||||
var cryptoAtoms = tx.cryptoAtoms
|
||||
var fiat = tx.fiat
|
||||
insertOutgoing(client, session, tx, satoshis, fiat, stage, authority, cb)
|
||||
insertOutgoing(client, session, tx, cryptoAtoms, fiat, stage, authority, cb)
|
||||
}
|
||||
|
||||
function insertIncoming (client, session, tx, satoshis, fiat, stage, authority, cb) {
|
||||
var realSatoshis = satoshis || new BigNumber(0)
|
||||
insertTx(client, session, true, tx, realSatoshis, fiat, stage, authority, cb)
|
||||
function insertIncoming (client, session, tx, cryptoAtoms, fiat, stage, authority, cb) {
|
||||
var realCryptoAtoms = cryptoAtoms || new BigNumber(0)
|
||||
insertTx(client, session, true, tx, realCryptoAtoms, fiat, stage, authority, cb)
|
||||
}
|
||||
|
||||
function insertOutgoing (client, session, tx, satoshis, fiat, stage, authority,
|
||||
function insertOutgoing (client, session, tx, cryptoAtoms, fiat, stage, authority,
|
||||
cb) {
|
||||
insertTx(client, session, false, tx, satoshis, fiat, stage, authority, cb)
|
||||
insertTx(client, session, false, tx, cryptoAtoms, fiat, stage, authority, cb)
|
||||
}
|
||||
|
||||
function insertTx (client, session, incoming, tx, satoshis, fiat, stage,
|
||||
function insertTx (client, session, incoming, tx, cryptoAtoms, fiat, stage,
|
||||
authority, cb) {
|
||||
var fields = [
|
||||
'session_id',
|
||||
|
|
@ -299,7 +299,7 @@ function insertTx (client, session, incoming, tx, satoshis, fiat, stage,
|
|||
incoming,
|
||||
session.fingerprint,
|
||||
tx.toAddress,
|
||||
satoshis.toString(),
|
||||
cryptoAtoms,
|
||||
tx.currencyCode,
|
||||
tx.cryptoCode,
|
||||
fiat,
|
||||
|
|
@ -328,12 +328,12 @@ function refreshPendingTx (client, session, cb) {
|
|||
}
|
||||
|
||||
function addPendingTx (client, session, incoming, currencyCode, cryptoCode, toAddress,
|
||||
satoshis, cb) {
|
||||
cryptoAtoms, cb) {
|
||||
var fields = ['device_fingerprint', 'session_id', 'incoming',
|
||||
'currency_code', 'crypto_code', 'to_address', 'satoshis']
|
||||
var sql = getInsertQuery('pending_transactions', fields, false)
|
||||
var values = [session.fingerprint, session.id, incoming, currencyCode,
|
||||
cryptoCode, toAddress, satoshis.toString()]
|
||||
cryptoCode, toAddress, cryptoAtoms.toString()]
|
||||
query(client, sql, values, function (err) {
|
||||
cb(err)
|
||||
})
|
||||
|
|
@ -346,7 +346,7 @@ function buildOutgoingTx (client, session, tx, cb) {
|
|||
], cb)
|
||||
}
|
||||
|
||||
// Calling function should only send bitcoins if result.satoshisToSend > 0
|
||||
// Calling function should only send bitcoins if result.cryptoAtomsToSend > 0
|
||||
exports.addOutgoingTx = function addOutgoingTx (session, tx, cb) {
|
||||
connect(function (cerr, client, done) {
|
||||
if (cerr) return cb(cerr)
|
||||
|
|
@ -378,7 +378,7 @@ exports.sentCoins = function sentCoins (session, tx, authority, toSend, fee,
|
|||
var newTx = _.clone(tx)
|
||||
newTx.txHash = txHash
|
||||
newTx.error = error
|
||||
insertOutgoing(client, session, newTx, toSend.satoshis, toSend.fiat,
|
||||
insertOutgoing(client, session, newTx, toSend.cryptoAtoms, toSend.fiat,
|
||||
'partial_send', authority, function (err) {
|
||||
done()
|
||||
if (err) logger.error(err)
|
||||
|
|
@ -447,8 +447,8 @@ exports.addInitialIncoming = function addInitialIncoming (session, tx, cb) {
|
|||
async.series([
|
||||
async.apply(silentQuery, client, 'BEGIN', null),
|
||||
async.apply(addPendingTx, client, session, true, tx.currencyCode,
|
||||
tx.cryptoCode, tx.toAddress, tx.satoshis),
|
||||
async.apply(insertIncoming, client, session, tx, tx.satoshis, tx.fiat,
|
||||
tx.cryptoCode, tx.toAddress, tx.cryptoAtoms),
|
||||
async.apply(insertIncoming, client, session, tx, tx.cryptoAtoms, tx.fiat,
|
||||
'initial_request', 'pending')
|
||||
], function (err) {
|
||||
if (err) {
|
||||
|
|
@ -500,6 +500,24 @@ exports.addIncomingPhone = function addIncomingPhone (session, tx, cb) {
|
|||
})
|
||||
}
|
||||
|
||||
function normalizeTxs (txs) {
|
||||
return txs.map(function (tx) {
|
||||
tx.toAddress = tx.to_address
|
||||
tx.currencyCode = tx.currency_code
|
||||
tx.txHash = tx.tx_hash
|
||||
tx.cryptoCode = tx.crypto_code
|
||||
tx.cryptoAtoms = new BigNumber(tx.satoshis)
|
||||
|
||||
tx.to_address = undefined
|
||||
tx.currency_code = undefined
|
||||
tx.tx_hash = undefined
|
||||
tx.crypto_code = undefined
|
||||
tx.satoshis = undefined
|
||||
|
||||
return tx
|
||||
})
|
||||
}
|
||||
|
||||
exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout, cb) {
|
||||
var sql = 'SELECT * FROM transactions ' +
|
||||
'WHERE phone=$1 AND dispensed=$2 ' +
|
||||
|
|
@ -510,7 +528,7 @@ exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout, cb) {
|
|||
query(client, sql, [phone, false, dispenseTimeout], function (err, results) {
|
||||
done()
|
||||
if (err) return cb(err)
|
||||
cb(null, results.rows)
|
||||
cb(null, normalizeTxs(results.rows))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
@ -525,7 +543,7 @@ exports.fetchTx = function fetchTx (session, cb) {
|
|||
query(client, sql, [session.fingerprint, session.id], function (err, results) {
|
||||
done()
|
||||
if (err) return cb(err)
|
||||
cb(null, results.rows)
|
||||
cb(null, normalizeTxs(results.rows))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
@ -645,7 +663,7 @@ exports.fetchOpenTxs = function fetchOpenTxs (statuses, age, cb) {
|
|||
query(client, sql, [true, age, statuses], function (err, results) {
|
||||
done()
|
||||
if (err) return cb(err)
|
||||
cb(null, results)
|
||||
cb(null, normalizeTxs(results))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue