Skip to content

Commit

Permalink
Added new metadata fields for all type of elections
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Oct 30, 2024
1 parent 8da4777 commit 79898f3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
15 changes: 9 additions & 6 deletions src/types/election/approval.ts
Original file line number Diff line number Diff line change
@@ -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 {}
Expand All @@ -26,7 +26,8 @@ export class ApprovalElection extends UnpublishedElection {
public addQuestion(
title: string | MultiLanguage<string>,
description: string | MultiLanguage<string>,
choices: Array<{ title: string } | { title: MultiLanguage<string> }>
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');
Expand All @@ -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
);
}

Expand Down
14 changes: 9 additions & 5 deletions src/types/election/budget.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -54,7 +55,8 @@ export class BudgetElection extends UnpublishedElection {
public addQuestion(
title: string | MultiLanguage<string>,
description: string | MultiLanguage<string>,
choices: Array<{ title: string } | { title: MultiLanguage<string> }>
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');
Expand All @@ -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
);
}

Expand Down
15 changes: 9 additions & 6 deletions src/types/election/multichoice.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -35,7 +35,8 @@ export class MultiChoiceElection extends UnpublishedElection {
public addQuestion(
title: string | MultiLanguage<string>,
description: string | MultiLanguage<string>,
choices: Array<{ title: string } | { title: MultiLanguage<string> }>
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');
Expand All @@ -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
);
}

Expand Down
14 changes: 9 additions & 5 deletions src/types/election/quadratic.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -59,7 +60,8 @@ export class QuadraticElection extends UnpublishedElection {
public addQuestion(
title: string | MultiLanguage<string>,
description: string | MultiLanguage<string>,
choices: Array<{ title: string } | { title: MultiLanguage<string> }>
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');
Expand All @@ -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
);
}

Expand Down
6 changes: 2 additions & 4 deletions src/types/election/unpublished.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MultiLanguage } from '../../util/lang';
import {
checkValidElectionMetadata,
Choice,
ElectionMetadata,
getElectionMetadataTemplate,
IChoice,
Expand Down Expand Up @@ -42,10 +43,7 @@ export class UnpublishedElection extends Election {
public addQuestion(
title: string | MultiLanguage<string>,
description: string | MultiLanguage<string>,
choices: Array<
| { title: string; value: number; meta?: CustomMeta }
| { title: MultiLanguage<string>; value: number; meta?: CustomMeta }
>,
choices: Array<{ title: string; value: number; meta?: CustomMeta } | Choice>,
meta?: CustomMeta
): UnpublishedElection {
this._questions.push({
Expand Down
2 changes: 2 additions & 0 deletions src/types/metadata/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function checkValidElectionMetadata(electionMetadata: ElectionMetadata):
throw err;
}
}
export type Choice = Pick<IChoice, 'title' | 'value' | 'meta'>;
export type Question = Pick<IQuestion, 'title' | 'description' | 'choices' | 'meta'>;

export interface IChoice {
title: MultiLanguage<string>;
Expand Down

0 comments on commit 79898f3

Please sign in to comment.