Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new metadata fields for all type of elections #420

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 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 @@ -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
);
}

Expand Down
12 changes: 8 additions & 4 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 @@ -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
);
}

Expand Down
13 changes: 8 additions & 5 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 @@ -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
);
}

Expand Down
12 changes: 8 additions & 4 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 @@ -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
);
}

Expand Down
10 changes: 4 additions & 6 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,19 +43,16 @@ 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({
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;
}),
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
Loading