# andmiron/my-tour architecture

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

- Interactive diagram: https://gitdiagram.com/andmiron/my-tour
- Repository: https://github.com/andmiron/my-tour

## Overview

##### Project Type and Purpose
- This is an Express-based MVC web application designed as a pet project to showcase full-stack development skills.
- Its purpose is to provide a tour management platform with features such as user authentication, booking tours, posting reviews, and handling payments (via Stripe).
- The application clearly separates logic into distinct components (e.g., auth, bookings, tours, reviews, users) and uses a modular design with controllers, routers, models, and validators.

##### Main Components
a. Client Browser:
- Represents users interacting with the application through a web client.
- Uses Pug templates rendered on the server, with some client-side JavaScript under the public/js folder.

b. Express Application Server:
- Entry Points: The main server file (app.js) and starting script (bin/www).
- Routing Layer: Clearly separated into various components (auth, bookings, tours, reviews, users, views), each having its own router file.
- Controllers: Manage the business logic associated with each component.
- Models: Represent the data schema (using Mongoose) for users, tours, bookings, and reviews.
- Validators: Ensure data integrity before processing.
- Middlewares: Global error handler and authentication checks (e.g., errorHandler.js and isAuthenticated.js).

c. Configuration and Services:
- Config: Contains configuration files (session.options.js, config.js) which determine application settings.
- Services: A folder that abstracts integrations with external systems such as:
- MongoDB (mongo.js) for database connectivity.
- Stripe (stripe.js) for payment processing.
- AWS S3 client (clientS3.js) for file storage.
- Email and other utilities (email.js, morgan.js, multer.js, passport.js).

d. Database:
- Although not directly visible, the use of Mongoose indicates a MongoDB database.
- Database as an external component linked to the models in the server.

e. External and Third-Party Services:
- Stripe as the external payment service.
- Email service, file storage service (Amazon S3), and any external authentication mechanisms (Passport) as distinct external components.

##### Relationships and Interactions
- From Client Browser → Express Server: HTTP requests are made by the client, which are routed to specific routers.
- Express Server Routes → Controllers: Routers delegate requests to controllers where business logic is executed.
- Controllers ↔ Models: Controllers interact with the models to perform database operations (create, read, update, delete).
- Controllers → Views: Upon processing data, controllers render Pug templates or send JSON responses.
- Controllers → Services: For external interactions (e.g., handling payments via Stripe, sending emails, file uploads).
- Middlewares: Positioned in the flow to intercept and process requests for error handling and authentication.
- Database: Connects to the Express server via the Mongoose layer.

##### Architectural Patterns and Design Principles
- MVC (Model-View-Controller): Clearly visible by the segregation of controllers, models, and views (Pug templates).
- Modular Organization: Components are organized by domain (auth, bookings, tours, reviews, users) each with its own controllers, routers, models, and validators.
- Service Abstraction: External interactions are abstracted in the services folder, which isolates concerns such as payment processing, file management, logging, and database connectivity.
- Clear Separation of Concerns: Config, middleware, and routes are defined in separate directories/files to ensure maintainability.

- A subsection for routing and controllers organized by component (auth, tours, bookings, reviews, users, views).
- A middleware layer that is applied to all or selected routes.
- A service layer that abstracts interactions to external systems.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        CB["Client Browser<br/>(Pug Templates & Client-side JS)"]:::client
    end

    %% Express Application Server
    subgraph "Express Application Server"
        %% Entry Points
        subgraph "Entry Points"
            EP1["app.js"]:::entry
            EP2["bin/www"]:::entry
        end

        %% Routing & Controllers
        subgraph "Routing & Controllers"
            subgraph "Auth Component"
                A_R["Auth Router"]:::router
                A_C["Auth Controller"]:::controller
            end
            subgraph "Tours Component"
                T_R["Tours Router"]:::router
                T_C["Tours Controller"]:::controller
            end
            subgraph "Bookings Component"
                B_R["Bookings Router"]:::router
                B_C["Bookings Controller"]:::controller
            end
            subgraph "Reviews Component"
                R_R["Reviews Router"]:::router
                R_C["Reviews Controller"]:::controller
            end
            subgraph "Users Component"
                U_R["Users Router"]:::router
                U_C["Users Controller"]:::controller
            end
            subgraph "Views Component"
                V_R["Views Router"]:::router
                V_C["Views Controller"]:::controller
            end
        end

        %% Models (Mongoose Schemas)
        subgraph "Models"
            TM["Tours Model"]:::model
            BM["Bookings Model"]:::model
            RM["Reviews Model"]:::model
            UM["Users Model"]:::model
        end

        %% Validators
        subgraph "Validators"
            A_V["Auth Validator"]:::validator
            T_V["Tours Validator"]:::validator
            B_V["Bookings Validator"]:::validator
            R_V["Reviews Validator"]:::validator
            U_V["Users Validator"]:::validator
        end

        %% Middlewares
        subgraph "Middlewares"
            EH["Global Error Handler"]:::middleware
            AT["Authentication Check"]:::middleware
        end

        %% Configuration
        subgraph "Configuration"
            CFG["App Configuration"]:::config
            SO["Session Options"]:::config
        end

        %% Services (External Integrations)
        subgraph "Services"
            MONGO["MongoDB Service"]:::service
            STRIPE["Stripe Payment Processing"]:::service
            S3["AWS S3 Integration"]:::service
            EMAIL["Email Utilities"]:::service
            LOG["Logging Service"]:::service
            MULTER["File Upload Handling"]:::service
            PASSPORT["Passport for Authentication"]:::service
        end
    end

    %% Database and External Services
    subgraph "Database"
        DB["MongoDB"]:::database
    end

    subgraph "External Services"
        EXT_STRIPE["Stripe Payment Service"]:::external
        EXT_AWS["Amazon S3"]:::external
        EXT_EMAIL["Email Service"]:::external
        EXT_PASSPORT["Passport Auth Service"]:::external
    end

    %% Connections from Client to Express
    CB -->|"HTTP Request"| EP1
    CB -->|"HTTP Request"| EP2
    EP1 -->|"Routes to"| A_R
    EP1 -->|"Routes to"| T_R
    EP1 -->|"Routes to"| B_R
    EP1 -->|"Routes to"| R_R
    EP1 -->|"Routes to"| U_R
    EP1 -->|"Routes to"| V_R

    %% Routers delegate to Controllers
    A_R -->|"Delegates"| A_C
    T_R -->|"Delegates"| T_C
    B_R -->|"Delegates"| B_C
    R_R -->|"Delegates"| R_C
    U_R -->|"Delegates"| U_C
    V_R -->|"Delegates"| V_C

    %% Routers use Validators
    A_R -->|"Validates"| A_V
    T_R -->|"Validates"| T_V
    B_R -->|"Validates"| B_V
    R_R -->|"Validates"| R_V
    U_R -->|"Validates"| U_V

    %% Controllers interact with Models
    A_C -->|"Uses"| UM
    T_C -->|"Uses"| TM
    B_C -->|"Uses"| BM
    R_C -->|"Uses"| RM
    U_C -->|"Uses"| UM

    %% Models connect to Database
    TM -->|"Stores Data"| DB
    BM -->|"Stores Data"| DB
    RM -->|"Stores Data"| DB
    UM -->|"Stores Data"| DB

    %% Controllers call Services for external operations
    A_C -->|"Calls"| STRIPE
    T_C -->|"Calls"| MONGO
    B_C -->|"Calls"| MONGO
    R_C -->|"Calls"| MONGO
    U_C -->|"Calls"| MONGO
    V_C -->|"Calls"| EMAIL

    %% Services interact with External Services
    STRIPE -->|"Processes Payment"| EXT_STRIPE
    S3 -->|"Handles Storage"| EXT_AWS
    EMAIL -->|"Sends Emails"| EXT_EMAIL
    PASSPORT -->|"Authenticates"| EXT_PASSPORT

    %% Middlewares intercept requests (abstract representation)
    EP1 -->|"Middleware"| EH
    EP1 -->|"Middleware"| AT

    %% Configuration is utilized by the Application
    EP1 -->|"Configures"| CFG
    EP1 -->|"Configures"| SO

    %% Styling Classes
    classDef client fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef entry fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef router fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef controller fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef model fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef validator fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef middleware fill:#fefefe,stroke:#6c757d,stroke-width:2px;
    classDef config fill:#f0e68c,stroke:#b8860b,stroke-width:2px;
    classDef service fill:#d6d8d9,stroke:#343a40,stroke-width:2px;
    classDef database fill:#c3e6cb,stroke:#155724,stroke-width:2px;
    classDef external fill:#f5c6cb,stroke:#721c24,stroke-width:2px;

    %% Click Events for Client Layer
    click CB "https://github.com/andmiron/my-tour/tree/main/public/js"

    %% Click Events for Express Application Entry Points
    click EP1 "https://github.com/andmiron/my-tour/blob/main/app.js"
    click EP2 "https://github.com/andmiron/my-tour/tree/main/bin/www"

    %% Click Events for Auth Component
    click A_R "https://github.com/andmiron/my-tour/blob/main/components/auth/auth.router.js"
    click A_C "https://github.com/andmiron/my-tour/blob/main/components/auth/auth.controller.js"
    click A_V "https://github.com/andmiron/my-tour/blob/main/components/auth/auth.validator.js"

    %% Click Events for Tours Component
    click T_R "https://github.com/andmiron/my-tour/blob/main/components/tours/tours.router.js"
    click T_C "https://github.com/andmiron/my-tour/blob/main/components/tours/tours.controller.js"
    click T_V "https://github.com/andmiron/my-tour/blob/main/components/tours/tours.validator.js"
    click TM "https://github.com/andmiron/my-tour/blob/main/components/tours/tours.model.js"

    %% Click Events for Bookings Component
    click B_R "https://github.com/andmiron/my-tour/blob/main/components/bookings/bookings.router.js"
    click B_C "https://github.com/andmiron/my-tour/blob/main/components/bookings/bookings.controller.js"
    click B_V "https://github.com/andmiron/my-tour/blob/main/components/bookings/bookings.validator.js"
    click BM "https://github.com/andmiron/my-tour/blob/main/components/bookings/bookings.model.js"

    %% Click Events for Reviews Component
    click R_R "https://github.com/andmiron/my-tour/blob/main/components/reviews/reviews.router.js"
    click R_C "https://github.com/andmiron/my-tour/blob/main/components/reviews/reviews.controller.js"
    click R_V "https://github.com/andmiron/my-tour/blob/main/components/reviews/reviews.validator.js"
    click RM "https://github.com/andmiron/my-tour/blob/main/components/reviews/reviews.model.js"

    %% Click Events for Users Component
    click U_R "https://github.com/andmiron/my-tour/blob/main/components/users/users.router.js"
    click U_C "https://github.com/andmiron/my-tour/blob/main/components/users/users.controller.js"
    click U_V "https://github.com/andmiron/my-tour/blob/main/components/users/users.validator.js"
    click UM "https://github.com/andmiron/my-tour/blob/main/components/users/users.model.js"

    %% Click Events for Views Component
    click V_R "https://github.com/andmiron/my-tour/blob/main/components/views/views.router.js"
    click V_C "https://github.com/andmiron/my-tour/blob/main/components/views/views.controller.js"

    %% Click Events for Middlewares
    click EH "https://github.com/andmiron/my-tour/blob/main/middlewares/errorHandler.js"
    click AT "https://github.com/andmiron/my-tour/blob/main/middlewares/isAuthenticated.js"

    %% Click Events for Configuration
    click CFG "https://github.com/andmiron/my-tour/blob/main/config/config.js"
    click SO "https://github.com/andmiron/my-tour/blob/main/config/session.options.js"

    %% Click Events for Services
    click MONGO "https://github.com/andmiron/my-tour/blob/main/services/mongo.js"
    click STRIPE "https://github.com/andmiron/my-tour/blob/main/services/stripe.js"
    click S3 "https://github.com/andmiron/my-tour/blob/main/services/clientS3.js"
    click EMAIL "https://github.com/andmiron/my-tour/blob/main/services/email.js"
    click LOG "https://github.com/andmiron/my-tour/blob/main/services/morgan.js"
    click MULTER "https://github.com/andmiron/my-tour/blob/main/services/multer.js"
    click PASSPORT "https://github.com/andmiron/my-tour/blob/main/services/passport.js"

    %% Click Events for Client Templates (Views)
    click CB "https://github.com/andmiron/my-tour/tree/main/views"
```
