# kudith/e-voting architecture

> Architecture diagram of the GitHub repository kudith/e-voting, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kudith/e-voting
- Repository: https://github.com/kudith/e-voting

## Overview

Based on our analysis of the file tree and README, this project is a Next.js application with both frontend and backend elements combined. Its purpose appears to be a voting or election management system, given the variety of API endpoints handling candidates, elections, faculties, majors, voters, and even vote submissions. Additionally, there are clear areas dedicated to different user roles (admin and voter) with specific dashboard pages and related UI components.

- Client (User Interface): Represent the web client (browser) accessing the application.
- Differentiate between the Admin Dashboard and the Voter Dashboard based on the directory structure (e.g., app/admin/dashboard and app/voter/dashboard).
- Frontend (Next.js Pages & Components): Show how the application’s layout, global styles (e.g., app/globals.css), and individual page files (e.g., app/page.js, app/layout.js) contribute to building the UI.
- Components like Navbar, Sidebar, forms, charts, and other reusable UI components found under the “components” folder.
- Backend (API Routes): Illustrate the various RESTful API endpoints organized under app/api for handling:
- Authentication (app/api/auth and related [kindeAuth] routes)
- Candidate management (create, delete, update, fetch candidates)
- Election management (similar CRUD operations and statistics)
- Faculties and Majors management
- OTP handling for security and authentication
- Vote operations and voter management
- Authentication Provider: Mark the presence of the AuthProvider and middleware (e.g., app/AuthProvider.jsx, middleware.js) that encapsulate the authentication and authorization logic.
- Database: Indicate data persistence via Prisma by highlighting the “prisma/schema.prisma”. This shows the connection between the API endpoints and the underlying database.
- Utility and Library Layers: Represent supporting layers such as the utilities (lib/utils.js), authentication helpers (lib/auth.js), custom hooks (hooks/use-mobile.js), and validations (validations/voterSchema.js).

##### Relationships and Data Flow
- From the Frontend, illustrate how user actions (e.g., submitting a vote, managing election data) invoke API calls to the Backend (app/api endpoints).
- Within the API layer, show how calls are routed to their specific endpoints (for example, from candidate creation to app/api/candidate/createCandidate/route.js).
- Authentication flow: The client communicates with authentication API routes, which interact with the AuthProvider and associated middleware.

##### Architectural Patterns and Design Principles
- Next.js Routing: The folder-based routing approach clearly separates pages and API endpoints.
- RESTful API Design: Grouped endpoints (e.g., candidate, election, voter) illustrate the standard CRUD operations that follow REST principles.
- Role-based Views: Separate admin-specific and voter-specific paths show a design that tailors the experience to different user roles.

##### Relevant Technologies and Tools
- Next.js framework for both SSR and client-side navigation.
- Prisma as an ORM for database interactions.
- JSX components and custom hooks for state and responsiveness.
- Integration with third-party authentication (using [kindeAuth]) and possibly OTP mechanisms.
- Styling and UI components with CSS and reusable UI modules (components/ui).

##### Following
- Clear, descriptive labels for each component (e.g., “Next.js Frontend”, “Admin API Endpoint: Create Candidate”, “Database via Prisma”).
- Annotations for critical middleware and security flows (auth provider, OTP routes).

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Client (Browser)"]:::client

    subgraph "Next.js Frontend"
        A1["Global Pages & Layout"]:::frontend
        A2["UI Components"]:::frontend
        A3["Admin Dashboard"]:::frontend
        A4["Voter Dashboard"]:::frontend
    end

    subgraph "Backend API"
        B1["Auth API Endpoints"]:::backend
        B2["Candidate API Endpoints"]:::backend
        B3["Election API Endpoints"]:::backend
        B4["Faculty API Endpoints"]:::backend
        B5["Major API Endpoints"]:::backend
        B6["OTP API Endpoints"]:::backend
        B7["Vote API Endpoints"]:::backend
        B8["Voter API Endpoints"]:::backend
        B9["VoterElection API Endpoints"]:::backend
    end

    subgraph "Authentication & Middleware"
        C1["Auth Provider"]:::auth
        C2["Middleware"]:::auth
        C3["Auth Helper"]:::auth
    end

    D["Database via Prisma"]:::database

    subgraph "Utilities & Helpers"
        U1["Utilities"]:::utility
        U2["Custom Hooks"]:::utility
        U3["Validations"]:::utility
    end

    %% Data Flow Connections
    A -->|"accesses"| A1
    A1 -->|"renders"| A2
    A2 -->|"navigates to"| A3
    A2 -->|"navigates to"| A4

    %% Frontend to API: User actions trigger API calls
    A1 -->|"API_calls"| B1

    %% API Endpoints interacting with Database
    B1 -->|"DB_ops"| D
    B2 -->|"DB_ops"| D
    B3 -->|"DB_ops"| D
    B4 -->|"DB_ops"| D
    B5 -->|"DB_ops"| D
    B6 -->|"DB_ops"| D
    B7 -->|"DB_ops"| D
    B8 -->|"DB_ops"| D
    B9 -->|"DB_ops"| D

    %% Authentication flow
    B1 -->|"uses_auth"| C1
    B1 -->|"checks_auth"| C2
    B1 -->|"helpers"| C3
    C1 ---|"session_info"| A1
    C2 ---|"session_info"| A1
    C3 ---|"session_info"| A1

    %% Utilities supporting Frontend
    U1 ---|"provides_funcs"| A2
    U2 ---|"provides_hooks"| A2
    U3 ---|"validates"| A2

    %% Click Events
    click A3 "https://github.com/kudith/e-voting/tree/main/app/admin/dashboard"
    click A4 "https://github.com/kudith/e-voting/blob/main/app/voter/dashboard/page.js"
    click A1 "https://github.com/kudith/e-voting/blob/main/app/page.js"
    click A2 "https://github.com/kudith/e-voting/tree/main/components"
    click B1 "https://github.com/kudith/e-voting/tree/main/app/api/auth"
    click B2 "https://github.com/kudith/e-voting/tree/main/app/api/candidate"
    click B3 "https://github.com/kudith/e-voting/tree/main/app/api/election"
    click B4 "https://github.com/kudith/e-voting/tree/main/app/api/faculty"
    click B5 "https://github.com/kudith/e-voting/tree/main/app/api/major"
    click B6 "https://github.com/kudith/e-voting/tree/main/app/api/otp"
    click B7 "https://github.com/kudith/e-voting/tree/main/app/api/vote"
    click B8 "https://github.com/kudith/e-voting/tree/main/app/api/voter"
    click B9 "https://github.com/kudith/e-voting/tree/main/app/api/voterElection"
    click C1 "https://github.com/kudith/e-voting/blob/main/app/AuthProvider.jsx"
    click C2 "https://github.com/kudith/e-voting/blob/main/middleware.js"
    click C3 "https://github.com/kudith/e-voting/blob/main/lib/auth.js"
    click D "https://github.com/kudith/e-voting/blob/main/prisma/schema.prisma"
    click U1 "https://github.com/kudith/e-voting/blob/main/lib/utils.js"
    click U2 "https://github.com/kudith/e-voting/blob/main/hooks/use-mobile.js"
    click U3 "https://github.com/kudith/e-voting/blob/main/validations/voterSchema.js"

    %% Styles
    classDef client fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef frontend fill:#c8e6c9,stroke:#1b5e20,stroke-width:2px;
    classDef backend fill:#ffcdd2,stroke:#b71c1c,stroke-width:2px;
    classDef auth fill:#f8bbd0,stroke:#880e4f,stroke-width:2px;
    classDef database fill:#b2ebf2,stroke:#006064,stroke-width:2px;
    classDef utility fill:#fff9c4,stroke:#f57f17,stroke-width:2px;
```
