Skip to content

Commit

Permalink
Refactored results in questions and added new data for multi choice e…
Browse files Browse the repository at this point in the history
…lections
  • Loading branch information
marcvelmer committed Jan 17, 2024
1 parent 138f5cd commit 832c71e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
48 changes: 43 additions & 5 deletions src/services/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ArchivedElection,
Census,
CspCensus,
ElectionResultsTypeNames,
InvalidElection,
PublishedCensus,
PublishedElection,
Expand Down Expand Up @@ -162,14 +163,11 @@ export class ElectionService extends Service implements ElectionServicePropertie
questions: electionInfo.metadata?.questions.map((question, qIndex) => ({
title: question.title,
description: question.description,
numAbstains: this.calculateMultichoiceAbstains(electionInfo.metadata.type, electionInfo.result),
choices: question.choices.map((choice, cIndex) => ({
title: choice.title,
value: choice.value,
results: electionInfo.result
? electionInfo.tallyMode.maxTotalCost > 0
? electionInfo.result[cIndex][1]
: electionInfo.result[qIndex][cIndex]
: null,
results: this.calculateChoiceResults(electionInfo.metadata.type.name, electionInfo.result, qIndex, cIndex),
})),
})),
resultsType: electionInfo.metadata.type,
Expand All @@ -181,6 +179,46 @@ export class ElectionService extends Service implements ElectionServicePropertie
: new PublishedElection(electionParameters);
}

private calculateChoiceResults(electionType, result, qIndex, cIndex) {
try {
switch (electionType) {
case ElectionResultsTypeNames.SINGLE_CHOICE_MULTIQUESTION:
return result ? result[qIndex][cIndex] : null;
case ElectionResultsTypeNames.MULTIPLE_CHOICE:
return result
.reduce((prev, cur) => {
return prev + +cur[cIndex];
}, 0)
.toString();
case ElectionResultsTypeNames.BUDGET:
return result[cIndex][0];
default:
return null;
}
} catch (e) {
return null;
}
}

private calculateMultichoiceAbstains(electionType, result) {
try {
switch (electionType.name) {
case ElectionResultsTypeNames.MULTIPLE_CHOICE:
let abstains = 0;
for (const pos of electionType.properties.abstainValues) {
abstains += result.reduce((prev, cur) => {
return prev + +cur[+pos];
}, 0);
}
return abstains.toString();
default:
return null;
}
} catch (e) {
return null;
}
}

async fetchElections(
params: Partial<FetchElectionsParameters>
): Promise<Array<PublishedElection | ArchivedElection | InvalidElection>> {
Expand Down
1 change: 1 addition & 0 deletions src/types/metadata/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface IChoice {
export interface IQuestion {
title: MultiLanguage<string>;
description?: MultiLanguage<string>;
numAbstains?: string;
choices: Array<IChoice>;
}

Expand Down
7 changes: 4 additions & 3 deletions test/integration/election.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ describe('Election integration tests', () => {
participants.map(async (participant) => {
const pClient = new VocdoniSDKClient(clientParams(participant));
pClient.setElectionId(electionId);
const vote = new Vote([0, 1, 2]);
const vote = new Vote([0, 5, 7]);
return pClient.submitVote(vote);
})
)
Expand All @@ -646,9 +646,10 @@ describe('Election integration tests', () => {
});
expect(election.results).toStrictEqual([
['5', '0', '0', '0', '0', '0', '0', '0'],
['0', '5', '0', '0', '0', '0', '0', '0'],
['0', '0', '5', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '5', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '5'],
]);
expect(election.questions[0].numAbstains).toEqual('10');
});
}, 850000);
it('should create a budget election without weights and have the correct values set', async () => {
Expand Down

0 comments on commit 832c71e

Please sign in to comment.