implement pagination using skip & limit
This commit is contained in:
parent
f13fc5660c
commit
330c399c07
|
@ -913,12 +913,11 @@ WalletService.prototype._normalizeTxHistory = function(txs) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Retrieves all transactions (incoming & outgoing) in the range (maxTs-minTs)
|
||||
* Retrieves all transactions (incoming & outgoing)
|
||||
* Times are in UNIX EPOCH
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {Number} opts.minTs (defaults to 0)
|
||||
* @param {Number} opts.maxTs (defaults to now)
|
||||
* @param {Number} opts.skip (defaults to 0)
|
||||
* @param {Number} opts.limit
|
||||
* @returns {TxProposal[]} Transaction proposals, first newer
|
||||
*/
|
||||
|
@ -1003,17 +1002,14 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
|
|||
};
|
||||
|
||||
function paginate(txs) {
|
||||
var limited = opts.limit && opts.limit != -1;
|
||||
var minTs = opts.minTs || 0;
|
||||
var maxTs = opts.maxTs || Math.ceil(Date.now() / 1000);
|
||||
var skip = opts.skip || 0;
|
||||
var limited = _.isNumber(opts.limit) && opts.limit != -1;
|
||||
|
||||
var filtered = _.sortBy(_.filter(txs, function(tx) {
|
||||
return tx.time >= minTs && tx.time <= maxTs;
|
||||
}), function(tx) {
|
||||
var sliced = _.slice(_.sortBy(txs, function(tx) {
|
||||
return -tx.time;
|
||||
});
|
||||
}), skip);
|
||||
|
||||
return limited ? _.take(filtered, opts.limit) : filtered;
|
||||
return limited ? _.take(sliced, opts.limit) : sliced;
|
||||
};
|
||||
|
||||
// Get addresses for this wallet
|
||||
|
|
|
@ -2383,6 +2383,18 @@ describe('Copay server', function() {
|
|||
limit: 0,
|
||||
},
|
||||
expected: [],
|
||||
}, {
|
||||
opts: {
|
||||
skip: 4,
|
||||
limit: 20,
|
||||
},
|
||||
expected: [10],
|
||||
}, {
|
||||
opts: {
|
||||
skip: 20,
|
||||
limit: 1,
|
||||
},
|
||||
expected: [],
|
||||
}];
|
||||
|
||||
server._normalizeTxHistory = sinon.stub().returnsArg(0);
|
||||
|
|
Loading…
Reference in New Issue