feat(resend): Re-send process improvements

This commit is contained in:
Damian Mee 2014-10-01 01:54:11 +02:00
parent f7f98d986e
commit 9ea8af28f8
3 changed files with 79 additions and 65 deletions

View file

@ -77,9 +77,9 @@ exports.recordBill = function recordBill(deviceFingerprint, rec, cb) {
client.query(getInsertQuery('bills', fields), values, function(err, billInfo) {
if (err && PG_ERRORS[err.code] === 'uniqueViolation')
return cb(null, {code: 304}); // 304 => Not Modified (vel. already noted)
return cb(null, {code: 204});
cb(); // 201 => Accepted (vel. saved)
cb(); // 201 => Accepted / saved
});
};
@ -90,30 +90,6 @@ exports.recordDeviceEvent = function recordDeviceEvent(deviceFingerprint, event,
cb);
};
function _getTxs(txId, onlyPending, cb) {
var query = 'SELECT * FROM transactions WHERE id=$1';
var values = [txId];
if (onlyPending) {
query += ' AND status=$2 AND tx_hash IS NULL';
values.push('pending');
}
client.query(query, values, function(err, results) {
if (err) return cb(err);
if (results.rows.length === 0)
return cb(new Error('Couldn\'t find transaction'));
cb(null, results.rows);
});
}
// returns complete [txs]
exports.getTxs = function getTxs(txId, cb) {
_getTxs(txId, false, cb);
};
exports.getPendingAmount = function getPendingAmount(txId, cb) {
async.parallel({
// NOTE: `async.apply()` would strip context here
@ -126,7 +102,7 @@ exports.getPendingAmount = function getPendingAmount(txId, cb) {
},
bills: function(_cb) {
client.query(
'SELECT * FROM bills WHERE transaction_id=$1 ORDER BY created DESC',
'SELECT * FROM bills WHERE transaction_id=$1 ORDER BY device_time DESC',
[txId],
_cb
);
@ -156,14 +132,19 @@ exports.getPendingAmount = function getPendingAmount(txId, cb) {
newTx.fiat = lastBill.total_fiat;
results.txs.rows.forEach(function(tx) {
newTx.satoshis -= tx.satoshis;
newTx.fiat -= tx.fiat;
// try sending again only in case of a fail due to insufficientFunds
if (tx.status !== 'insufficientFunds') {
newTx.satoshis -= tx.satoshis;
newTx.fiat -= tx.fiat;
}
});
}
// Nothing to send == nothing to do
if (newTx.satoshis <= 0) {
logger.error('Negative tx amount (%d) for txId: %s', newTx.satoshis, txId);
if (newTx.satoshis < 0)
logger.error('Negative tx amount (%d) for txId: %s', newTx.satoshis, txId);
return cb();
}
@ -171,7 +152,7 @@ exports.getPendingAmount = function getPendingAmount(txId, cb) {
});
};
exports.summonTx = function summonTx(deviceFingerprint, tx, cb) {
exports.insertTx = function insertTx(deviceFingerprint, tx, cb) {
var fields = [
'id',
'status',
@ -197,14 +178,22 @@ exports.summonTx = function summonTx(deviceFingerprint, tx, cb) {
values.push(tx.partial_id);
}
if (typeof tx.is_completed !== 'undefined') {
fields.push('is_completed');
values.push(tx.is_completed);
}
// First attampt an INSERT
// If it worked, go ahead with transaction
client.query(getInsertQuery('transactions', fields),
values,
function(err) {
if (err) {
if (PG_ERRORS[err.code] === 'uniqueViolation')
return _getTxs(tx.txId, false, cb);
if (PG_ERRORS[err.code] === 'uniqueViolation') {
var _err = new Error(err);
_err.name = 'UniqueViolation';
return cb(_err);
}
return cb(err);
}
@ -213,7 +202,7 @@ exports.summonTx = function summonTx(deviceFingerprint, tx, cb) {
});
};
// `@data` can contain `partial_id`, `hash`, or `error`
// `@data` can contain `partial_id`, `is_completed`, `hash`, or `error`
exports.changeTxStatus = function changeTxStatus(txId, newStatus, data, cb) {
data = data || {};
cb = typeof cb === 'function' ? cb : function() {};
@ -231,8 +220,17 @@ exports.changeTxStatus = function changeTxStatus(txId, newStatus, data, cb) {
}
if (newStatus === 'completed') {
query += ', tx_hash=$' + n++;
values.push(data.hash);
// set tx_hash (if available)
if (typeof data.hash !== 'undefined') {
query += ', tx_hash=$' + n++;
values.push(data.hash);
}
// indicates if tx was finished by a `/send` call (and not timeout)
if (typeof data.is_completed !== 'undefined') {
query += ', is_completed=$' + n++;
values.push(data.is_completed);
}
}
query += ' WHERE id=$' + n++;
@ -243,6 +241,5 @@ exports.changeTxStatus = function changeTxStatus(txId, newStatus, data, cb) {
query += ' AND partial_id=$' + n++;
values.push(partial_id);
}
client.query(query, values, cb);
};