Skip to content

Commit

Permalink
Added new meta fields for questions and choices
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Oct 25, 2024
1 parent 52f8f0e commit a937597
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
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
8 changes: 5 additions & 3 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 @@ -110,10 +110,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 +206,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

0 comments on commit a937597

Please sign in to comment.