# tronghghe172557/ecommerce_backend_typescript architecture

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

- Interactive diagram: https://gitdiagram.com/tronghghe172557/ecommerce_backend_typescript
- Repository: https://github.com/tronghghe172557/ecommerce_backend_typescript

## Overview

##### Project Type and Purpose
- This is a monolithic TypeScript-based backend for an e‑commerce application.
- It exposes RESTful endpoints (see /src/modules/*/router and docs/openapi.yml).
- Core features: user/shop authentication, product management, cart, discounts, inventory, checkout.

##### File Structure
- Top‑level “Ecommerce_Backend” directory contains:
- src/server.ts (entry point)
- src/base (common infrastructure: routing, error handling, logging, DB & Redis helpers, Swagger setup)
- src/modules (domain modules: auth, products, cart, discount, inventory, checkouts)
- docs/openapi.yml & docs/api (API specs & test scripts)
- package.json, .env, tsconfig.json, and build/test config files show a standard Node/TS toolchain.
- Logging lives under logs/, environment variables via .env.example, nodemon.json for dev.

##### Key Architectural Insights from the README
- Layered architecture: routers → controllers → services → models → database/Redis.
- Uses Bruno for API testing, Swagger/OpenAPI for documentation.
- Emphasis on DTOs for input/output validation, custom exceptions for error handling.

##### Main Components & Their Relationships
a. API Gateway / HTTP Server
- Express (or similar) listening on port (src/server.ts).
- Central router (src/base/router/app.router.ts) composes module routers.
b. Routing Layer
- Module-specific routers forward to controllers.
c. Controller Layer
- Orchestrates request → calls Service → returns response/throws exceptions.
d. Service Layer
- Business logic, orchestrates Models & external calls.
e. Model Layer & Data Layer
- ORM/ODM models under src/modules/*/models.
- Database connection via src/base/database.
- Redis cache layer via src/base/redis.
f. Cross‑Cutting Infrastructure
- Common utilities (logger, env, error handlers) under src/base/common.
- Swagger initialization under src/base/swagger.
g. External Systems
- Relational/NoSQL database (configure .env for host, credentials).
- Redis for caching/session.
- Bruno CLI for testing (external tool but not in production path).

##### Architectural Patterns & Principles
- Layered (hexagonal-like) separation: presentation (router/controller) → domain (service/model) → infrastructure (DB/Redis).
- DTOs and centralized exception handling enforce consistency.
- Modular design: each domain (auth, products, cart, etc.) encapsulates its own controllers, services, models.
- Single monolith deployable as one container/process.

- Use clear, consistent labels (e.g., “ProductController”, “CartService”).
- Major protocols (HTTP/REST, Redis protocol, SQL/NoSQL).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    subgraph "Clients"
        ClientWeb["Web/Mobile UI"]:::external
        Bruno["Bruno Test CLI"]:::external
    end

    %% API Server
    subgraph "API Server (Ecommerce_Backend/src)"
        subgraph "Entry & Core"
            Server["server.ts"]:::entry
        end

        subgraph "Routing Layer"
            AppRouter["app.router.ts"]:::router
            AuthRouter["Auth Router"]:::router
            ProdRouter["Product Router"]:::router
            CartRouter["Cart Router"]:::router
            DiscRouter["Discount Router"]:::router
            InvRouter["Inventory Router"]:::router
            CheckRouter["Checkout Router"]:::router
        end

        subgraph "Controller Layer"
            AuthCtrl["AccessController"]:::controller
            ProdCtrl["ProductController"]:::controller
            CartCtrl["CartController"]:::controller
            DiscCtrl["DiscountController"]:::controller
            CheckCtrl["CheckoutController"]:::controller
        end

        subgraph "Service Layer"
            AuthSvc["AuthService"]:::service
            ProdSvc["ProductService"]:::service
            CartSvc["CartService"]:::service
            DiscSvc["DiscountService"]:::service
            CheckSvc["CheckoutService"]:::service
        end

        subgraph "Model/Repository Layer"
            AuthModel["Auth Models/Repos"]:::model
            ProdModel["ProductModel"]:::model
            CartModel["CartModel"]:::model
            DiscModel["DiscountModel"]:::model
            InvModel["InventoryModel"]:::model
            CheckModel["Checkout Models"]:::model
        end

        subgraph "Infrastructure (src/base)"
            subgraph "Database & Cache"
                DBConn["Database Module"]:::infra
                RedisMod["Redis Module"]:::infra
            end
            Swagger["Swagger Setup"]:::infra
            subgraph "Common Utilities"
                DTOs["DTOs"]:::infra
                Exc["Exceptions"]:::infra
                Handlers["Handlers"]:::infra
                Utils["Utils"]:::infra
            end
        end
    end

    %% External Systems
    subgraph "External Systems"
        Database["Relational/NoSQL DB"]:::database
        RedisDS["Redis Cache"]:::database
        Payment["External Payment Gateway"]:::external
    end

    %% Flows
    ClientWeb -->|HTTP/REST| Server
    Bruno -->|HTTP Tests| Server
    Server --> AppRouter
    Server --> Swagger

    AppRouter --> AuthRouter
    AppRouter --> ProdRouter
    AppRouter --> CartRouter
    AppRouter --> DiscRouter
    AppRouter --> InvRouter
    AppRouter --> CheckRouter

    AuthRouter --> AuthCtrl
    ProdRouter --> ProdCtrl
    CartRouter --> CartCtrl
    DiscRouter --> DiscCtrl
    InvRouter --> InvModel
    CheckRouter --> CheckCtrl

    AuthCtrl --> AuthSvc
    ProdCtrl --> ProdSvc
    CartCtrl --> CartSvc
    DiscCtrl --> DiscSvc
    CheckCtrl --> CheckSvc

    AuthSvc --> AuthModel
    ProdSvc --> ProdModel
    CartSvc --> CartModel
    DiscSvc --> DiscModel
    CheckSvc --> CheckModel

    AuthSvc --> DBConn
    ProdSvc --> DBConn
    CartSvc --> DBConn
    DiscSvc --> DBConn
    CheckSvc --> DBConn

    AuthSvc --> RedisMod
    CartSvc --> RedisMod

    DBConn --> Database
    RedisMod --> RedisDS

    CheckSvc -->|Optional| Payment

    AuthCtrl --> Handlers
    ProdCtrl --> Handlers
    CartCtrl --> Handlers
    DiscCtrl --> Handlers
    CheckCtrl --> Handlers

    %% Click Events
    click Server "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/server.ts"
    click AppRouter "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/base/router/app.router.ts"
    click DBConn "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/base/database/index.ts"
    click RedisMod "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/base/redis/index.ts"
    click Swagger "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/base/swagger/index.ts"
    click DTOs "https://github.com/tronghghe172557/ecommerce_backend_typescript/tree/main/Ecommerce_Backend/src/base/common/dtos"
    click Exc "https://github.com/tronghghe172557/ecommerce_backend_typescript/tree/main/Ecommerce_Backend/src/base/common/exceptions"
    click Handlers "https://github.com/tronghghe172557/ecommerce_backend_typescript/tree/main/Ecommerce_Backend/src/base/common/handlers"
    click Utils "https://github.com/tronghghe172557/ecommerce_backend_typescript/tree/main/Ecommerce_Backend/src/base/common/utils"
    click AuthRouter "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/auth/router/auth.router.ts"
    click AuthCtrl "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/auth/controllers/access.controller.ts"
    click AuthSvc "https://github.com/tronghghe172557/ecommerce_backend_typescript/tree/main/Ecommerce_Backend/src/modules/auth/services"
    click AuthModel "https://github.com/tronghghe172557/ecommerce_backend_typescript/tree/main/Ecommerce_Backend/src/modules/auth/models"
    click ProdRouter "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/products/router/product.router.ts"
    click ProdCtrl "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/products/controllers/product.controller.ts"
    click ProdSvc "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/products/services/product.service.ts"
    click ProdModel "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/products/models/product.model.ts"
    click CartRouter "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Cart/router/cart.router.ts"
    click CartCtrl "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Cart/controllers/cart.controller.ts"
    click CartSvc "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Cart/services/cart.service.ts"
    click CartModel "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Cart/models/cart.model.ts"
    click DiscRouter "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Discount/router/discount.router.ts"
    click DiscCtrl "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Discount/controllers/discount.controller.ts"
    click DiscSvc "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Discount/services/discount.service.ts"
    click DiscModel "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Discount/models/discount.model.ts"
    click InvRouter "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Inventory/router/inventory.router.ts"
    click InvModel "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Inventory/models/inventory.model.ts"
    click CheckRouter "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Checkouts/router/checkout.router.ts"
    click CheckCtrl "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Checkouts/controllers/checkout.controller.ts"
    click CheckSvc "https://github.com/tronghghe172557/ecommerce_backend_typescript/blob/main/Ecommerce_Backend/src/modules/Checkouts/services/checkout.service.ts"
    click CheckModel "https://github.com/tronghghe172557/ecommerce_backend_typescript/tree/main/Ecommerce_Backend/src/modules/Checkouts/models"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:1px;
    classDef entry fill:#ff9,stroke:#333,stroke-width:1px;
    classDef router fill:#9ff,stroke:#333,stroke-width:1px;
    classDef controller fill:#f99,stroke:#333,stroke-width:1px;
    classDef service fill:#99f,stroke:#333,stroke-width:1px;
    classDef model fill:#9f9,stroke:#333,stroke-width:1px;
    classDef infra fill:#ffc,stroke:#333,stroke-width:1px;
    classDef database fill:#cfc,stroke:#333,stroke-width:1px;
```
