diff --git a/src/types/election/approval.ts b/src/types/election/approval.ts index 4b52d94..3d121c1 100644 --- a/src/types/election/approval.ts +++ b/src/types/election/approval.ts @@ -1,7 +1,7 @@ import { MultiLanguage } from '../../util/lang'; -import { IElectionParameters, IVoteType } from './election'; +import { CustomMeta, IElectionParameters, IVoteType } from './election'; import { UnpublishedElection } from './unpublished'; -import { ElectionMetadata, ElectionResultsTypeNames, getElectionMetadataTemplate } from '../metadata'; +import { Choice, ElectionMetadata, ElectionResultsTypeNames, getElectionMetadataTemplate } from '../metadata'; import { Vote } from '../vote'; export interface IApprovalElectionParameters extends IElectionParameters {} @@ -26,7 +26,8 @@ export class ApprovalElection extends UnpublishedElection { public addQuestion( title: string | MultiLanguage, description: string | MultiLanguage, - choices: Array<{ title: string } | { title: MultiLanguage }> + choices: Array<{ title: string; value: number; meta?: CustomMeta } | Choice>, + meta?: CustomMeta ) { if (this.questions.length > 0) { throw new Error('This type of election can only have one question'); @@ -35,10 +36,12 @@ export class ApprovalElection extends UnpublishedElection { return super.addQuestion( title, description, - choices.map((choice, index) => ({ + choices.map((choice) => ({ title: typeof choice.title === 'string' ? { default: choice.title } : choice.title, - value: index, - })) + value: choice.value, + meta: choice.meta, + })), + meta ); } diff --git a/src/types/election/budget.ts b/src/types/election/budget.ts index 91cb6d9..799ad46 100644 --- a/src/types/election/budget.ts +++ b/src/types/election/budget.ts @@ -1,7 +1,8 @@ import { MultiLanguage } from '../../util/lang'; -import { IElectionParameters, IVoteType } from './election'; +import { CustomMeta, IElectionParameters, IVoteType } from './election'; import { UnpublishedElection } from './unpublished'; import { + Choice, ElectionMetadata, ElectionResultsType, ElectionResultsTypeNames, @@ -54,7 +55,8 @@ export class BudgetElection extends UnpublishedElection { public addQuestion( title: string | MultiLanguage, description: string | MultiLanguage, - choices: Array<{ title: string } | { title: MultiLanguage }> + choices: Array<{ title: string; value: number; meta?: CustomMeta } | Choice>, + meta?: CustomMeta ) { if (this.questions.length > 0) { throw new Error('This type of election can only have one question'); @@ -63,10 +65,12 @@ export class BudgetElection extends UnpublishedElection { return super.addQuestion( title, description, - choices.map((choice, index) => ({ + choices.map((choice) => ({ title: typeof choice.title === 'string' ? { default: choice.title } : choice.title, - value: index, - })) + value: choice.value, + meta: choice.meta, + })), + meta ); } diff --git a/src/types/election/multichoice.ts b/src/types/election/multichoice.ts index fa2de90..a5bbe9d 100644 --- a/src/types/election/multichoice.ts +++ b/src/types/election/multichoice.ts @@ -1,7 +1,7 @@ import { MultiLanguage } from '../../util/lang'; -import { IElectionParameters, IVoteType } from './election'; +import { CustomMeta, IElectionParameters, IVoteType } from './election'; import { UnpublishedElection } from './unpublished'; -import { ElectionMetadata, ElectionResultsTypeNames, getElectionMetadataTemplate } from '../metadata'; +import { Choice, ElectionMetadata, ElectionResultsTypeNames, getElectionMetadataTemplate } from '../metadata'; import { Vote } from '../vote'; export interface IMultiChoiceElectionParameters extends IElectionParameters { @@ -35,7 +35,8 @@ export class MultiChoiceElection extends UnpublishedElection { public addQuestion( title: string | MultiLanguage, description: string | MultiLanguage, - choices: Array<{ title: string } | { title: MultiLanguage }> + choices: Array<{ title: string; value: number; meta?: CustomMeta } | Choice>, + meta?: CustomMeta ) { if (this.questions.length > 0) { throw new Error('This type of election can only have one question'); @@ -44,10 +45,12 @@ export class MultiChoiceElection extends UnpublishedElection { return super.addQuestion( title, description, - choices.map((choice, index) => ({ + choices.map((choice) => ({ title: typeof choice.title === 'string' ? { default: choice.title } : choice.title, - value: index, - })) + value: choice.value, + meta: choice.meta, + })), + meta ); } diff --git a/src/types/election/quadratic.ts b/src/types/election/quadratic.ts index d9a27ea..29fec34 100644 --- a/src/types/election/quadratic.ts +++ b/src/types/election/quadratic.ts @@ -1,7 +1,8 @@ import { MultiLanguage } from '../../util/lang'; -import { IElectionParameters, IVoteType } from './election'; +import { CustomMeta, IElectionParameters, IVoteType } from './election'; import { UnpublishedElection } from './unpublished'; import { + Choice, ElectionMetadata, ElectionResultsType, ElectionResultsTypeNames, @@ -59,7 +60,8 @@ export class QuadraticElection extends UnpublishedElection { public addQuestion( title: string | MultiLanguage, description: string | MultiLanguage, - choices: Array<{ title: string } | { title: MultiLanguage }> + choices: Array<{ title: string; value: number; meta?: CustomMeta } | Choice>, + meta?: CustomMeta ) { if (this.questions.length > 0) { throw new Error('This type of election can only have one question'); @@ -68,10 +70,12 @@ export class QuadraticElection extends UnpublishedElection { return super.addQuestion( title, description, - choices.map((choice, index) => ({ + choices.map((choice) => ({ title: typeof choice.title === 'string' ? { default: choice.title } : choice.title, - value: index, - })) + value: choice.value, + meta: choice.meta, + })), + meta ); } diff --git a/src/types/election/unpublished.ts b/src/types/election/unpublished.ts index fb87bee..2bc1a73 100644 --- a/src/types/election/unpublished.ts +++ b/src/types/election/unpublished.ts @@ -1,6 +1,7 @@ import { MultiLanguage } from '../../util/lang'; import { checkValidElectionMetadata, + Choice, ElectionMetadata, getElectionMetadataTemplate, IChoice, @@ -42,10 +43,7 @@ export class UnpublishedElection extends Election { public addQuestion( title: string | MultiLanguage, description: string | MultiLanguage, - choices: Array< - | { title: string; value: number; meta?: CustomMeta } - | { title: MultiLanguage; value: number; meta?: CustomMeta } - >, + choices: Array<{ title: string; value: number; meta?: CustomMeta } | Choice>, meta?: CustomMeta ): UnpublishedElection { this._questions.push({ diff --git a/src/types/metadata/election.ts b/src/types/metadata/election.ts index c86658e..4b983e2 100644 --- a/src/types/metadata/election.ts +++ b/src/types/metadata/election.ts @@ -20,6 +20,8 @@ export function checkValidElectionMetadata(electionMetadata: ElectionMetadata): throw err; } } +export type Choice = Pick; +export type Question = Pick; export interface IChoice { title: MultiLanguage;