diff --git a/server/components/api/voteIntervals/voteIntervalsDAL.js b/server/components/api/voteIntervals/voteIntervalsDAL.js index 02a9fc01..86a1db92 100644 --- a/server/components/api/voteIntervals/voteIntervalsDAL.js +++ b/server/components/api/voteIntervals/voteIntervalsDAL.js @@ -16,9 +16,9 @@ voteIntervalsDAL.findAllWithoutSnapshot = async function(height) { where: { hasSnapshot: false, beginHeight: { - [Op.lte]: height, - }, - }, + [Op.lte]: height + } + } }); }; @@ -26,8 +26,8 @@ voteIntervalsDAL.findByIntervalAndPhase = async function(interval, phase) { return this.findOne({ where: { interval, - phase, - }, + phase + } }); }; @@ -36,14 +36,14 @@ voteIntervalsDAL.findCurrent = async function(currentBlock) { where: { [Op.and]: { beginHeight: { - [Op.lte]: currentBlock, + [Op.lte]: currentBlock }, endHeight: { - [Op.gt]: currentBlock, - }, - }, + [Op.gt]: currentBlock + } + } }, - order: [['beginHeight', 'ASC']], + order: [['beginHeight', 'ASC']] }); }; @@ -51,10 +51,10 @@ voteIntervalsDAL.findPrev = async function(currentBlock) { return this.findOne({ where: { endHeight: { - [Op.lte]: currentBlock, - }, + [Op.lte]: currentBlock + } }, - order: [['endHeight', 'DESC']], + order: [['endHeight', 'DESC']] }); }; @@ -62,10 +62,10 @@ voteIntervalsDAL.findNext = async function(currentBlock) { return this.findOne({ where: { beginHeight: { - [Op.gt]: currentBlock, - }, + [Op.gt]: currentBlock + } }, - order: [['beginHeight', 'ASC']], + order: [['beginHeight', 'ASC']] }); }; @@ -74,26 +74,12 @@ voteIntervalsDAL.findNext = async function(currentBlock) { * * @param {number} currentBlock */ -voteIntervalsDAL.findAllRecent = async function(currentBlock, limit = 5) { - const [prevs, current, next] = await Promise.all([ - this.findAll({ - where: { - endHeight: { - [Op.lte]: currentBlock, - }, - }, - order: [['endHeight', 'DESC']], - limit, - }), - this.findCurrent(currentBlock), - this.findNext(currentBlock), - ]); - - const intervals = []; - next && intervals.push(next); - current && intervals.push(current); - intervals.push.apply(intervals, prevs); - return intervals; +voteIntervalsDAL.findAllRecent = async function({ limit, offset = 0 }) { + return await this.findAll({ + limit, + offset, + order: [['beginHeight', 'ASC']] + }); }; /** diff --git a/server/components/api/votes/votesBLL.js b/server/components/api/votes/votesBLL.js index af7983c7..7d37b256 100644 --- a/server/components/api/votes/votesBLL.js +++ b/server/components/api/votes/votesBLL.js @@ -15,7 +15,11 @@ const AFTER_TALLY_BLOCKS = configAfterTallyBlocks module.exports = { findIntervalAndTally: async function({ interval, phase } = {}) { const currentBlock = await blocksBLL.getCurrentBlockNumber(); - const currentInterval = await getCurrentInterval({interval, phase, currentBlock}); + const currentInterval = await getCurrentInterval({ + interval, + phase, + currentBlock + }); if (!currentInterval) { return null; @@ -53,7 +57,11 @@ module.exports = { sorted } = {}) { const currentBlock = await blocksBLL.getCurrentBlockNumber(); - const currentInterval = await getCurrentInterval({interval, phase, currentBlock}); + const currentInterval = await getCurrentInterval({ + interval, + phase, + currentBlock + }); if (!currentInterval) { return null; } @@ -87,7 +95,11 @@ module.exports = { pageSize = 10 } = {}) { const currentBlock = await blocksBLL.getCurrentBlockNumber(); - const currentInterval = await getCurrentInterval({interval, phase, currentBlock}); + const currentInterval = await getCurrentInterval({ + interval, + phase, + currentBlock + }); if (!currentInterval) { return null; } @@ -109,13 +121,18 @@ module.exports = { ) ]).then(votesDAL.getItemsAndCountResult); }, - findRecentIntervals: async function() { - const currentBlock = await blocksBLL.getCurrentBlockNumber(); - return voteIntervalsDAL.findAllRecent(currentBlock); + findRecentIntervals: async function({ page, pageSize } = {}) { + return voteIntervalsDAL.findAllRecent( + createQueryObject({ page, pageSize }) + ); }, findContestantWinners: async function({ interval } = {}) { const currentBlock = await blocksBLL.getCurrentBlockNumber(); - const currentInterval = await getCurrentInterval({interval, phase: 'Contestant', currentBlock}); + const currentInterval = await getCurrentInterval({ + interval, + phase: 'Contestant', + currentBlock + }); if (!currentInterval) { return null; } @@ -134,7 +151,7 @@ module.exports = { * 4. previous interval if next does not exist * 4. next block */ -async function getCurrentInterval({interval, phase, currentBlock} = {}) { +async function getCurrentInterval({ interval, phase, currentBlock } = {}) { if (interval && phase) { return voteIntervalsDAL.findByIntervalAndPhase(interval, phase); } diff --git a/server/components/api/votes/votesController.js b/server/components/api/votes/votesController.js index aea9097c..2939ef54 100644 --- a/server/components/api/votes/votesController.js +++ b/server/components/api/votes/votesController.js @@ -62,7 +62,8 @@ module.exports = { .json(jsonResponse.create(httpStatus.OK, result || [])); }, recentIntervals: async function(req, res) { - const result = await votesBLL.findRecentIntervals(); + const { page, pageSize } = req.query; + const result = await votesBLL.findRecentIntervals({ page, pageSize }); res.status(httpStatus.OK).json(jsonResponse.create(httpStatus.OK, result)); }, getCandidates: async function(req, res) {