From bcd16f0f3e45548e80437f7c8598c14c7cb0dd57 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 17 Mar 2015 10:59:00 -0300 Subject: [PATCH] add pagination to tx history --- lib/server.js | 13 +++++++-- test/integration/server.js | 57 +++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/lib/server.js b/lib/server.js index 056c74a..56e42da 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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); }); diff --git a/test/integration/server.js b/test/integration/server.js index d223b41..f53299c 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -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(); + }); + }); + }); }); });