add pagination to tx history

This commit is contained in:
Ivan Socolsky 2015-03-17 10:59:00 -03:00
parent 9344b591e7
commit bcd16f0f3e
2 changed files with 64 additions and 6 deletions

View File

@ -981,7 +981,16 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
};
function paginate(txs) {
// TODO
var limited = opts.limit && opts.limit != -1;
if (!opts.minTs && !opts.maxTs && !limited) return;
var minTs = opts.minTs || 0;
var maxTs = opts.maxTs || Math.ceil(Date.now() / 1000);
var filtered = _.sortBy(_.filter(txs, function(tx) {
return tx.time >= minTs && tx.time <= maxTs;
}), 'time');
return limited ? _.take(filtered, opts.limit) : filtered;
};
// Get addresses for this wallet
@ -1015,7 +1024,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
var txs = res[1];
decorate(txs, addresses, proposals);
paginate(txs);
txs = paginate(txs);
return cb(null, txs);
});

View File

@ -2206,7 +2206,7 @@ describe('Copay server', function() {
txid: '1',
confirmations: 1,
fees: 100,
minedTs: 1,
time: 1,
inputs: [{
address: 'external',
amount: 500,
@ -2234,7 +2234,7 @@ describe('Copay server', function() {
txid: '1',
confirmations: 1,
fees: 100,
minedTs: 1,
time: 1,
inputs: [{
address: mainAddresses[0].address,
amount: 500,
@ -2262,7 +2262,7 @@ describe('Copay server', function() {
txid: '1',
confirmations: 1,
fees: 100,
minedTs: 1,
time: 1,
inputs: [{
address: mainAddresses[0].address,
amount: 500,
@ -2312,7 +2312,7 @@ describe('Copay server', function() {
txid: '1122334455',
confirmations: 1,
fees: 5460,
minedTs: 1,
time: 1,
inputs: [{
address: tx.inputs[0].address,
amount: utxos[0].satoshis,
@ -2345,5 +2345,54 @@ describe('Copay server', function() {
});
});
});
describe('Pagination', function() {
beforeEach(function() {
server._normalizeTxHistory = sinon.stub().returnsArg(0);
var timestamps = [10, 50, 30, 40, 20];
var txs = _.map(timestamps, function(ts, idx) {
return {
txid: (idx + 1).toString(),
confirmations: ts / 10,
fees: 100,
time: ts,
inputs: [{
address: 'external',
amount: 500,
}],
outputs: [{
address: mainAddresses[0].address,
amount: 200,
}],
};
});
helpers.stubHistory(txs);
});
it('should get paginated tx history', function(done) {
server.getTxHistory({
minTs: 15,
maxTs: 45,
}, function(err, txs) {
should.not.exist(err);
should.exist(txs);
txs.length.should.equal(3);
_.pluck(txs, 'time').should.deep.equal([20, 30, 40]);
done();
});
});
it('should get paginated tx history with limit', function(done) {
server.getTxHistory({
minTs: 15,
maxTs: 45,
limit: 2,
}, function(err, txs) {
should.not.exist(err);
should.exist(txs);
txs.length.should.equal(2);
_.pluck(txs, 'time').should.deep.equal([20, 30]);
done();
});
});
});
});
});