# hungtran3011/ecombackend architecture

> Architecture diagram of the GitHub repository hungtran3011/ecombackend, 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/hungtran3011/ecombackend
- Repository: https://github.com/hungtran3011/ecombackend

## Overview

##### Main Components (Classes) and Their Groupings
- Entry Point & Core Infrastructure:
- Main Application (e.g., class or module represented by ‘src/index.js’) that instantiates and wires up the Express server.
- Common Configuration classes (located in src/common/config), which include CORS, rate limiting, etc.
- Middlewares (e.g., admin.middleware.js, cache.middleware.js, csrf.middleware.js, security.middleware.js) that are applied at various request stages.
- Common Services (e.g., cloudinary.service.js, mail.service.js, otp.service.js, redis.service.js, token.service.js) that provide shared business logic.
- Validators from src/common/validators (e.g., otp.validator.js, password.validator.js, etc.)

- Domain-Specific Modules:
Each module in src/modules should be represented as a grouping of classes, for example:

- Auth Module:
- AuthController and AdminController (the controllers handling authentication and administrative actions).
- AuthService (managing authentication logic).
- AuthRoute (the route definition for auth endpoints).

- User Module:
- UserController (handles user-based requests).
- UserService (business logic for user management).
- UserMiddleware (if applicable, showing pre-route processing).
- UserRoute and UserSchema (for API route definitions and data validation/modeling).

- Product Module:
- ProductController (managing product-related requests).
- ProductService (handling product business logic).
- ProductRoute and ProductSchema for endpoint mapping and data validation.

- Similar blocks for Cart, Order, Search, Storage, and Upload modules with each containing the respective Controller, Route, Schema (if applicable), and Service classes.

- API Routing:
- Central routing logic found in src/api/routes.js.
- Additionally, include any standalone admin routes (from src/routes/admin.route.js).

##### Relationships and Dependencies
- Associations:
- Controllers depend on the corresponding Services. For instance, AuthController calls methods in AuthService.
- Routes are a composition of controllers (each route class method calls a controller handler).
- Middleware is attached to both application-wide routes (via src/index.js) and module-specific endpoints.
- Validators might be called from within Controllers or at the route level for request pre-validation.

- Aggregation and Inheritance:

##### Document Architectural Patterns
- Express MVC-like Separation:
- Controllers, routes, schemas, and services are clearly separated into individual classes.
- Modular Design:
- Each module (auth, user, product, etc.) is self-contained, making it easier to see how each component interacts within the larger system.
- Integration Points:
- Modules to the main Express application from src/index.js to emphasize centralized request handling.

b. Create separate sections for Common Components and Domain-Specific Modules:
- For each module (Auth, User, Product, etc.), detail the following:
- Controller class(es)
- Service class(es)
- Route definitions (which might be represented as a composite class or a container)
- Schema and optionally Validator if defined within the module.
d. Link out any Middleware components that are applied across the system.
e. Configuration and common services as centralized components to which the module classes have dependencies.
f. Adding notes on which classes interact with external libraries or services (like Cloudinary in cloudinary.service.js, or Mail in mail.service.js).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Point & Core Infrastructure
    subgraph "Entry Point & Core Infrastructure"
        A["Main Application"]:::main
        B["API Routes"]:::main
        C["Admin Routes"]:::main
    end

    %% Common Configuration
    subgraph "Common Configuration"
        CCORS["CORS Configuration"]:::config
        CRL["Rate Limiting"]:::config
    end

    %% Common Middlewares
    subgraph "Common Middlewares"
        MAdmin["Admin Middleware"]:::middleware
        MCache["Cache Middleware"]:::middleware
        MCSRF["CSRF Middleware"]:::middleware
        MSecurity["Security Middleware"]:::middleware
    end

    %% Common Services
    subgraph "Common Services"
        SCloud["Cloudinary Service"]:::service
        SMail["Mail Service"]:::service
        SOTP["OTP Service"]:::service
        SRedis["Redis Service"]:::service
        SToken["Token Service"]:::service
    end

    %% Common Validators
    subgraph "Common Validators"
        VOTP["OTP Validator"]:::validator
        VPassword["Password Validator"]:::validator
        VProduct["Product Validator"]:::validator
        VUser["User Validator"]:::validator
    end

    %% Auth Module
    subgraph "Auth Module"
        AC["Auth Controller"]:::controller
        ADC["Admin Controller"]:::controller
        AS["Auth Service"]:::service
        AR["Auth Routes"]:::route
    end

    %% User Module
    subgraph "User Module"
        UC["User Controller"]:::controller
        US["User Service"]:::service
        UM["User Middleware"]:::middleware
        USch["User Schema"]:::schema
        UR["User Routes"]:::route
    end

    %% Product Module
    subgraph "Product Module"
        PC["Product Controller"]:::controller
        PS["Product Service"]:::service
        PSch["Product Schema"]:::schema
        PR["Product Routes"]:::route
    end

    %% Cart Module
    subgraph "Cart Module"
        CCt["Cart Controller"]:::controller
        CSch["Cart Schema"]:::schema
        CRt["Cart Routes"]:::route
    end

    %% Order Module
    subgraph "Order Module"
        OC["Order Controller"]:::controller
        OSch["Order Schema"]:::schema
        ORt["Order Routes"]:::route
    end

    %% Search Module
    subgraph "Search Module"
        SC["Search Controller"]:::controller
        SR["Search Routes"]:::route
    end

    %% Storage Module
    subgraph "Storage Module"
        StrC["Storage Controller"]:::controller
        StrSch["Storage Schema"]:::schema
        StrR["Storage Routes"]:::route
    end

    %% Upload Module
    subgraph "Upload Module"
        UCn["Upload Controller"]:::controller
        URl["Upload Routes"]:::route
    end

    %% Relationships
    %% Main Application uses configuration and middlewares
    A -->|"uses"| CCORS
    A -->|"uses"| CRL
    A -->|"applies"| MAdmin
    A -->|"applies"| MCache
    A -->|"applies"| MCSRF
    A -->|"applies"| MSecurity
    A -->|"mounts"| B
    A -->|"mounts"| C

    %% API Routes to Module Routes
    B -->|"aggregates"| AR
    B -->|"aggregates"| UR
    B -->|"aggregates"| PR
    B -->|"aggregates"| CRt
    B -->|"aggregates"| ORt
    B -->|"aggregates"| SR
    B -->|"aggregates"| StrR
    B -->|"aggregates"| URl

    %% Auth Module Relationships
    AR -->|"calls"| AC
    AR -->|"calls"| ADC
    AC -->|"uses"| AS
    AS -->|"depends on"| SToken

    %% User Module Relationships
    UR -->|"calls"| UC
    UC -->|"uses"| US
    UR -->|"validates with"| USch
    UR -->|"secured by"| UM
    UC -->|"validates with"| VUser

    %% Product Module Relationships
    PR -->|"calls"| PC
    PC -->|"uses"| PS
    PR -->|"validates with"| PSch
    PC -->|"validates with"| VProduct

    %% Cart Module Relationships
    CRt -->|"calls"| CCt
    CRt -->|"validates with"| CSch

    %% Order Module Relationships
    ORt -->|"calls"| OC
    ORt -->|"validates with"| OSch

    %% Search Module Relationships
    SR -->|"calls"| SC

    %% Storage Module Relationships
    StrR -->|"calls"| StrC
    StrR -->|"validates with"| StrSch

    %% Upload Module Relationships
    URl -->|"calls"| UCn

    %% Additional Validation Dependency in Auth Module
    AC -->|"validates with"| VOTP

    %% Additional dependency in User Module
    UC -->|"may use"| VUser

    %% Styles
    classDef main fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef config fill:#FFA500,stroke:#333,stroke-width:2px;
    classDef middleware fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef service fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef validator fill:#FFB6C1,stroke:#333,stroke-width:2px;
    classDef controller fill:#FFFF66,stroke:#333,stroke-width:2px;
    classDef route fill:#D3D3D3,stroke:#333,stroke-width:2px;
    classDef schema fill:#E0FFFF,stroke:#333,stroke-width:2px;

    %% Click Events
    click A "https://github.com/hungtran3011/ecombackend/blob/main/src/index.js"
    click CCORS "https://github.com/hungtran3011/ecombackend/blob/main/src/common/config/cors.config.js"
    click CRL "https://github.com/hungtran3011/ecombackend/blob/main/src/common/config/rate-limit.js"
    click MAdmin "https://github.com/hungtran3011/ecombackend/blob/main/src/common/middlewares/admin.middleware.js"
    click MCache "https://github.com/hungtran3011/ecombackend/blob/main/src/common/middlewares/cache.middleware.js"
    click MCSRF "https://github.com/hungtran3011/ecombackend/blob/main/src/common/middlewares/csrf.middleware.js"
    click MSecurity "https://github.com/hungtran3011/ecombackend/blob/main/src/common/middlewares/security.middleware.js"
    click SCloud "https://github.com/hungtran3011/ecombackend/blob/main/src/common/services/cloudinary.service.js"
    click SMail "https://github.com/hungtran3011/ecombackend/blob/main/src/common/services/mail.service.js"
    click SOTP "https://github.com/hungtran3011/ecombackend/blob/main/src/common/services/otp.service.js"
    click SRedis "https://github.com/hungtran3011/ecombackend/blob/main/src/common/services/redis.service.js"
    click SToken "https://github.com/hungtran3011/ecombackend/blob/main/src/common/services/token.service.js"
    click VOTP "https://github.com/hungtran3011/ecombackend/blob/main/src/common/validators/otp.validator.js"
    click VPassword "https://github.com/hungtran3011/ecombackend/blob/main/src/common/validators/password.validator.js"
    click VProduct "https://github.com/hungtran3011/ecombackend/blob/main/src/common/validators/product.validator.js"
    click VUser "https://github.com/hungtran3011/ecombackend/blob/main/src/common/validators/user.validator.js"
    click B "https://github.com/hungtran3011/ecombackend/blob/main/src/api/routes.js"
    click C "https://github.com/hungtran3011/ecombackend/blob/main/src/routes/admin.route.js"
    click AC "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/auth/auth.controller.js"
    click ADC "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/auth/admin.controller.js"
    click AS "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/auth/auth.service.js"
    click AR "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/auth/auth.route.js"
    click UC "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/user/user.controller.js"
    click US "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/user/user.service.js"
    click UM "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/user/user.middleware.js"
    click USch "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/user/user.schema.js"
    click UR "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/user/user.route.js"
    click PC "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/product/product.controller.js"
    click PS "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/product/product.service.js"
    click PSch "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/product/product.schema.js"
    click PR "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/product/product.route.js"
    click CCt "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/cart/cart.controller.js"
    click CSch "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/cart/cart.schema.js"
    click CRt "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/cart/cart.route.js"
    click OC "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/order/order.controller.js"
    click OSch "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/order/order.schema.js"
    click ORt "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/order/order.route.js"
    click SC "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/search/search.controller.js"
    click SR "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/search/search.route.js"
    click StrC "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/storage/storage.controller.js"
    click StrSch "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/storage/storage.schema.js"
    click StrR "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/storage/storage.route.js"
    click UCn "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/upload/upload.controller.js"
    click URl "https://github.com/hungtran3011/ecombackend/blob/main/src/modules/upload/upload.route.js"
```
