Skip to content

Commit

Permalink
Infoboard vote improvements
Browse files Browse the repository at this point in the history
Correctly show results for tied votes.

Show infoboard entry about a vote receiving no votes for just 10
minutes.
  • Loading branch information
nicou committed Jun 17, 2024
1 parent 78fb0dc commit bc9ee30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/routes/vote.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async function createVoteCreatedInfoboardEntry(vote) {

const title = `Vote: ${vote.get('title')}`;
const votingAllowedFor = voteFilterToTextMap[vote.get('allowed_voters')] || vote.get('allowed_voters');
const body = `${votingAllowedFor} can now cast their vote in EOC Datahub.<br><br>Voting ends at ${voteActiveUntilFormatted}.`;
const body = `${votingAllowedFor} can now cast their vote in EOC Datahub.<br><br>Voting ends at ${voteActiveUntilFormatted}`;
await InfoEntry.forge().save({
priority: 1,
enabled: true,
Expand Down
20 changes: 16 additions & 4 deletions src/rules/social/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ async function createVoteResultsInfoEntry(vote) {
const voteOptions = await new VoteOption().where('vote_id', vote.get('id')).fetchAll();

const infoEntry = new InfoEntry();
const activeMinutes = 60;
const activeUntil = moment().add(activeMinutes, 'minutes').toDate();
const activeUntil = moment().add(60, 'minutes').toDate();
const postData = {
priority: 1,
enabled: true,
Expand All @@ -33,7 +32,11 @@ async function createVoteResultsInfoEntry(vote) {
if (totalVotes === 0) {
const body = 'No votes were cast.';
const metadata = { vote_results: [] };
await infoEntry.save({ ...postData, body, metadata }, { method: 'insert' });

// No one cared about the vote so no one will care about the results, only show for 10min
const shortActiveTime = moment().add(10, 'minutes').toDate();

await infoEntry.save({ ...postData, body, metadata, active_until: shortActiveTime }, { method: 'insert' });
return;
}

Expand All @@ -44,8 +47,17 @@ async function createVoteResultsInfoEntry(vote) {
}
results.sort((a, b) => b.votes - a.votes);
results.forEach(result => result.votesPercentage = Math.round((result.votes / results[0].votes) * 100));

const tiedResults = results.filter(result => result.votes === results[0].votes);
const votesWord = voteEntries.length === 1 ? 'vote' : 'votes';
const body = `Vote results are in! ${totalVotes} ${votesWord} were cast. The winning option is <strong>${results[0].option}</strong> with ${results[0].votes} ${votesWord}.`;
let body = "";
if (tiedResults.length === 2) {
body = `Vote results are in! ${totalVotes} ${votesWord} were cast. There was a tie between <strong>${tiedResults[0].option}</strong> and <strong>${tiedResults[1].option}</strong> with ${tiedResults[0].votes} ${tiedResults[0].votes === 1 ? "vote" : "votes"} each.`;
} else if (tiedResults.length > 2) {
body = `Vote results are in! ${totalVotes} ${votesWord} were cast. There was a tie between multiple options with ${tiedResults[0].votes} ${tiedResults[0].votes === 1 ? "vote" : "votes"} each.`;
} else {
body = `Vote results are in! ${totalVotes} ${votesWord} ${totalVotes === 1 ? "was" : "were"} cast. The winning option is <strong>${results[0].option}</strong> with ${results[0].votes} ${votesWord}.`;
}

await infoEntry.save({
...postData,
Expand Down

0 comments on commit bc9ee30

Please sign in to comment.