# prince-1501/voting_app architecture

> Architecture diagram of the GitHub repository prince-1501/voting_app, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/prince-1501/voting_app
- Repository: https://github.com/prince-1501/voting_app

## Overview

##### Project Type and Purpose
- This is a RESTful backend service for a voting system built with Node.js/Express, MongoDB for persistence, and JWT for stateless authentication. There is no bundled frontend—clients (web or mobile) will consume its API.

##### File Structure
- Top-level
- server.js – bootstraps the Express app and wires up middleware, routes, database, and JWT.
- db.js – handles MongoDB connection logic.
- jwt.js – configures JWT signing and verification.
- routes/ – Express route definitions for user and candidate endpoints.
- models/ – Mongoose models for User and Candidate entities.

##### Extract Architecture Details from README
- Stateless REST API with endpoints for signup/login, candidate CRUD, voting, and profile management.
- Access control: Admin vs Regular User enforced via JWT claims.
- Persistence: MongoDB collections for user and candidate data.

##### Main Components
a. Client Layer
- Web/Mobile Client (Browser or App)
b. API Layer (Express Server)
- Authentication Middleware (JWT)
- Route Handlers (UserRoutes, CandidateRoutes)
- Business Logic / Controllers (embedded in route handlers)
c. Data Layer
- MongoDB Database
- Mongoose Models (User, Candidate)
d. (Optional) Test Data Injection
- Dummy Data Scripts
e. External Services (if any in future, e.g., Email/SMS for notifications)

##### Relationships & Data Flows
1. Client → POST /signup or /login → Auth Middleware → Server Issues JWT → Client
2. Client → GET/POST/PUT/DELETE /candidates (with JWT) → Route Handler → MongoDB → Response
3. Client → POST /candidates/vote/:id → Business Logic enforces “one vote per user” → Update Candidate model → Response
4. Client → GET /users/profile → Auth → Model → Response
- Token flow: after login, Client includes “Authorization: Bearer <JWT>” header on subsequent calls.

##### Architectural Patterns & Principles
- MVC-like separation:
- Models → Mongoose schemas
- Controllers/Routes → Express handlers
- Middleware → JWT auth layer
- Statelessness: no session storage on server
- Role-based access control (RBAC) scoped in middleware

##### Technologies & Frameworks
- Node.js and Express.js (API Server)
- MongoDB (NoSQL database)
- Mongoose (ODM library)
- JSON Web Tokens (jwt.js) for authentication
- Optional: dotenv for environment variables (not shown but likely present)

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Client Layer"
        WebClient["Web Client"]:::frontend
        MobileClient["Mobile Client"]:::frontend
    end

    subgraph "API Layer"
        ExpressServer["Express Server"]:::api
        subgraph "Security"
            AuthMiddleware["JWT Authentication Middleware"]:::security
        end
        subgraph "Routes"
            UserRoutes["User Routes"]:::api
            CandidateRoutes["Candidate Routes"]:::api
        end
    end

    subgraph "Data Persistence"
        MongoDB["MongoDB"]:::db
        UserModel["User Model"]:::models
        CandidateModel["Candidate Model"]:::models
    end

    subgraph "Test Data Source"
        DummyUserJS["dummy data/user.js"]:::testdata
        DummyCandidateJS["dummy data/candidate.js"]:::testdata
        DummyVoteDataJSON["dummy data/vote_data.json"]:::testdata
        DummySampleCandidateJSON["dummy data/sample_candiate.json"]:::testdata
    end

    WebClient -->|"HTTP Requests"| ExpressServer
    MobileClient -->|"HTTP Requests"| ExpressServer
    ExpressServer -->|"uses"| AuthMiddleware
    AuthMiddleware --> UserRoutes
    AuthMiddleware --> CandidateRoutes
    UserRoutes -->|"CRUD & Auth"| UserModel
    CandidateRoutes -->|"CRUD & Vote"| CandidateModel
    UserModel --> MongoDB
    CandidateModel --> MongoDB
    DummyUserJS -.-> MongoDB
    DummyCandidateJS -.-> MongoDB
    DummyVoteDataJSON -.-> MongoDB
    DummySampleCandidateJSON -.-> MongoDB

    click ExpressServer "https://github.com/prince-1501/voting_app/blob/main/server.js"
    click AuthMiddleware "https://github.com/prince-1501/voting_app/blob/main/jwt.js"
    click UserRoutes "https://github.com/prince-1501/voting_app/blob/main/routes/userRoutes.js"
    click CandidateRoutes "https://github.com/prince-1501/voting_app/blob/main/routes/candidateRoutes.js"
    click UserModel "https://github.com/prince-1501/voting_app/blob/main/models/user.js"
    click CandidateModel "https://github.com/prince-1501/voting_app/blob/main/models/candidate.js"
    click MongoDB "https://github.com/prince-1501/voting_app/blob/main/db.js"
    click DummyUserJS "https://github.com/prince-1501/voting_app/blob/main/dummy data/user.js"
    click DummyCandidateJS "https://github.com/prince-1501/voting_app/blob/main/dummy data/candidate.js"
    click DummyVoteDataJSON "https://github.com/prince-1501/voting_app/blob/main/dummy data/vote_data.json"
    click DummySampleCandidateJSON "https://github.com/prince-1501/voting_app/blob/main/dummy data/sample_candiate.json"

    classDef frontend fill:#f0f8ff,stroke:#036,stroke-width:1px
    classDef api fill:#e6ffe6,stroke:#090,stroke-width:1px
    classDef security fill:#ffe6e6,stroke:#c00,stroke-width:1px,stroke-dasharray: 4 2
    classDef db fill:#e6f7ff,stroke:#06c,stroke-width:1px,stroke-dasharray: 2 2
    classDef models fill:#f2e6ff,stroke:#60c,stroke-width:1px
    classDef testdata fill:#fff0e6,stroke:#c60,stroke-width:1px,stroke-dasharray: 2 2
```
