Skip to main content

Overview

The Voter class is a domain entity that represents a registered voter in the Consensus e-voting platform. It encapsulates voter identity, authentication credentials, and registration status with controlled mutation through setters.

Constructor

new Voter(
  voterID: string,
  name: string,
  email: string,
  passwordHash: string,
  registrationStatus?: RegistrationStatus,
  registrationDate?: Date
)
voterID
string
required
Unique identifier for the voter
name
string
required
Full name of the voter (cannot be empty)
email
string
required
Email address of the voter (must contain @)
passwordHash
string
required
Hashed password for authentication (never store plain text)
registrationStatus
RegistrationStatus
default:"RegistrationStatus.PENDING"
Current registration status. Defaults to PENDING if not specified.
registrationDate
Date
default:"new Date()"
Date when the voter registered. Defaults to current date if not specified.

Example

const voter = new Voter(
  "voter-123",
  "John Doe",
  "[email protected]",
  "hashedPassword123",
  RegistrationStatus.PENDING,
  new Date("2024-01-01")
);

Properties

voterID
string
required
Unique identifier for the voter (read-only)
name
string
required
Full name of the voter. Can be updated via setter with validation.
email
string
required
Email address of the voter. Can be updated via setter with validation.
passwordHash
string
required
Hashed password for authentication. Can be updated via setter with validation.
registrationStatus
RegistrationStatus
required
Current registration status. Can be updated via setter.Valid values:
  • RegistrationStatus.PENDING - Awaiting approval
  • RegistrationStatus.APPROVED - Approved to vote
  • RegistrationStatus.REJECTED - Registration rejected
  • RegistrationStatus.SUSPENDED - Temporarily suspended
registrationDate
Date
required
Date when the voter registered (read-only)

Setters

name

Updates the voter’s name with validation.
voter.name = "Updated Name";
Validation:
  • Throws Error if name is empty or contains only whitespace

email

Updates the voter’s email address with validation.
voter.email = "[email protected]";
Validation:
  • Throws Error if email is empty or doesn’t contain @

passwordHash

Updates the voter’s password hash with validation.
voter.passwordHash = "newHashedPassword";
Validation:
  • Throws Error if password hash is empty

registrationStatus

Updates the voter’s registration status.
voter.registrationStatus = RegistrationStatus.APPROVED;
Valid transitions:
  • PENDINGAPPROVED
  • PENDINGREJECTED
  • APPROVEDSUSPENDED

Methods

isApproved()

Checks if the voter is approved.
isApproved(): boolean
Returns: true if the registration status is APPROVED. Example:
const voter = new Voter(
  "v1",
  "Test",
  "[email protected]",
  "hash",
  RegistrationStatus.APPROVED
);

voter.isApproved(); // true

isSuspended()

Checks if the voter is suspended.
isSuspended(): boolean
Returns: true if the registration status is SUSPENDED.

canVote()

Determines if the voter is eligible to cast a vote.
canVote(): boolean
Returns: true if the voter is approved and not suspended. Business Logic: A voter can vote only if they are both approved AND not suspended. Example:
const approvedVoter = new Voter(
  "v1",
  "John",
  "[email protected]",
  "hash",
  RegistrationStatus.APPROVED
);
approvedVoter.canVote(); // true

const suspendedVoter = new Voter(
  "v2",
  "Jane",
  "[email protected]",
  "hash",
  RegistrationStatus.SUSPENDED
);
suspendedVoter.canVote(); // false

Usage Example

import { Voter } from "./domain/entities/Voter";
import { RegistrationStatus } from "./domain/enums";

// Create a new voter
const voter = new Voter(
  "voter-456",
  "Jane Doe",
  "[email protected]",
  "hashedPassword456"
);

// Check default status
console.log(voter.registrationStatus); // RegistrationStatus.PENDING

// Approve the voter
voter.registrationStatus = RegistrationStatus.APPROVED;

// Check if voter can vote
if (voter.canVote()) {
  console.log("Voter is eligible to cast a ballot");
}

// Update voter name
voter.name = "Jane Smith";

// Update email
voter.email = "[email protected]";

Security Notes

  • Always store password hashes, never plain text passwords
  • The passwordHash property should only be set with properly hashed values
  • Email validation is basic; implement additional validation in your application layer

Build docs developers (and LLMs) love