Skip to content

Commit

Permalink
Added CspCensus in fetchElection to avoid requests to the census API
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Jan 11, 2024
1 parent f46fd2e commit 373fa28
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/services/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { Service, ServiceProperties } from './service';
import {
ArchivedElection,
Census,
CspCensus,
InvalidElection,
PublishedCensus,
PublishedElection,
UnpublishedElection,
} from '../types';
import { AccountAPI, ElectionAPI, IElectionCreateResponse, IElectionKeysResponse } from '../api';
import { AccountAPI, CensusTypeEnum, ElectionAPI, IElectionCreateResponse, IElectionKeysResponse } from '../api';
import { CensusService } from './census';
import { allSettled } from '../util/promise';
import invariant from 'tiny-invariant';
Expand Down Expand Up @@ -97,6 +98,15 @@ export class ElectionService extends Service implements ElectionServicePropertie
);
}

private buildCensus(electionInfo): Promise<PublishedCensus | ArchivedCensus> {
if (electionInfo.census.censusOrigin === CensusTypeEnum.OFF_CHAIN_CA) {
return Promise.resolve(new CspCensus(electionInfo.census.censusRoot, electionInfo.census.censusURL));
}
return electionInfo.fromArchive
? Promise.resolve(new ArchivedCensus(electionInfo.census.censusRoot, electionInfo.census.censusURL))
: this.buildPublishedCensus(electionInfo);
}

/**
* Fetches info about an election.
*
Expand All @@ -122,9 +132,7 @@ export class ElectionService extends Service implements ElectionServicePropertie
meta: electionInfo.metadata?.meta,
startDate: electionInfo.startDate,
endDate: electionInfo.endDate,
census: electionInfo.fromArchive
? new ArchivedCensus(electionInfo.census.censusRoot, electionInfo.census.censusURL)
: await this.buildPublishedCensus(electionInfo),
census: await this.buildCensus(electionInfo),
maxCensusSize: electionInfo.census.maxCensusSize,
manuallyEnded: electionInfo.manuallyEnded,
fromArchive: electionInfo.fromArchive,
Expand Down
1 change: 1 addition & 0 deletions test/integration/csp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('CSP tests', () => {
expect(election.title).toEqual(election.title);
expect(election.voteCount).toEqual(numVotes);
expect(election.results[0][0]).toEqual(election.results[0][1]);
expect(election.census).toBeInstanceOf(CspCensus);
expect(election.census.size).toBeUndefined();
expect(election.census.weight).toBeUndefined();
});
Expand Down

0 comments on commit 373fa28

Please sign in to comment.