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

Fixed bug when using metadata template and deep copying it #417

Merged
merged 2 commits into from
Oct 4, 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
4 changes: 2 additions & 2 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 { UnpublishedElection } from './unpublished';
import { ElectionMetadata, ElectionMetadataTemplate, ElectionResultsTypeNames } from '../metadata';
import { ElectionMetadata, ElectionResultsTypeNames, getElectionMetadataTemplate } from '../metadata';
import { Vote } from '../vote';

export interface IApprovalElectionParameters extends IElectionParameters {}
Expand Down Expand Up @@ -63,7 +63,7 @@ export class ApprovalElection extends UnpublishedElection {
}

public generateMetadata(): ElectionMetadata {
const metadata = ElectionMetadataTemplate;
const metadata = getElectionMetadataTemplate();

metadata.type = {
name: ElectionResultsTypeNames.APPROVAL,
Expand Down
9 changes: 7 additions & 2 deletions src/types/election/budget.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { MultiLanguage } from '../../util/lang';
import { IElectionParameters, IVoteType } from './election';
import { UnpublishedElection } from './unpublished';
import { ElectionMetadata, ElectionMetadataTemplate, ElectionResultsType, ElectionResultsTypeNames } from '../metadata';
import {
ElectionMetadata,
ElectionResultsType,
ElectionResultsTypeNames,
getElectionMetadataTemplate,
} from '../metadata';
import { Vote } from '../vote';

export interface IBudgetElectionParametersInfo extends IElectionParameters {
Expand Down Expand Up @@ -86,7 +91,7 @@ export class BudgetElection extends UnpublishedElection {
}

public generateMetadata(): ElectionMetadata {
const metadata = ElectionMetadataTemplate;
const metadata = getElectionMetadataTemplate();

metadata.type = {
name: ElectionResultsTypeNames.BUDGET,
Expand Down
4 changes: 2 additions & 2 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 { UnpublishedElection } from './unpublished';
import { ElectionMetadata, ElectionMetadataTemplate, ElectionResultsTypeNames } from '../metadata';
import { ElectionMetadata, ElectionResultsTypeNames, getElectionMetadataTemplate } from '../metadata';
import { Vote } from '../vote';

export interface IMultiChoiceElectionParameters extends IElectionParameters {
Expand Down Expand Up @@ -73,7 +73,7 @@ export class MultiChoiceElection extends UnpublishedElection {
}

public generateMetadata(): ElectionMetadata {
const metadata = ElectionMetadataTemplate;
const metadata = getElectionMetadataTemplate();

metadata.type = {
name: ElectionResultsTypeNames.MULTIPLE_CHOICE,
Expand Down
9 changes: 7 additions & 2 deletions src/types/election/quadratic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { MultiLanguage } from '../../util/lang';
import { IElectionParameters, IVoteType } from './election';
import { UnpublishedElection } from './unpublished';
import { ElectionMetadata, ElectionMetadataTemplate, ElectionResultsType, ElectionResultsTypeNames } from '../metadata';
import {
ElectionMetadata,
ElectionResultsType,
ElectionResultsTypeNames,
getElectionMetadataTemplate,
} from '../metadata';
import { Vote } from '../vote';

export interface IQuadraticElectionParametersInfo extends IElectionParameters {
Expand Down Expand Up @@ -91,7 +96,7 @@ export class QuadraticElection extends UnpublishedElection {
}

public generateMetadata(): ElectionMetadata {
const metadata = ElectionMetadataTemplate;
const metadata = getElectionMetadataTemplate();

metadata.type = {
name: ElectionResultsTypeNames.QUADRATIC,
Expand Down
4 changes: 2 additions & 2 deletions src/types/election/unpublished.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MultiLanguage } from '../../util/lang';
import {
checkValidElectionMetadata,
ElectionMetadata,
ElectionMetadataTemplate,
getElectionMetadataTemplate,
IChoice,
IQuestion,
} from '../metadata';
Expand Down Expand Up @@ -91,7 +91,7 @@ export class UnpublishedElection extends Election {

public generateMetadata(metadata?: ElectionMetadata): ElectionMetadata {
if (!metadata) {
metadata = ElectionMetadataTemplate;
metadata = getElectionMetadataTemplate();
}

metadata.title = this.title;
Expand Down
4 changes: 4 additions & 0 deletions src/types/metadata/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,7 @@ export const ElectionMetadataTemplate: ElectionMetadata = {
properties: {},
},
};

export const getElectionMetadataTemplate = () => {
return JSON.parse(JSON.stringify(ElectionMetadataTemplate));
};
4 changes: 2 additions & 2 deletions test/integration/other.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElectionMetadataTemplate, VocdoniSDKClient } from '../../src';
import { ElectionMetadataTemplate, getElectionMetadataTemplate, VocdoniSDKClient } from '../../src';
// @ts-ignore
import { clientParams } from './util/client.params';

Expand All @@ -11,7 +11,7 @@ describe('Other tests', () => {
});
it('should calculate the correct IPFS hash with special characters', async () => {
const client = new VocdoniSDKClient(clientParams());
const election = ElectionMetadataTemplate;
const election = getElectionMetadataTemplate();
election.title = {
default: 'Això és un títol',
};
Expand Down
4 changes: 2 additions & 2 deletions test/unit/types/metadata/election.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ElectionMetadata, checkValidElectionMetadata, ElectionMetadataTemplate } from '../../../../src';
import { ElectionMetadata, checkValidElectionMetadata, getElectionMetadataTemplate } from '../../../../src';

let electionMetadata: ElectionMetadata;

beforeEach(() => {
electionMetadata = ElectionMetadataTemplate;
electionMetadata = getElectionMetadataTemplate();
});

describe('Election metadata', () => {
Expand Down
Loading