Overview
TheVotingService handles all business logic related to voting in the Consensus e-voting platform. It manages vote casting with strict validation, ballot anonymization using the Adapter pattern, and result calculation using the Strategy pattern for different voting systems.
Constructor
Repository for anonymous ballot persistence
Repository for tracking voter eligibility and voting status
Repository for vote confirmation receipts
Repository for election data
Repository for candidate data
Methods
castVote
Casts a vote with full validation and anonymization.The voter entity casting the vote
Vote dataCastVoteDTO:
voterID: string- ID of the voterelectionID: string- ID of the electioncandidateID?: string- Single candidate selection (for FPTP)preferences?: string[]- Ranked preferences (for STV/AV/Preferential)
Digital confirmation receipt with unique confirmation ID
"Voter is not approved"- Voter registration status is not APPROVED"Election not found"- Invalid election ID"Election is not active"- Election status is not ACTIVE"Election is not currently open for voting"- Current date outside election date range"Voter has already voted in this election"- Duplicate vote attempt"Invalid candidate ID"- Candidate does not exist in this election"Invalid candidate ID in preferences: {id}"- Preference contains invalid candidate"No voting strategy for election type: {type}"- Unsupported election type"Invalid ballot: Please ensure each candidate has a unique ranking with no duplicates."- Ballot validation failed"Ballot anonymity verification failed"- Anonymization process failed
- Voter must be APPROVED
- Election must be ACTIVE
- Current date must be within election start/end dates
- Voter cannot have already voted in this election
- All candidate IDs must belong to the election
- Ballot must pass voting strategy validation
- Ballot must pass anonymity verification
- Validates voter approval status
- Validates election exists and is active
- Checks election date range
- Verifies voter hasn’t already voted
- Validates all candidate IDs
- Creates ballot using BallotFactory
- Validates ballot using voting strategy
- Anonymizes ballot using AnonymousBallotAdapter
- Verifies ballot anonymity
- Stores anonymous ballot (no voter link)
- Stores confirmation receipt
- Marks voter as having voted
getVoterConfirmations
Retrieves all vote confirmations for a voter.ID of the voter
Array of confirmation receipts (contains no vote details for privacy)
- Confirmations do not contain vote choices (privacy protection)
- Used to prove participation without revealing vote
hasVoted
Checks if a voter has already voted in a specific election.ID of the voter
ID of the election
True if voter has voted in this election, false otherwise
calculateResults
Calculates election results using the appropriate voting strategy.ID of the election
Array of results containing vote counts per candidateVoteResult:
candidateID: string- Candidate identifiervotes: number- Number of votes received- Additional fields may vary by voting strategy
"Election not found"- Invalid election ID"Results only available for closed elections"- Election is not CLOSED"No voting strategy for election type: {type}"- Unsupported election type
- Only available for CLOSED elections
- Uses Strategy pattern to apply correct counting algorithm
- FPTP: Simple vote count
- STV: Single Transferable Vote algorithm
- AV: Alternative Vote (Instant Runoff)
getVoteCount
Returns the total number of votes cast in an election.ID of the election
Total number of ballots cast
Voting Strategies
The service automatically selects the appropriate voting strategy based on election type:FPTP (First Past The Post)
- Single candidate selection (
candidateIDrequired) - Winner is candidate with most votes
- No preference ranking
STV (Single Transferable Vote)
- Ranked preferences (
preferencesarray required) - Proportional representation
- Transfers votes based on preferences
AV (Alternative Vote)
- Ranked preferences (
preferencesarray required) - Instant runoff voting
- Eliminates lowest candidates and redistributes votes
PREFERENTIAL
- Uses STV strategy
- Ranked preferences (
preferencesarray required)
Privacy & Security
Ballot Anonymization
The service uses theAnonymousBallotAdapter to ensure:
- Ballots contain no voter identification
- Stored ballots cannot be traced back to voters
- Anonymity is verified before storage
Vote Confirmation
Confirmations provide:- Proof of participation
- Unique confirmation ID
- Timestamp of vote
- No vote details (maintains secret ballot)
Example: Anonymity Verification
Related Types
ElectionStatus
Enum for election states:DRAFT- Election being set upACTIVE- Voting is openCLOSED- Voting ended, results available
ElectionType
Enum for voting systems:FPTP- First Past The PostSTV- Single Transferable VoteAV- Alternative VotePREFERENTIAL- Preferential voting
RegistrationStatus
Enum for voter approval:PENDING- Awaiting approvalAPPROVED- Can voteREJECTED- Cannot vote