Skip to content

Commit

Permalink
fix(governance): return default commit in relevant
Browse files Browse the repository at this point in the history
  • Loading branch information
liorfrenkel committed Apr 16, 2020
1 parent 895a36e commit 19a501e
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions server/components/api/votes/votesBLL.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const configDefaultCommitId = config.get('governance:defaultCommitId');
const AFTER_TALLY_BLOCKS = configAfterTallyBlocks ? Number(configAfterTallyBlocks) : 1000;

module.exports = {
findIntervalAndTally: async function({ interval, phase } = {}) {
findIntervalAndTally: async function ({ interval, phase } = {}) {
const currentBlock = await blocksBLL.getCurrentBlockNumber();
const currentInterval = await getCurrentInterval({
interval,
Expand All @@ -35,9 +35,11 @@ module.exports = {
interval: currentInterval.interval,
});

const candidatesPromise = votesDAL.findContestantWinners({
interval: currentInterval.interval,
});
const candidatesPromise = votesDAL
.findContestantWinners({
interval: currentInterval.interval,
})
.then(addDefaultCommitId);

const [winner, candidates] = await Promise.all([winnerPromise, candidatesPromise]);
return {
Expand All @@ -50,15 +52,15 @@ module.exports = {
candidates,
};
},
findNextInterval: async function() {
findNextInterval: async function () {
const currentBlock = await blocksBLL.getCurrentBlockNumber();
return voteIntervalsDAL.findNext(currentBlock);
},
findCurrentOrNextInterval: async function() {
findCurrentOrNextInterval: async function () {
const currentBlock = await blocksBLL.getCurrentBlockNumber();
return voteIntervalsDAL.findCurrentOrNext(currentBlock);
},
findAllVotesByInterval: async function({
findAllVotesByInterval: async function ({
interval,
phase,
page = 0,
Expand Down Expand Up @@ -95,7 +97,7 @@ module.exports = {
votesDAL.findAllByInterval(query),
]).then(votesDAL.getItemsAndCountResult);
},
findAllVoteResults: async function({ interval, phase, page = 0, pageSize = 10 } = {}) {
findAllVoteResults: async function ({ interval, phase, page = 0, pageSize = 10 } = {}) {
const currentBlock = await blocksBLL.getCurrentBlockNumber();
const currentInterval = await getCurrentInterval({
interval,
Expand Down Expand Up @@ -123,11 +125,11 @@ module.exports = {
),
]).then(votesDAL.getItemsAndCountResult);
},
findRecentIntervals: async function() {
findRecentIntervals: async function () {
const currentBlock = await blocksBLL.getCurrentBlockNumber();
return voteIntervalsDAL.findAllRecent(currentBlock);
},
findContestantWinners: async function({ interval } = {}) {
findContestantWinners: async function ({ interval } = {}) {
const currentBlock = await blocksBLL.getCurrentBlockNumber();
const currentInterval = await getCurrentInterval({
interval,
Expand All @@ -142,12 +144,7 @@ module.exports = {
.findContestantWinners({
interval: currentInterval.interval,
})
// add the default commit id to the list if it does not exist
.then(results =>
results.some(candidate => candidate.commitId === configDefaultCommitId)
? results
: [{ commitId: configDefaultCommitId, zpAmount: '0' }, ...results]
);
.then(addDefaultCommitId);
},
};

Expand Down Expand Up @@ -178,3 +175,12 @@ async function getCurrentInterval({ interval, phase, currentBlock } = {}) {
? prev
: next;
}

/**
* Add the default commit id to the list if it does not exist
*/
function addDefaultCommitId(candidates = []) {
return candidates.some((candidate) => candidate.commitId === configDefaultCommitId)
? candidates
: [{ commitId: configDefaultCommitId, zpAmount: '0' }, ...candidates];
}

0 comments on commit 19a501e

Please sign in to comment.