Overview
TheElectionService handles all business logic related to elections in the Consensus e-voting platform. It manages the complete election lifecycle from draft creation through activation and closure, implements the Observer pattern for election state changes, and enforces validation rules for election integrity.
Constructor
Repository for election data persistence
Repository for candidate data persistence
Optional event emitter for Observer pattern notifications (created automatically if not provided)
Methods
createElection
Creates a new election in DRAFT status.Election creation dataElectionCreationDTO:
name: string- Election nameelectionType: ElectionType- Type (FPTP, STV, AV, PREFERENTIAL)startDate: Date- Election start dateendDate: Date- Election end datedescription: string- Election description
The newly created election entity with DRAFT status
"End date must be after start date"- Invalid date range"Start date cannot be in the past"- Start date before current date
addCandidate
Adds a candidate to a draft election.ID of the election to add the candidate to
Candidate dataCandidateCreationDTO:
name: string- Candidate nameparty: string- Political party affiliationbiography: string- Candidate biography
The newly created candidate entity
"Election not found"- Invalid election ID"Cannot add candidates to non-draft elections"- Election is already active or closed
removeCandidate
Removes a candidate from a draft election.ID of the election
ID of the candidate to remove
"Election not found"- Invalid election ID"Cannot remove candidates from non-draft elections"- Election is already active or closed"Candidate not found"- Invalid candidate ID"Candidate does not belong to this election"- Candidate/election mismatch
getElectionById
Retrieves an election by its ID.ID of the election to retrieve
The election entity if found, otherwise null
getCandidates
Retrieves all candidates for a specific election.ID of the election
Array of candidate entities for the election
getActiveElections
Retrieves all currently active elections.Array of active election entities
getAllElections
Retrieves all elections regardless of status.Array of all election entities
getClosedElections
Retrieves all closed elections.Array of closed election entities
activateElection
Activates a draft election, making it available for voting.ID of the election to activate
"Election not found"- Invalid election ID"Election must have at least 2 candidates"- Insufficient candidates
- Triggers Observer pattern notification to all subscribers
- Changes election status from DRAFT to ACTIVE
closeElection
Closes an election, preventing further votes.ID of the election to close
"Election not found"- Invalid election ID
- Triggers Observer pattern notification to all subscribers
- Changes election status to CLOSED
- Results become available after closure
deleteElection
Deletes a draft election and all its candidates.ID of the election to delete
"Election not found"- Invalid election ID"Only draft elections can be deleted"- Election is active or closed
- Automatically deletes all associated candidates
- Cannot delete active or closed elections
getEventEmitter
Returns the event emitter to allow external code to subscribe observers.The event emitter instance for subscribing to election state changes
Related Types
ElectionStatus
Enum representing election lifecycle states:DRAFT- Election created but not yet activeACTIVE- Election open for votingCLOSED- Election ended, results available
ElectionType
Enum representing voting systems:FPTP- First Past The PostSTV- Single Transferable VoteAV- Alternative VotePREFERENTIAL- Preferential voting