# aryan-upa/pg-finder-v2 architecture

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

- Interactive diagram: https://gitdiagram.com/aryan-upa/pg-finder-v2
- Repository: https://github.com/aryan-upa/pg-finder-v2

## Overview

##### Project Type & Purpose
- PG Finder is a full-stack web application built using Node.js, Express, and MongoDB.
- Its primary purpose is to help users (riders) find and book PGs/hostels while also allowing providers to add and manage properties, with an admin module overseeing the overall operations.
- The README clearly indicates that the application is modular with three primary roles: Rider, Provider, and Admin.

##### Main Components Identification
a. Frontend / Presentation Layer:
- “views” directory: EJS templates for various pages (home, login, dashboards, property details), including separate layouts and partials.
- “public” directory: Static files including CSS, JavaScript, images, and site-data files.

b. Routing and Controllers:
- “routes” directory: Contains route files like admin.js, auth.js, booking.js, contact.js, property.js, provider.js, review.js, rider.js. These files handle endpoint definitions and traffic routing.

c. Middleware:
- “middlewares” directory: Includes common middleware functions for features such as file uploading, role validation, schema validation, and others.

d. Data Models:
- “models” directory: Defines the data structure for different entities such as booking, contact, login, property, provider, register, review, and rider.

e. Configuration & Application Bootstrapping:
- Files like app.js and config.js initialize the application and setup configurations.

f. Utilities & Helpers:
- “utils” directory: Contains modules for internal processes (cron jobs, database connection, key generation, mail sending, validation schemas, and more).

g. Data static file:
- “data” directory: May contain JSON data such as top_property.json, likely used for initial data seeding or reference.

##### Relationships and Interactions
- The central Express app (app.js) is configured in conjunction with config.js and loads the routes. Incoming HTTP requests route to controllers present in the “routes” directory.
- Each route may use one or more middleware functions (from the “middlewares” folder) to perform validations or handle file uploads.
- Data interaction is abstracted through the defined models in “models” which interact with the underlying database (MongoDB).
- The “views” render the dynamic HTML pages using data passed from the controllers.
- Utility modules perform supplementary functions (e.g., sending emails on booking success, running cron jobs for background tasks, handling database connections).

##### Architectural Patterns and Design Principles
- MVC (Model-View-Controller) Pattern: Clearly visible with models defining data, routes and controllers managing the business logic, and views handling the presentation layer.
- Layered Architecture: Separation of concerns exists across static assets (public), server-side logic (app.js, routes, middleware, and models), and utility functions.
- Modularization by Roles: The application is divided into Rider, Provider, and Admin modules, each with specific routes and associated views.
- Use of middleware for separation of common concerns such as validation, file uploads, and role checking.

##### Final Implementation Advice
- Drill down into the server by splitting it into logical directories (Routes, Middlewares, Models, Utilities) and then further detail the role-specific modules.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Client
    Browser["Browser"]:::client

    %% Frontend Layer
    subgraph "Frontend Layer"
        Views["EJS Views"]:::frontend
        Public["Static Assets"]:::frontend
    end

    %% Backend (Express)
    subgraph "Backend (Express)"
        %% Bootstrapping and Configuration
        subgraph "Bootstrapping"
            AppJs["app.js"]:::backend
            Config["config.js"]:::backend
            AppJs --> Config
        end

        %% Detailed Routes
        subgraph "Routes"
            AdminRoute["admin.js"]:::backend
            AuthRoute["auth.js"]:::backend
            BookingRoute["booking.js"]:::backend
            PropertyRoute["property.js"]:::backend
            ProviderRoute["provider.js"]:::backend
            RiderRoute["rider.js"]:::backend
            ContactRoute["contact.js"]:::backend
            ReviewRoute["review.js"]:::backend
        end

        %% Detailed Middlewares
        subgraph "Middlewares"
            Common["common.js"]:::backend
            FileUploader["file_uploader.js"]:::backend
            RoleValidator["role_validator.js"]:::backend
            SchemaValidator["schema_validator.js"]:::backend
        end

        %% Detailed Models
        subgraph "Models"
            BookingModel["booking.js"]:::backend
            ContactModel["contact.js"]:::backend
            KeyModel["key.js"]:::backend
            LoginModel["login.js"]:::backend
            PropertyModel["property.js"]:::backend
            ProviderModel["provider.js"]:::backend
            RegisterModel["register.js"]:::backend
            ReviewModel["review.js"]:::backend
            RiderModel["rider.js"]:::backend
        end

        %% Detailed Utilities
        subgraph "Utilities"
            CronJobs["cron_jobs.js"]:::backend
            DatabaseConnect["database_connect.js"]:::backend
            KeyGenerator["key_generator.js"]:::backend
            MailSender["mail_sender.js"]:::backend
            ValidationSchemas["validation_schemas.js"]:::backend
        end

        %% Data for Seeding/Reference
        subgraph "Data"
            TopProperty["top_property.json"]:::backend
        end
    end

    %% Database
    subgraph "Database Layer"
        MongoDB["MongoDB"]:::database
    end

    %% Aggregated Flow Nodes
    RoutesAgg["[Routes Aggregate]"]:::backend
    MiddlewaresAgg["[Middlewares Aggregate]"]:::backend
    ModelsAgg["[Models Aggregate]"]:::backend
    UtilitiesAgg["[Utilities Aggregate]"]:::backend

    %% Flows between layers
    Browser -->|"HTTP Request"| AppJs
    AppJs -->|"Initializes"| RoutesAgg
    RoutesAgg -->|"Uses"| MiddlewaresAgg
    MiddlewaresAgg -->|"Processes"| ModelsAgg
    ModelsAgg -->|"DB Queries"| MongoDB

    AppJs -->|"Renders"| Views
    AppJs -->|"Serves"| Public
    AppJs -->|"Triggers"| UtilitiesAgg
    UtilitiesAgg -->|"Assists"| ModelsAgg
    UtilitiesAgg -->|"Reads"| TopProperty

    %% Linking detailed clusters to aggregates
    %% (These invisible links conceptually group the detailed nodes)
    AdminRoute --- RoutesAgg
    AuthRoute --- RoutesAgg
    BookingRoute --- RoutesAgg
    PropertyRoute --- RoutesAgg
    ProviderRoute --- RoutesAgg
    RiderRoute --- RoutesAgg
    ContactRoute --- RoutesAgg
    ReviewRoute --- RoutesAgg

    Common --- MiddlewaresAgg
    FileUploader --- MiddlewaresAgg
    RoleValidator --- MiddlewaresAgg
    SchemaValidator --- MiddlewaresAgg

    BookingModel --- ModelsAgg
    ContactModel --- ModelsAgg
    KeyModel --- ModelsAgg
    LoginModel --- ModelsAgg
    PropertyModel --- ModelsAgg
    ProviderModel --- ModelsAgg
    RegisterModel --- ModelsAgg
    ReviewModel --- ModelsAgg
    RiderModel --- ModelsAgg

    CronJobs --- UtilitiesAgg
    DatabaseConnect --- UtilitiesAgg
    KeyGenerator --- UtilitiesAgg
    MailSender --- UtilitiesAgg
    ValidationSchemas --- UtilitiesAgg

    %% Click Events for Component Mapping
    click Views "https://github.com/aryan-upa/pg-finder-v2/tree/master/views"
    click Public "https://github.com/aryan-upa/pg-finder-v2/tree/master/public"
    click AdminRoute "https://github.com/aryan-upa/pg-finder-v2/blob/master/routes/admin.js"
    click AuthRoute "https://github.com/aryan-upa/pg-finder-v2/blob/master/routes/auth.js"
    click BookingRoute "https://github.com/aryan-upa/pg-finder-v2/blob/master/routes/booking.js"
    click PropertyRoute "https://github.com/aryan-upa/pg-finder-v2/blob/master/routes/property.js"
    click ProviderRoute "https://github.com/aryan-upa/pg-finder-v2/blob/master/routes/provider.js"
    click RiderRoute "https://github.com/aryan-upa/pg-finder-v2/blob/master/routes/rider.js"
    click ContactRoute "https://github.com/aryan-upa/pg-finder-v2/blob/master/routes/contact.js"
    click ReviewRoute "https://github.com/aryan-upa/pg-finder-v2/blob/master/routes/review.js"
    click Common "https://github.com/aryan-upa/pg-finder-v2/blob/master/middlewares/common.js"
    click FileUploader "https://github.com/aryan-upa/pg-finder-v2/blob/master/middlewares/file_uploader.js"
    click RoleValidator "https://github.com/aryan-upa/pg-finder-v2/blob/master/middlewares/role_validator.js"
    click SchemaValidator "https://github.com/aryan-upa/pg-finder-v2/blob/master/middlewares/schema_validator.js"
    click BookingModel "https://github.com/aryan-upa/pg-finder-v2/blob/master/models/booking.js"
    click ContactModel "https://github.com/aryan-upa/pg-finder-v2/blob/master/models/contact.js"
    click KeyModel "https://github.com/aryan-upa/pg-finder-v2/blob/master/models/key.js"
    click LoginModel "https://github.com/aryan-upa/pg-finder-v2/blob/master/models/login.js"
    click PropertyModel "https://github.com/aryan-upa/pg-finder-v2/blob/master/models/property.js"
    click ProviderModel "https://github.com/aryan-upa/pg-finder-v2/blob/master/models/provider.js"
    click RegisterModel "https://github.com/aryan-upa/pg-finder-v2/blob/master/models/register.js"
    click ReviewModel "https://github.com/aryan-upa/pg-finder-v2/blob/master/models/review.js"
    click RiderModel "https://github.com/aryan-upa/pg-finder-v2/blob/master/models/rider.js"
    click AppJs "https://github.com/aryan-upa/pg-finder-v2/blob/master/app.js"
    click Config "https://github.com/aryan-upa/pg-finder-v2/blob/master/config.js"
    click CronJobs "https://github.com/aryan-upa/pg-finder-v2/blob/master/utils/cron_jobs.js"
    click DatabaseConnect "https://github.com/aryan-upa/pg-finder-v2/blob/master/utils/database_connect.js"
    click KeyGenerator "https://github.com/aryan-upa/pg-finder-v2/blob/master/utils/key_generator.js"
    click MailSender "https://github.com/aryan-upa/pg-finder-v2/blob/master/utils/mail_sender.js"
    click ValidationSchemas "https://github.com/aryan-upa/pg-finder-v2/blob/master/utils/validation_schemas.js"
    click TopProperty "https://github.com/aryan-upa/pg-finder-v2/blob/master/data/top_property.json"

    %% Styles
    classDef client fill:#ffe0b3,stroke:#e67300,stroke-width:2px;
    classDef frontend fill:#cce5ff,stroke:#3399ff,stroke-width:2px;
    classDef backend fill:#d1e7dd,stroke:#0f5132,stroke-width:2px;
    classDef database fill:#f8d7da,stroke:#842029,stroke-width:2px;
```
