# mdtdz113/backend-ecom architecture

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

- Interactive diagram: https://gitdiagram.com/mdtdz113/backend-ecom
- Repository: https://github.com/mdtdz113/backend-ecom

## Overview

##### Project type and purpose
- This is a RESTful E-commerce API built with Node.js, Express and MongoDB (via Mongoose).
- It exposes endpoints for user authentication, product catalog, shopping cart, order management and payment processing (via a SePay webhook).

##### File structure
- Top-level files:
- index.js – application entry point
- db.js – database connection setup
- swagger.js / swagger.json – auto-generated API docs
- Directories:
- controllers/ – business-logic functions (cart.js, order.js, product.js, payment.js, user.js)
- routers/ – HTTP route definitions binding controllers to URL paths
- models/ – Mongoose data models (User, Auth, Product, Cart, Order, Base)
- middleware/ – request filters (authentication, admin guard, generic error/validation)
- docs/ – markdown documentation of architecture, API reference, workflows
- Config files:
- package.json / package-lock.json – dependencies
- vercel.json – deployment config

##### README for additional insights
- The README describes the overall projects, tech stack, folder layout, request flow and key use cases (sign-up/login, browse products, cart, order, payment, status update via webhook).

##### Essential elements
- Clear component labels (e.g., “Express Router”, “CartController”)
- Notes for environment variables (e.g., DB_URI, JWT_SECRET)
- Deployment target (Vercel) and any scaling considerations (e.g., horizontal scaling of API containers)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients
    Client["Client\n(Browser/Mobile)"]:::external
    Client -->|"HTTP Requests"| APIGateway

    %% API Gateway
    APIGateway["Express Server\n(index.js)"]:::api
    click APIGateway "https://github.com/mdtdz113/backend-ecom/blob/main/index.js"

    %% Swagger UI
    APIGateway -->|"GET /docs"| SwaggerUI
    SwaggerUI["Swagger UI\n(swagger.js)"]:::docs
    click SwaggerUI "https://github.com/mdtdz113/backend-ecom/blob/main/swagger.js"
    click SwaggerUI "https://github.com/mdtdz113/backend-ecom/blob/main/swagger.json"

    %% Deployment
    APIGateway -->|"Deployed on"| Vercel
    Vercel["Vercel\n(vercel.json)"]:::infra
    click Vercel "https://github.com/mdtdz113/backend-ecom/blob/main/vercel.json"

    %% Router Layer
    subgraph "Router Layer"
        direction TB
        R_Index["routers/index.js"]:::api
        R_Auth["routers/auth.js"]:::api
        R_Cart["routers/cart.js"]:::api
        R_Order["routers/order.js"]:::api
        R_Payment["routers/payment.js"]:::api
        R_Products["routers/products.js"]:::api
        R_User["routers/user.js"]:::api
    end
    APIGateway --> R_Index
    APIGateway --> R_Auth
    APIGateway --> R_Cart
    APIGateway --> R_Order
    APIGateway --> R_Payment
    APIGateway --> R_Products
    APIGateway --> R_User

    click R_Index "https://github.com/mdtdz113/backend-ecom/blob/main/routers/index.js"
    click R_Auth "https://github.com/mdtdz113/backend-ecom/blob/main/routers/auth.js"
    click R_Cart "https://github.com/mdtdz113/backend-ecom/blob/main/routers/cart.js"
    click R_Order "https://github.com/mdtdz113/backend-ecom/blob/main/routers/order.js"
    click R_Payment "https://github.com/mdtdz113/backend-ecom/blob/main/routers/payment.js"
    click R_Products "https://github.com/mdtdz113/backend-ecom/blob/main/routers/products.js"
    click R_User "https://github.com/mdtdz113/backend-ecom/blob/main/routers/user.js"

    %% Middleware Layer
    subgraph "Middleware Layer"
        direction TB
        M_Auth["authMiddleware.js"]:::middleware
        M_Admin["adminMiddleware.js"]:::middleware
        M_Error["middleware.js"]:::middleware
    end
    R_Index --> M_Auth
    R_Index --> M_Admin
    R_Index --> M_Error

    click M_Auth "https://github.com/mdtdz113/backend-ecom/blob/main/middleware/authMiddleware.js"
    click M_Admin "https://github.com/mdtdz113/backend-ecom/blob/main/middleware/adminMiddleware.js"
    click M_Error "https://github.com/mdtdz113/backend-ecom/blob/main/middleware/middleware.js"

    %% Controller Layer
    subgraph "Controller Layer"
        direction TB
        C_User["controller/user.js"]:::api
        C_Product["controller/product.js"]:::api
        C_Cart["controller/cart.js"]:::api
        C_Order["controller/order.js"]:::api
        C_Payment["controller/payment.js"]:::api
    end
    M_Auth --> C_User
    M_Auth --> C_Product
    M_Auth --> C_Cart
    M_Auth --> C_Order
    M_Auth --> C_Payment

    click C_User "https://github.com/mdtdz113/backend-ecom/blob/main/controller/user.js"
    click C_Product "https://github.com/mdtdz113/backend-ecom/blob/main/controller/product.js"
    click C_Cart "https://github.com/mdtdz113/backend-ecom/blob/main/controller/cart.js"
    click C_Order "https://github.com/mdtdz113/backend-ecom/blob/main/controller/order.js"
    click C_Payment "https://github.com/mdtdz113/backend-ecom/blob/main/controller/payment.js"

    %% Models Layer
    subgraph "Models Layer"
        direction TB
        Model_User["models/User.js"]:::db
        Model_Auth["models/Auth.js"]:::db
        Model_Product["models/Product.js"]:::db
        Model_Cart["models/Cart.js"]:::db
        Model_Order["models/Order.js"]:::db
        Model_Base["models/Base.js"]:::db
    end
    C_User --> Model_User
    C_User --> Model_Auth
    C_Product --> Model_Product
    C_Cart --> Model_Cart
    C_Order --> Model_Order
    Model_User --> DB
    Model_Auth --> DB
    Model_Product --> DB
    Model_Cart --> DB
    Model_Order --> DB
    Model_Base --> DB

    click Model_User "https://github.com/mdtdz113/backend-ecom/blob/main/models/User.js"
    click Model_Auth "https://github.com/mdtdz113/backend-ecom/blob/main/models/Auth.js"
    click Model_Product "https://github.com/mdtdz113/backend-ecom/blob/main/models/Product.js"
    click Model_Cart "https://github.com/mdtdz113/backend-ecom/blob/main/models/Cart.js"
    click Model_Order "https://github.com/mdtdz113/backend-ecom/blob/main/models/Order.js"
    click Model_Base "https://github.com/mdtdz113/backend-ecom/blob/main/models/Base.js"

    %% Database
    DB["MongoDB\n(db.js)"]:::db
    click DB "https://github.com/mdtdz113/backend-ecom/blob/main/db.js"

    %% External Payment Service
    C_Payment -->|"POST /pay"| SePay
    SePay["SePay Service"]:::external
    SePay -.->|"Webhook /payment/notify"| C_Payment

    %% Documentation
    subgraph "Project Docs"
        direction TB
        Docs_Readme["docs/README.md"]:::docs
        Docs_API["docs/api-reference.md"]:::docs
        Docs_Tong["docs/tong-quan.md"]:::docs
        Docs_CTDuAn["docs/cau-truc-du-an.md"]:::docs
        Docs_Others["Other docs"]:::docs
    end
    click Docs_Readme "https://github.com/mdtdz113/backend-ecom/blob/main/docs/README.md"
    click Docs_API "https://github.com/mdtdz113/backend-ecom/blob/main/docs/api-reference.md"
    click Docs_Tong "https://github.com/mdtdz113/backend-ecom/blob/main/docs/tong-quan.md"
    click Docs_CTDuAn "https://github.com/mdtdz113/backend-ecom/blob/main/docs/cau-truc-du-an.md"

    %% Dependencies
    subgraph "Config & Dependencies"
        direction TB
        Pkg["package.json"]:::infra
        Lock["package-lock.json"]:::infra
    end
    click Pkg "https://github.com/mdtdz113/backend-ecom/blob/main/package.json"
    click Lock "https://github.com/mdtdz113/backend-ecom/blob/main/package-lock.json"

    %% Styles
    classDef api fill:#cce5ff,stroke:#004085,color:#004085;
    classDef db fill:#d4edda,stroke:#155724,color:#155724;
    classDef external fill:#fff3cd,stroke:#856404,color:#856404;
    classDef middleware fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
    classDef docs fill:#f8d7da,stroke:#721c24,color:#721c24;
    classDef infra fill:#e2f0d9,stroke:#2d572c,color:#2d572c;
```
