# kalebb12/bus-ticket-booking-system architecture

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

- Interactive diagram: https://gitdiagram.com/kalebb12/bus-ticket-booking-system
- Repository: https://github.com/kalebb12/bus-ticket-booking-system

## Overview

##### Project Type & Purpose
- The project is a backend service for a bus ticket booking system.
- It manages functionalities such as user registration/login, email verification, ride management (create, update, cancel), seat booking, QR code generation for tickets, and email notifications.
- The application uses JWT for authentication, and further integrates with external services like Nodemailer for emailing and a QR code package for generating tickets.

##### Overall Architectural Components
a. Application Server:
- Express-based server which handles incoming HTTP requests.
- This is initiated in files like server.js (or index.js/app.js from the file tree) where the Express app is configured.
b. API Controllers:
- These modules encapsulate business logic and interact with models.
c. Routing Layer:
- “routes” directory, with separate route definitions for admin, bookings, rides, and users.
- How each route connects to its corresponding controller.
d. Middleware:
- Authentication and request handling/validation middleware (e.g., protectMiddleware.js, protectBooking.js, restrictAccessMiddleware.js) that ensures security and validation before controllers are invoked.
e. Data Models:
f. Utilities & Helpers:
- Utility functions in the “utils” folder (error handling, token creation, QR code generation, email sending, token expiry checks).
g. External/Third-party Integrations:
- Components for integration with email services (using Nodemailer with email templates), and QR code generation (using the qrcode package).
- JWT handling, password hashing (bcrypt), and input validation libraries (Joi, Express Validator).

##### Relationships & Interactions
- Incoming HTTP requests flow from client endpoints (e.g., /api/users/register, /api/bookings) into the Application Server.
- The Routing layer directs these requests to the appropriate controller based on the endpoint.
- Controllers then interact with Models to perform CRUD operations on the database and use Utilities to handle tasks such as sending emails or generating QR codes.
- Middleware components are placed in the path (pre-controller) to enforce security and data validation.
- Some components may run background jobs (e.g., sending booking confirmations through email, or running crons for scheduled tasks) as indicated by files like crons.js.

##### Architectural Patterns & Design Principles
- Modular design through separation of concerns: Controllers, Routes, Models, and Middlewares are all separated.
- Use of the MVC-inspired pattern: Although not strictly MVC, the controllers and models are organized to keep business logic separate from routing.
- Emphasis on security (JWT authentication, password hashing, input validation) and error handling (via custom error handling modules in utils).

## Diagram (Mermaid)

```mermaid
flowchart TD
    CR["Client Request"]:::client
    AS["Application Server"]:::app

    subgraph "Routing Layer"
        UR["User Routes"]:::route
        RR["Rides Routes"]:::route
        BR["Bookings Routes"]:::route
        AR["Admin Routes"]:::route
    end

    subgraph "Middleware Layer"
        PM["Protect Middleware"]:::middleware
        BPM["Booking Protection Middleware"]:::middleware
        RAM["Restrict Access Middleware"]:::middleware
    end

    subgraph "API Controllers"
        UC["User Controller"]:::controller
        RC["Rides Controller"]:::controller
        BC["Bookings Controller"]:::controller
        EC["Error Controller"]:::controller
        HF["Handler Factory"]:::controller
    end

    subgraph "Data Models"
        UM["User Model"]:::models
        RM["Ride Model"]:::models
        BM["Booking Model"]:::models
    end

    DB["Database\n(MongoDB)"]:::db

    subgraph "Utilities & Helpers"
        AE["App Error"]:::utils
        CA["Catch Async"]:::utils
        CST["Create Sign Token"]:::utils
        GQ["Generate QR Code"]:::utils
        GQB["Generate QR Code Buffer"]:::utils
        CTE["Check Token Expiry"]:::utils
        SE["Send Email"]:::utils
    end

    subgraph "External Services"
        ET["Email Tickets"]:::external
        EVT["Email Verification Template"]:::external
    end

    SC["Crons"]:::scheduled

    %% Flow Connections
    CR -->|"HTTP_Request"| AS
    AS -->|"Route_Resolution"| UR
    %% Routes to Middleware (representative connection)
    UR -->|"AuthCheck"| PM
    %% Middleware to Controllers
    PM -->|"Process_Request"| UC
    %% Controllers to Models (CRUD operations)
    UC -->|"CRUD_Operation"| UM
    RC -->|"CRUD_Operation"| RM
    BC -->|"CRUD_Operation"| BM
    %% Models to Database
    UM -->|"Persist_Data"| DB
    RM -->|"Persist_Data"| DB
    BM -->|"Persist_Data"| DB
    %% Controllers to Utilities (Service Calls)
    BC -->|"Service_Call"| SE
    %% Controllers to External Services (Notifications/QR)
    RC -->|"NotifyQR"| ET
    BC -->|"NotifyQR"| EVT
    %% Scheduled Tasks triggering Email utility
    SC -->|"BackgroundJob"| SE

    %% Styles
    classDef client fill:#ffa502,stroke:#000,stroke-width:1px;
    classDef app fill:#a0d0f0,stroke:#000,stroke-width:1px;
    classDef route fill:#d0f0d0,stroke:#000,stroke-width:1px;
    classDef middleware fill:#f9e79f,stroke:#000,stroke-width:1px;
    classDef controller fill:#a0d0f0,stroke:#000,stroke-width:1px;
    classDef models fill:#d8b3ff,stroke:#000,stroke-width:1px;
    classDef utils fill:#f3a683,stroke:#000,stroke-width:1px;
    classDef external fill:#7DCEA0,stroke:#000,stroke-width:1px;
    classDef scheduled fill:#b2bec3,stroke:#000,stroke-width:1px;
    classDef db fill:#d1ccc0,stroke:#000,stroke-width:1px;

click AS "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/app.js"
click UC "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/controllers/userController.js"
click RC "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/controllers/ridesController.js"
click BC "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/controllers/bookingsController.js"
click EC "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/controllers/errorController.js"
click HF "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/controllers/handlerFactory.js"
click UR "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/routes/userRoutes.js"
click RR "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/routes/ridesRoutes.js"
click BR "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/routes/bookingsRoutes.js"
click AR "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/routes/adminRoutes.js"
click PM "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/middlewares/protectMiddleware.js"
click BPM "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/middlewares/protectBooking.js"
click RAM "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/middlewares/restrictAccessMiddleware.js"
click UM "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/models/userModels.js"
click RM "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/models/rideModel.js"
click BM "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/models/bookingModels.js"
click AE "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/utils/appError.js"
click CA "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/utils/catchAsync.js"
click CST "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/utils/createSignToken.js"
click GQ "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/utils/generateQRCode.js"
click GQB "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/utils/generateQrCodeBuffer.js"
click CTE "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/utils/istokenExpired.js"
click SE "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/utils/sendEmail.js"
click ET "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/templates/emailTickets.js"
click EVT "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/templates/emailVerificationTemplate.js"
click SC "https://github.com/kalebb12/bus-ticket-booking-system/blob/main/crons.js"
```
