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 meta fields for questions and choices #419

Merged
merged 3 commits into from
Oct 25, 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
2 changes: 2 additions & 0 deletions src/services/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ export class ElectionService extends Service implements ElectionServicePropertie
title: choice.title,
value: choice.value,
results: this.calculateChoiceResults(electionInfo.metadata.type.name, electionInfo.result, qIndex, cIndex),
...(choice.meta && { meta: choice.meta }),
})),
...(question.meta && { meta: question.meta }),
})),
resultsType: electionInfo.metadata?.type,
raw: electionInfo,
Expand Down
8 changes: 4 additions & 4 deletions src/types/election/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ interface JsonMap {
}
interface JsonArray extends Array<AnyJson> {}

export type ElectionMeta = AnyJson | JsonArray | JsonMap;
export type CustomMeta = AnyJson | JsonArray | JsonMap;

/**
* Define election parameters.
Expand All @@ -132,7 +132,7 @@ export interface IElectionParameters {
/**
* Metadata (anything added by the election creator)
*/
meta?: ElectionMeta;
meta?: CustomMeta;
startDate?: string | number | Date;
endDate: string | number | Date;
census: Census;
Expand Down Expand Up @@ -172,7 +172,7 @@ export abstract class Election {
protected _description: MultiLanguage<string>;
protected _header: string;
protected _streamUri: string;
protected _meta: ElectionMeta;
protected _meta: CustomMeta;
protected _startDate: Date;
protected _endDate: Date;
protected _electionType: IElectionType;
Expand Down Expand Up @@ -232,7 +232,7 @@ export abstract class Election {
return this._streamUri;
}

get meta(): ElectionMeta {
get meta(): CustomMeta {
return this._meta;
}

Expand Down
16 changes: 12 additions & 4 deletions src/types/election/unpublished.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../metadata';
import invariant from 'tiny-invariant';
import { Census } from '../census';
import { Election, ElectionMeta, IElectionParameters, IElectionType, IVoteType } from './election';
import { Election, CustomMeta, IElectionParameters, IElectionType, IVoteType } from './election';
import { SDK_VERSION } from '../../version';
import { Asymmetric } from '../../util/encryption';

Expand Down Expand Up @@ -42,7 +42,11 @@ export class UnpublishedElection extends Election {
public addQuestion(
title: string | MultiLanguage<string>,
description: string | MultiLanguage<string>,
choices: Array<{ title: string; value: number } | { title: MultiLanguage<string>; value: number }>
choices: Array<
| { title: string; value: number; meta?: CustomMeta }
| { title: MultiLanguage<string>; value: number; meta?: CustomMeta }
>,
meta?: CustomMeta
): UnpublishedElection {
this._questions.push({
title: typeof title === 'string' ? { default: title } : title,
Expand All @@ -51,8 +55,10 @@ export class UnpublishedElection extends Election {
return {
title: typeof choice.title === 'string' ? { default: choice.title } : choice.title,
value: choice.value,
...(choice.meta && { meta: choice.meta }),
} as IChoice;
}),
...(meta && { meta: meta }),
});

return this;
Expand Down Expand Up @@ -110,10 +116,12 @@ export class UnpublishedElection extends Election {
return {
title: question.title,
description: question.description,
meta: question.meta,
choices: question.choices.map((choice) => {
return {
title: choice.title,
value: choice.value,
meta: choice.meta,
};
}),
};
Expand Down Expand Up @@ -204,11 +212,11 @@ export class UnpublishedElection extends Election {
this._streamUri = value;
}

get meta(): ElectionMeta {
get meta(): CustomMeta {
return super.meta;
}

set meta(value: ElectionMeta) {
set meta(value: CustomMeta) {
invariant(!value || value['sdk'] === undefined, 'Field `sdk` is restricted in metadata');
this._meta = value;
}
Expand Down
3 changes: 3 additions & 0 deletions src/types/metadata/election.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { object, array, string, number } from 'yup';
import { MultiLanguage, multiLanguageStringKeys } from '../../util/lang';
import { CustomMeta } from '../election';

/**
* Asserts that the given metadata is valid.
Expand All @@ -23,6 +24,7 @@ export function checkValidElectionMetadata(electionMetadata: ElectionMetadata):
export interface IChoice {
title: MultiLanguage<string>;
value: number;
meta?: CustomMeta;
results?: string;
answer?: number;
}
Expand All @@ -31,6 +33,7 @@ export interface IQuestion {
title: MultiLanguage<string>;
description?: MultiLanguage<string>;
numAbstains?: string;
meta?: CustomMeta;
choices: Array<IChoice>;
}

Expand Down
7 changes: 7 additions & 0 deletions test/integration/election.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ describe('Election integration tests', () => {
},
};

election.questions[0].meta = { image: 'https://img.io/test1.png' };
election.questions[0].choices[0].meta = { image: 'https://img.io/test2.png' };
election.questions[0].choices[1].meta = { image: 'https://img.io/test3.png' };

await client.createAccount();

await client
Expand Down Expand Up @@ -104,6 +108,9 @@ describe('Election integration tests', () => {
version: SDK_VERSION,
},
});
expect(publishedElection.questions[0].meta).toStrictEqual({ image: 'https://img.io/test1.png' });
expect(publishedElection.questions[0].choices[0].meta).toStrictEqual({ image: 'https://img.io/test2.png' });
expect(publishedElection.questions[0].choices[1].meta).toStrictEqual({ image: 'https://img.io/test3.png' });
expect(publishedElection.get('census.type')).toEqual('spreadsheet');
expect(publishedElection.electionType).toStrictEqual({
interruptible: true,
Expand Down
Loading