# d7omdev/detours architecture

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

- Interactive diagram: https://gitdiagram.com/d7omdev/detours
- Repository: https://github.com/d7omdev/detours

## Overview

##### Main System Components

a) Client Layer:
- Public-facing frontend (indicated by /public directory)
- Static assets (CSS, images)
- Template engine (Pug templates in dev-data/templates)

b) Application Layer:
- Express.js server (app.js and server.js)
- Authentication system (authController.js)
- Route handlers (routes directory)
- Controllers (controllers directory)
- Business logic (models directory)

c) Data Layer:
- MongoDB database
- Data models (User, Tour, Review)

##### Component Relationships

a) External Interfaces:
- Client requests → Express server
- Authentication flow (JWT tokens)
- API endpoints for tours, users, and reviews

b) Internal Flow:
- Routes → Controllers → Models → Database
- Error handling middleware
- Authentication middleware

##### Specific Architecture Elements

Create these distinct sections:

a) API Layer:
- RESTful endpoints (/api/v1/...)
- Route handlers for tours, users, and reviews
- Authentication endpoints

b) Business Logic Layer:
- Factory pattern (handlerFactory.js)
- Tour management
- User management
- Review system

c) Database Layer:
- MongoDB collections
- Relationships between models
- Data flow

##### Key Features to Highlight

Architectural patterns:

- MVC pattern implementation
- Middleware chain
- Error handling flow
- Authentication flow
- API features (pagination, filtering, sorting)

##### Additional Elements

Supporting components:

- Environment configuration
- Static file serving
- Email service integration
- API documentation endpoints
- Development tools and scripts

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        Browser["Web Browser"]
        Templates["Templates"]:::frontend
        StaticAssets["Static Assets"]:::frontend
    end

    subgraph Server
        Express["Express Server"]:::server
        click Express "https://github.com/d7omdev/detours/blob/master/app.js"

        subgraph Auth["Authentication Layer"]
            AuthController["Auth Controller"]:::auth
            click AuthController "https://github.com/d7omdev/detours/blob/master/controllers/authController.js"
            JWT["JWT Service"]:::auth
        end

        subgraph Routes["Route Layer"]
            TourRoutes["Tour Routes"]:::route
            UserRoutes["User Routes"]:::route
            ReviewRoutes["Review Routes"]:::route
            click TourRoutes "https://github.com/d7omdev/detours/blob/master/routes/tourRoutes.js"
            click UserRoutes "https://github.com/d7omdev/detours/blob/master/routes/userRoutes.js"
            click ReviewRoutes "https://github.com/d7omdev/detours/blob/master/routes/reviewRouts.js"
        end

        subgraph Controllers["Controller Layer"]
            HandlerFactory["Handler Factory"]:::controller
            TourController["Tour Controller"]:::controller
            UserController["User Controller"]:::controller
            ReviewController["Review Controller"]:::controller
            ErrorController["Error Controller"]:::error
            click HandlerFactory "https://github.com/d7omdev/detours/blob/master/controllers/handlerFactory.js"
            click TourController "https://github.com/d7omdev/detours/blob/master/controllers/tourController.js"
            click UserController "https://github.com/d7omdev/detours/blob/master/controllers/userController.js"
            click ReviewController "https://github.com/d7omdev/detours/blob/master/controllers/reviewController.js"
            click ErrorController "https://github.com/d7omdev/detours/blob/master/controllers/errorController.js"
        end

        subgraph Utils["Utility Layer"]
            ApiFeatures["API Features"]:::util
            CatchAsync["Catch Async"]:::util
            AppError["App Error"]:::util
            EmailService["Email Service"]:::util
            click ApiFeatures "https://github.com/d7omdev/detours/blob/master/utils/apiFeatures.js"
            click CatchAsync "https://github.com/d7omdev/detours/blob/master/utils/catchAsync.js"
            click AppError "https://github.com/d7omdev/detours/blob/master/utils/appError.js"
            click EmailService "https://github.com/d7omdev/detours/blob/master/utils/email.js"
        end
    end

    subgraph Data["Data Layer"]
        subgraph Models["Data Models"]
            TourModel["Tour Model"]:::model
            UserModel["User Model"]:::model
            ReviewModel["Review Model"]:::model
            click TourModel "https://github.com/d7omdev/detours/blob/master/models/tourModel.js"
            click UserModel "https://github.com/d7omdev/detours/blob/master/models/userModel.js"
            click ReviewModel "https://github.com/d7omdev/detours/blob/master/models/reviewModel.js"
        end

        MongoDB[(MongoDB)]:::database
        DevData["Development Data"]:::data
        click DevData "https://github.com/d7omdev/detours/tree/master/dev-data/data"
    end

    subgraph Deploy["Deployment"]
        Docker["Docker Container"]:::deploy
        EnvVars["Environment Variables"]:::deploy
        click Docker "https://github.com/d7omdev/detours/tree/master/Dockerfile"
        click EnvVars "https://github.com/d7omdev/detours/blob/master/.env_Variables.txt"
    end

    %% Client Layer Connections
    Browser --> Express
    Express --> Templates
    Express --> StaticAssets
    click Templates "https://github.com/d7omdev/detours/tree/master/dev-data/templates"
    click StaticAssets "https://github.com/d7omdev/detours/tree/master/public"

    %% Server Layer Connections
    Express --> Routes
    Express --> AuthController
    Routes --> Controllers
    Controllers --> Models
    Controllers --> Utils
    AuthController --> JWT
    ErrorController --> AppError

    %% Data Layer Connections
    Models --> MongoDB
    DevData --> MongoDB

    %% Styling
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef server fill:#2ecc71,stroke:#27ae60,color:white
    classDef auth fill:#f1c40f,stroke:#f39c12,color:black
    classDef route fill:#9b59b6,stroke:#8e44ad,color:white
    classDef controller fill:#1abc9c,stroke:#16a085,color:white
    classDef model fill:#34495e,stroke:#2c3e50,color:white
    classDef util fill:#e67e22,stroke:#d35400,color:white
    classDef database fill:#95a5a6,stroke:#7f8c8d,color:white
    classDef error fill:#e74c3c,stroke:#c0392b,color:white
    classDef data fill:#bdc3c7,stroke:#95a5a6,color:black
    classDef deploy fill:#2c3e50,stroke:#34495e,color:white
```
