Skip to content

Commit

Permalink
Added nextElectionId function
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Nov 28, 2023
1 parent a2a037b commit 28b0295
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/api/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { API } from './api';

enum ElectionAPIMethods {
INFO = '/elections',
NEXT_ELECTION_ID = '/elections/id',
PRICE = '/elections/price',
KEYS = '/elections/{id}/keys',
CREATE = '/elections',
Expand Down Expand Up @@ -138,6 +139,13 @@ export interface IElectionCreateResponse {
metadataURL: number;
}

export interface IElectionNextIdResponse {
/**
* The next election identifier
*/
electionID: string;
}

export enum CensusTypeEnum {
CENSUS_UNKNOWN = 'CENSUS_UNKNOWN',
OFF_CHAIN_TREE = 'OFF_CHAIN_TREE',
Expand Down Expand Up @@ -409,6 +417,31 @@ export abstract class ElectionAPI extends API {
.catch(this.isApiError);
}

/**
* Returns the next election id.
*
* @param {string} url API endpoint URL
* @param {string} organizationId The identifier of the organization
* @param {number} censusOrigin The census origin
* @param {IVoteMode} envelopeType The envelope type
* @returns {Promise<IElectionNextIdResponse>}
*/
public static nextElectionId(
url: string,
organizationId: string,
censusOrigin: number,
envelopeType?: Partial<IVoteMode>
): Promise<IElectionNextIdResponse> {
return axios
.post<IElectionNextIdResponse>(url + ElectionAPIMethods.NEXT_ELECTION_ID, {
organizationId,
censusOrigin,
envelopeType,
})
.then((response) => response.data)
.catch(this.isApiError);
}

/**
* Returns the number of votes of a given election
*
Expand Down
19 changes: 19 additions & 0 deletions src/services/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ export class ElectionService extends Service implements ElectionServicePropertie
return ElectionAPI.create(this.url, payload, metadata);
}

/**
* Returns the next election id.
*
* @param {string} address The address of the account
* @param {UnpublishedElection} election The unpublished election
* @returns {Promise<string>} The next election identifier
*/
nextElectionId(address: string, election: UnpublishedElection): Promise<string> {
invariant(this.url, 'No URL set');
const censusOrigin = ElectionCore.censusOriginFromCensusType(election.census.type);
return ElectionAPI.nextElectionId(this.url, address, censusOrigin, {
serial: false, // TODO
anonymous: election.electionType.anonymous,
encryptedVotes: election.electionType.secretUntilTheEnd,
uniqueValues: election.voteType.uniqueChoices,
costFromWeight: election.voteType.costFromWeight,
}).then((response) => response.electionID);
}

/**
* Fetches the encryption keys from the specified process.
*
Expand Down
20 changes: 20 additions & 0 deletions test/integration/election.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -916,4 +916,24 @@ describe('Election integration tests', () => {
expect(election.census.censusURI).toEqual(census2.censusURI);
});
}, 185000);
it('should return the next election id', async () => {
const census = new PlainCensus();
census.add(await Wallet.createRandom().getAddress());

const election = createElection(census);

await client.createAccount();

await client
.createElection(election)
.then((electionId) => {
expect(electionId).toMatch(/^[0-9a-fA-F]{64}$/);
client.setElectionId(electionId);
return waitForElectionReady(client, electionId);
})
.then(async () => {
const nextElectionId = await client.electionService.nextElectionId(await client.wallet.getAddress(), election);
expect(nextElectionId).toEqual(client.electionId.slice(0, -1) + '1');
});
}, 85000);
});

0 comments on commit 28b0295

Please sign in to comment.