# nikhil-gna/activity-booking-api architecture

> Architecture diagram of the GitHub repository nikhil-gna/activity-booking-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nikhil-gna/activity-booking-api
- Repository: https://github.com/nikhil-gna/activity-booking-api

## Overview

##### Project type and purpose
- This is a stateless, RESTful API backend for an Activity Booking App, built with Node.js and Express, using MongoDB (Mongoose) as its datastore and JWT for authentication. There is no separate frontend in this repo—clients (web/mobile apps, Postman, etc.) consume the API endpoints.

##### File structure
- Top-level: package.json, README, deployment and env config.
- src/index.js: server bootstrap and Express app entry.
- src/app.js: Express app configuration (middleware, error handlers, CORS).
- src/routes/*: HTTP route definitions by resource (auth, activities, booking, healthcheck).
- src/controllers/*: business-logic for each route.
- src/middlewares/auth.middleware.js: JWT token validation/guard.
- src/models/*: Mongoose schema definitions (User, Activity, Booking).
- src/db/index.js: MongoDB connection logic.
- src/utils/*: generic helpers (ApiError, ApiResponse, asyncHandler).

##### Extract architectural details from README
- Node + Express for HTTP.
- MongoDB with Mongoose for persistence.
- bcrypt for password hashing, JWT for stateless auth.
- Hosted on Render (implies a load-balanced, containerized or auto-scaled deployment).

##### Main components
a. External Clients
‑ Mobile/Web App or API Consumer (e.g., Postman).
b. (Optional) Load Balancer or API Gateway
‑ Represents Render’s routing layer.
c. Express API Server
‑ Routes Layer (activity.routes, auth.routes, booking.routes, healthcheck.routes).
‑ Middleware Layer (CORS, JSON parsing, auth.middleware).
‑ Controller Layer (auth.controller, activity.controller, booking.controller, healthcheck.controller).
‑ Utility Layer (ApiError, ApiResponse, asyncHandler).
d. Database Layer
‑ Mongoose models and MongoDB cluster.
e. Authentication Subsystem
‑ JWT issuer/validator, bcrypt for hashing.

##### Relationships and data flow
- Client → HTTP Request → Load Balancer → Express Routes → Auth Middleware (for protected routes) → Controller → Mongoose Model → MongoDB → Controller → HTTP Response → Client.
- Both successful flows (200 OK) and error flows (ApiError → error handler → client).

##### Architectural patterns and principles
- Layered (Route → Middleware → Controller → Model → DB).
- MVC-style separation of concerns.
- Stateless, token-based authentication.
- Single-responsibility per file/module.
- Asynchronous error handling via asyncHandler and centralized error middleware.

##### Technologies and frameworks to annotate
- Node.js (runtime)
- Express.js (web framework)
- Mongoose (ORM)
- MongoDB (NoSQL DB)
- JWT (jsonwebtoken library)
- bcrypt (password hashing)
- Render.com (deployment)

## Diagram (Mermaid)

```mermaid
flowchart TD
    client["External Client\n(Web/Mobile/Postman)"]:::external
    lb["Load Balancer\n(Render)"]:::external

    subgraph "Express API Server"
        direction TB
        index["Server Bootstrap\n(src/index.js)"]:::server
        app["Express App Config\n(src/app.js)"]:::server

        subgraph "Routes Layer"
            direction TB
            actRoutes["Activity Routes"]:::server
            authRoutes["Auth Routes"]:::server
            bookRoutes["Booking Routes"]:::server
            healthRoutes["Healthcheck Routes"]:::server
        end

        subgraph "Middleware Layer"
            direction TB
            cors["CORS & JSON Parser"]:::middleware
            authMw["JWT Auth Middleware"]:::middleware
            errorMw["Error Handling Middleware"]:::middleware
        end

        subgraph "Controller Layer"
            direction TB
            actCtrl["Activity Controller"]:::server
            authCtrl["Auth Controller"]:::server
            bookCtrl["Booking Controller"]:::server
            healthCtrl["Healthcheck Controller"]:::server
        end

        subgraph "Utility Layer"
            direction TB
            apiErr["ApiError"]:::utility
            apiRes["ApiResponse"]:::utility
            asyncH["asyncHandler"]:::utility
        end

        conn["MongoDB Connection\n(src/db/index.js)"]:::db

        subgraph "Model Layer"
            direction TB
            userModel["User Model"]:::db
            activityModel["Activity Model"]:::db
            bookingModel["Booking Model"]:::db
        end
    end

    dbSrv["MongoDB Cluster"]:::db

    %% Data Flow
    client -->|HTTP Request| lb
    lb --> index
    index --> app
    app --> actRoutes
    app --> authRoutes
    app --> bookRoutes
    app --> healthRoutes

    actRoutes --> cors
    authRoutes --> cors
    bookRoutes --> cors
    healthRoutes --> cors

    cors --> authMw
    authMw --> actCtrl
    authMw --> bookCtrl

    actRoutes --> actCtrl
    authRoutes --> authCtrl
    bookRoutes --> bookCtrl
    healthRoutes --> healthCtrl

    actCtrl --> asyncH
    authCtrl --> asyncH
    bookCtrl --> asyncH
    healthCtrl --> asyncH

    actCtrl --> activityModel
    authCtrl --> userModel
    bookCtrl --> bookingModel
    healthCtrl --> conn

    activityModel --> conn
    userModel --> conn
    bookingModel --> conn
    conn --> dbSrv

    actCtrl --> apiRes
    authCtrl --> apiRes
    bookCtrl --> apiRes
    healthCtrl --> apiRes

    apiErr --> errorMw
    errorMw --> app
    app -->|Error Response| client

    %% Click Events
    click index "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/index.js"
    click app "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/app.js"
    click actRoutes "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/routes/activity.routes.js"
    click authRoutes "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/routes/auth.routes.js"
    click bookRoutes "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/routes/booking.routes.js"
    click healthRoutes "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/routes/healthcheck.routes.js"
    click authMw "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/middlewares/auth.middleware.js"
    click actCtrl "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/controllers/activity.controller.js"
    click authCtrl "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/controllers/auth.controller.js"
    click bookCtrl "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/controllers/booking.controller.js"
    click healthCtrl "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/controllers/healthcheck.controller.js"
    click apiErr "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/utils/ApiError.js"
    click apiRes "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/utils/ApiResponse.js"
    click asyncH "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/utils/asyncHandler.js"
    click conn "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/db/index.js"
    click userModel "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/models/user.model.js"
    click activityModel "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/models/activity.model.js"
    click bookingModel "https://github.com/nikhil-gna/activity-booking-api/blob/main/src/models/booking.model.js"
    click readme "https://github.com/nikhil-gna/activity-booking-api/blob/main/README.md"
    click env "https://github.com/nikhil-gna/activity-booking-api/blob/main/.env.sample"
    click pkg "https://github.com/nikhil-gna/activity-booking-api/blob/main/package.json"

    %% Legend
    subgraph Legend
        direction TB
        ex["External"]:::external
        srv["Server"]:::server
        mid["Middleware"]:::middleware
        uti["Utility"]:::utility
        database["Database"]:::db
    end

    %% Styles
    classDef external fill:#ECEFF1,stroke:#546E7A,stroke-width:1px
    classDef server fill:#BBDEFB,stroke:#1E88E5,stroke-width:1px
    classDef middleware fill:#FFE0B2,stroke:#FB8C00,stroke-width:1px
    classDef utility fill:#C8E6C9,stroke:#43A047,stroke-width:1px
    classDef db fill:#D7CCC8,stroke:#6D4C41,stroke-width:1px
```
