Skip to main content

Overview

The VoterRepository class handles all database operations for voters in the Consensus e-voting platform. It implements the IVoterRepository interface and uses SQLite for data persistence.

Constructor

constructor(db?: Database.Database)
db
Database.Database
Optional database instance. If not provided, uses the singleton DatabaseConnection instance.

Methods

save

Persists a new voter to the database.
save(voter: Voter): void
voter
Voter
required
The voter entity to save to the database.
return
void
No return value. Throws an error if the save operation fails.
Example:
const voter = new Voter(
  "voter-123",
  "John Doe",
  "[email protected]",
  "$2b$10$hashedpassword",
  RegistrationStatus.PENDING
);

voterRepository.save(voter);

findById

Retrieves a voter by their unique identifier.
findById(voterID: string): Voter | null
voterID
string
required
The unique identifier of the voter to retrieve.
return
Voter | null
Returns the Voter entity if found, or null if no voter exists with the given ID.
Example:
const voter = voterRepository.findById("voter-123");
if (voter) {
  console.log(voter.name);
}

findByEmail

Retrieves a voter by their email address.
findByEmail(email: string): Voter | null
email
string
required
The email address of the voter to retrieve.
return
Voter | null
Returns the Voter entity if found, or null if no voter exists with the given email.
Example:
const voter = voterRepository.findByEmail("[email protected]");
if (voter) {
  console.log(`Found voter: ${voter.name}`);
}

update

Updates an existing voter in the database.
update(voter: Voter): void
voter
Voter
required
The voter entity with updated values. The voterID must match an existing voter.
return
void
No return value. Throws an error if the update operation fails.
Example:
const voter = voterRepository.findById("voter-123");
if (voter) {
  voter.registrationStatus = RegistrationStatus.APPROVED;
  voterRepository.update(voter);
}

delete

Deletes a voter from the database.
delete(voterID: string): void
voterID
string
required
The unique identifier of the voter to delete.
return
void
No return value. Throws an error if the delete operation fails.
Example:
voterRepository.delete("voter-123");

findAll

Retrieves all voters from the database.
findAll(): Voter[]
return
Voter[]
Array of all voters in the database. Returns empty array if no voters exist.
Example:
const allVoters = voterRepository.findAll();
console.log(`Total registered voters: ${allVoters.length}`);

Voter

The Voter entity with the following properties:
  • voterID: string - Unique identifier
  • name: string - Voter’s full name
  • email: string - Voter’s email address
  • passwordHash: string - Hashed password for authentication
  • registrationStatus: RegistrationStatus - Current registration status
  • registrationDate: Date - Date when the voter registered

RegistrationStatus

Enum with values:
  • PENDING - Registration is awaiting approval
  • APPROVED - Voter is approved and can vote
  • SUSPENDED - Voter account is suspended

Build docs developers (and LLMs) love