Skip to main content

Overview

The Election class is a core domain entity that represents an election in the Consensus e-voting platform. It encapsulates all election-related data and business logic, including candidate management, status transitions, and active state validation.

Constructor

new Election(
  electionID: string,
  name: string,
  electionType: ElectionType,
  startDate: Date,
  endDate: Date,
  description: string,
  status?: ElectionStatus
)
electionID
string
required
Unique identifier for the election
name
string
required
Name of the election (cannot be empty)
electionType
ElectionType
required
Type of election voting system. Supported values:
  • ElectionType.FPTP - First Past The Post
  • ElectionType.STV - Single Transferable Vote
  • ElectionType.AV - Alternative Vote
  • ElectionType.PREFERENTIAL - Preferential voting
startDate
Date
required
Date when the election begins
endDate
Date
required
Date when the election ends
description
string
required
Detailed description of the election
status
ElectionStatus
default:"ElectionStatus.DRAFT"
Current status of the election. Defaults to DRAFT if not specified.

Example

const election = new Election(
  "election-456",
  "Presidential Election",
  ElectionType.STV,
  new Date("2024-06-01"),
  new Date("2024-06-15"),
  "National presidential election"
);

Properties

electionID
string
required
Unique identifier for the election (read-only)
name
string
required
Name of the election. Can be updated via setter with validation.
electionType
ElectionType
required
Type of election voting system (read-only)
status
ElectionStatus
required
Current status of the election. Can be updated via setter to transition between states.
startDate
Date
required
Date when the election begins (read-only)
endDate
Date
required
Date when the election ends (read-only)
description
string
required
Detailed description of the election (read-only)
candidates
Candidate[]
required
Array of candidates in the election. Returns a copy for encapsulation (read-only array).

Setters

name

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

status

Updates the election status.
election.status = ElectionStatus.ACTIVE;
Valid transitions:
  • DRAFTACTIVE
  • ACTIVECLOSED
  • DRAFTCLOSED

Methods

isActive()

Checks if the election is currently active.
isActive(): boolean
Returns: true if the election status is ACTIVE and the current date is within the start and end dates. Example:
const election = new Election(
  "active-election",
  "Active",
  ElectionType.FPTP,
  new Date(Date.now() - 86400000), // Yesterday
  new Date(Date.now() + 86400000), // Tomorrow
  "Test",
  ElectionStatus.ACTIVE
);

election.isActive(); // true

isClosed()

Checks if the election is closed.
isClosed(): boolean
Returns: true if the election status is CLOSED.

addCandidate()

Adds a candidate to the election.
addCandidate(candidate: Candidate): void
candidate
Candidate
required
The candidate to add to the election
Throws: Error if the election status is not DRAFT. Note: Candidates can only be added to draft elections.

removeCandidate()

Removes a candidate from the election by ID.
removeCandidate(candidateID: string): void
candidateID
string
required
The ID of the candidate to remove
Throws: Error if the election status is not DRAFT. Note: Candidates can only be removed from draft elections.

getCandidateCount()

Returns the number of candidates in the election.
getCandidateCount(): number
Returns: The total number of candidates.

Usage Example

import { Election } from "./domain/entities/Election";
import { ElectionType, ElectionStatus } from "./domain/enums";

// Create a new election
const election = new Election(
  "election-123",
  "Test Election",
  ElectionType.FPTP,
  new Date("2024-01-01"),
  new Date("2024-12-31"),
  "Test election description"
);

// Update election name
election.name = "Updated Election Name";

// Transition to active status
election.status = ElectionStatus.ACTIVE;

// Check if active
if (election.isActive()) {
  console.log("Election is currently active");
}

// Get candidate count
console.log(`Total candidates: ${election.getCandidateCount()}`);

Build docs developers (and LLMs) love