Skip to content

Commit

Permalink
Added new changeElectionEndDate in client for changing the election…
Browse files Browse the repository at this point in the history
… end date directly by providing a date.
  • Loading branch information
marcvelmer committed Jun 26, 2024
1 parent 0075406 commit cdc378c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,25 @@ export class VocdoniSDKClient {
.then((hash) => this.chainService.waitForTransaction(hash));
}

/**
* Changes the end date of an election.
* @category Election
*
* @param electionId - The id of the election
* @param endDate - The new end date
*/
public changeElectionEndDate(electionId: string, endDate: string | number | Date): Promise<void> {
const date = new Date(endDate);
invariant(date instanceof Date, 'Date not valid');
if (!this.electionId && !electionId) {
throw Error('No election set');
}

return this.fetchElection(electionId ?? this.electionId).then((election) =>
this.changeElectionDuration(electionId ?? this.electionId, date.getTime() - election.startDate.getTime())
);
}

/**
* Checks if the user is in census.
* @category Voting
Expand Down
11 changes: 10 additions & 1 deletion test/integration/election.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ describe('Election integration tests', () => {

await client.createAccount();

let account, publishedElection;
let account, publishedElection, changedDate;

await client
.createElection(election)
Expand All @@ -1413,6 +1413,7 @@ describe('Election integration tests', () => {
})
.then(() => client.fetchElection())
.then(async (election) => {
changedDate = election.endDate;
expect(new Date(publishedElection.endDate).getTime()).toBeLessThan(new Date(election.endDate).getTime());
expect(new Date(election.endDate).getTime()).toBeGreaterThan(
new Date(publishedElection.endDate).getTime() + startDateStride + 60 * 60
Expand All @@ -1421,6 +1422,14 @@ describe('Election integration tests', () => {
await expect(async () => {
await client.changeElectionDuration(election.id, -(startDateStride + 60 * 60));
}).rejects.toThrow();
return client.changeElectionEndDate(
election.id,
new Date(election.startDate.getTime() + startDateStride + 60 * 60 + 1)
);
})
.then(() => client.fetchElection())
.then(async (election) => {
expect(new Date(changedDate).getTime()).toBe(new Date(election.endDate).getTime() - 1000);
});
}, 185000);
});

0 comments on commit cdc378c

Please sign in to comment.