diff --git a/src/types/election/approval.ts b/src/types/election/approval.ts index 4b52d94..524b1a2 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'); @@ -37,8 +38,10 @@ export class ApprovalElection extends UnpublishedElection { description, choices.map((choice, index) => ({ title: typeof choice.title === 'string' ? { default: choice.title } : choice.title, - value: index, - })) + value: choice.value ?? index, + meta: choice.meta, + })), + meta ); } diff --git a/src/types/election/budget.ts b/src/types/election/budget.ts index 91cb6d9..5cebe31 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'); @@ -65,8 +67,10 @@ export class BudgetElection extends UnpublishedElection { description, choices.map((choice, index) => ({ title: typeof choice.title === 'string' ? { default: choice.title } : choice.title, - value: index, - })) + value: choice.value ?? index, + meta: choice.meta, + })), + meta ); } diff --git a/src/types/election/multichoice.ts b/src/types/election/multichoice.ts index fa2de90..7128adb 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'); @@ -46,8 +47,10 @@ export class MultiChoiceElection extends UnpublishedElection { description, choices.map((choice, index) => ({ title: typeof choice.title === 'string' ? { default: choice.title } : choice.title, - value: index, - })) + value: choice.value ?? index, + meta: choice.meta, + })), + meta ); } diff --git a/src/types/election/quadratic.ts b/src/types/election/quadratic.ts index d9a27ea..747cf9b 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'); @@ -70,8 +72,10 @@ export class QuadraticElection extends UnpublishedElection { description, choices.map((choice, index) => ({ title: typeof choice.title === 'string' ? { default: choice.title } : choice.title, - value: index, - })) + value: choice.value ?? index, + meta: choice.meta, + })), + meta ); } diff --git a/src/types/election/unpublished.ts b/src/types/election/unpublished.ts index fb87bee..c041870 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,19 +43,16 @@ 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({ title: typeof title === 'string' ? { default: title } : title, description: typeof description === 'string' ? { default: description } : description, - choices: choices.map((choice) => { + choices: choices.map((choice, index) => { return { title: typeof choice.title === 'string' ? { default: choice.title } : choice.title, - value: choice.value, + value: choice.value ?? index, ...(choice.meta && { meta: choice.meta }), } as IChoice; }), 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;