Skip to main content

Overview

The VoterService handles all business logic related to voter management in the Consensus e-voting platform. It manages voter registration, approval workflows, profile updates, and account deletion (GDPR compliance).

Constructor

constructor(voterRepository: IVoterRepository)
voterRepository
IVoterRepository
required
Repository for voter data persistence

Methods

registerVoter

Registers a new voter in the system.
registerVoter(dto: VoterRegistrationDTO, autoApprove: boolean = false): Voter
dto
VoterRegistrationDTO
required
Voter registration dataVoterRegistrationDTO:
  • name: string - Voter’s full name
  • email: string - Voter’s email address (must be unique)
  • password: string - Password (minimum 8 characters)
autoApprove
boolean
default:"false"
Whether to automatically approve the voter (sets status to APPROVED instead of PENDING)
Voter
Voter
The newly created voter entity with PENDING or APPROVED status
Throws:
  • "Email already registered" - Email address is already in use
  • "Invalid email format" - Email does not match valid format
  • "Password must be at least 8 characters" - Password too short
Example:
const validDTO: VoterRegistrationDTO = {
    name: "John Doe",
    email: "[email protected]",
    password: "securePassword123"
};

const voter = service.registerVoter(validDTO);
console.log(voter.registrationStatus); // RegistrationStatus.PENDING

// Auto-approve for testing
const approvedVoter = service.registerVoter(validDTO, true);
console.log(approvedVoter.registrationStatus); // RegistrationStatus.APPROVED

getVoterById

Retrieves a voter by their ID.
getVoterById(voterID: string): Voter | null
voterID
string
required
ID of the voter to retrieve
Voter | null
Voter | null
The voter entity if found, otherwise null

getVoterByEmail

Retrieves a voter by their email address (used for login).
getVoterByEmail(email: string): Voter | null
email
string
required
Email address of the voter
Voter | null
Voter | null
The voter entity if found, otherwise null
Example:
const voter = service.getVoterByEmail("[email protected]");
if (voter) {
    console.log(voter.name); // "Bob"
}

approveVoter

Approves a pending voter registration.
approveVoter(voterID: string): void
voterID
string
required
ID of the voter to approve
Throws:
  • "Voter not found" - Invalid voter ID
Notes:
  • Changes voter’s registration status to APPROVED
  • Only approved voters can cast votes
Example:
service.approveVoter(registeredVoter.voterID);
const updated = service.getVoterById(registeredVoter.voterID);
console.log(updated?.registrationStatus); // RegistrationStatus.APPROVED

rejectVoter

Rejects a pending voter registration.
rejectVoter(voterID: string): void
voterID
string
required
ID of the voter to reject
Throws:
  • "Voter not found" - Invalid voter ID
Notes:
  • Changes voter’s registration status to REJECTED
  • Rejected voters cannot cast votes

getAllVoters

Retrieves all registered voters.
getAllVoters(): Voter[]
Voter[]
Voter[]
Array of all voter entities
Example:
const voters = service.getAllVoters();
console.log(voters.length); // Total number of registered voters

updateVoter

Updates a voter’s profile information.
updateVoter(
    voterID: string, 
    updates: { name?: string; email?: string; passwordHash?: string }
): Voter
voterID
string
required
ID of the voter to update
updates
object
required
Object containing fields to update:
  • name?: string - New name
  • email?: string - New email (must be unique and valid)
  • passwordHash?: string - New password hash
Voter
Voter
The updated voter entity
Throws:
  • "Voter not found" - Invalid voter ID
  • "Email already registered" - New email is already in use
  • "Invalid email format" - New email does not match valid format
Notes:
  • Only provided fields are updated
  • Email uniqueness is validated when changing email
  • Email format is validated when changing email
Example:
const updated = service.updateVoter(voterID, {
    name: "Jane Smith",
    email: "[email protected]"
});

deleteVoter

Deletes a voter account (GDPR compliance).
deleteVoter(voterID: string): void
voterID
string
required
ID of the voter to delete
Throws:
  • "Voter not found" - Invalid voter ID
Notes:
  • Permanently removes voter data from the system
  • Supports GDPR “right to be forgotten” compliance
  • Vote anonymity ensures ballots remain valid after deletion

RegistrationStatus

Enum representing voter registration states:
  • PENDING - Registration submitted, awaiting approval
  • APPROVED - Registration approved, can vote
  • REJECTED - Registration rejected, cannot vote

Validation Rules

Email Format

Email addresses must match the pattern: ^[^\s@]+@[^\s@]+\.[^\s@]+$ Valid examples: Invalid examples:
  • invalid (no @ symbol)
  • user@domain (no TLD)
  • user @example.com (contains spaces)

Password Requirements

  • Minimum length: 8 characters
  • No maximum length enforced
  • Passwords are stored as hashes in production

Build docs developers (and LLMs) love