# nhtbao101/be-ecormerce-supabase architecture

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

- Interactive diagram: https://gitdiagram.com/nhtbao101/be-ecormerce-supabase
- Repository: https://github.com/nhtbao101/be-ecormerce-supabase

## Overview

This project is a NestJS backend application, built using TypeScript with a modular architecture. It is designed for server-side operations (such as user management, product management, orders, etc.), and it follows a clean separation of concerns using Nest’s module system. The file tree indicates that the project uses dedicated modules (e.g., auth, admin, category, order, product, and user), has clear concerns for database migrations and entities, and leverages repositories and services to encapsulate business logic.

##### Main Components
- API / HTTP Layer: Represented by the entry point (src/main.ts and api/index.ts) and controllers located in the various modules. Each controller (for auth, admin, and other modules) handles specific routes and endpoints.
- Authentication Layer: The auth module comprises controllers, strategies (admin and user), DTOs, guards, and custom decorators, which enforce access and roles.
- Business Logic Layer: These are the services within each module (e.g., admin.service.ts, product.service.ts, etc.) which encapsulate core business operations.
- Data Access Layer: The repository folders in both src/auth and src (e.g., category.repository.ts, product.repository.ts) along with the entities (admin.entity.ts, product.entity.ts, etc.) which are integral to ORM mapping and database operations.
- Database Layer: Although not directly visible as a physical database, the existence of a migrations folder (with various migration scripts) and the src/database folder (providing configuration) indicates interactions with a database.
- Infrastructure & Configuration: Files like nest-cli.json, package.json, deployment configs (vercel.json), and CI/CD configurations (.github/workflows) illustrate the development, testing, and deployment infrastructure.

##### Relationships and Interactions
- Clients interact with the API endpoint; requests are routed through controllers.
- Controllers delegate incoming requests to services where business logic is processed.
- Services call repositories for data persistence and retrieval, interfacing with the database entities.
- The authentication module is woven through the system via guards, strategies, and decorators to protect routes and manage permissions.
- Migrations update the schema of the database, ensuring the entities remain in-sync with the structural requirements.

##### Important Architectural Patterns
- Modular Architecture: Each feature domain (auth, admin, category, order, product, user) is encapsulated in its own module, supporting separation of concerns and improved maintainability.
- Dependency Injection: Core to NestJS, services and repositories are injected into controllers, enabling loose coupling.
- MVC-Like Pattern: While not strict MVC, controllers (presentation), services (business logic), and repositories/entities (data layer) clearly mimic the pattern.
- Strategy and Guard Patterns in Authentication: These protect endpoints and enforce role-based access.

##### Technologies and Frameworks
- NestJS Framework for building the backend.
- TypeScript as the language.
- Likely an ORM (given entities and migrations) such as TypeORM or Sequelize.
- GitHub Actions and possibly other CI/CD integrations (as indicated by .github/workflows) for automated testing and deployment.
- Configuration tools like ESLint, Prettier, and deployment tools (vercel, Mau for AWS integration).

## Diagram (Mermaid)

```mermaid
graph TB
    %% API / HTTP Layer
    subgraph "API / HTTP Layer"
        A1["Main Entry Point"]:::api
        A2["API Entry Point"]:::api
        A3["Auth Controller"]:::api
        A4["Admin Controller"]:::api
        A5["Category Controller"]:::api
        A6["Order Controller"]:::api
        A7["Product Controller"]:::api
        A8["User Controller"]:::api
    end

    %% Authentication Layer
    subgraph "Authentication Layer"
        B1["Auth Module"]:::auth
        B2["Auth Service"]:::auth
        B3["Signin DTO"]:::auth
        B4["Signup DTO"]:::auth
        B5["Admin Guard"]:::auth
        B6["Role Guard"]:::auth
        B7["User Guard"]:::auth
        B8["Role Decorator"]:::auth
        B9["Admin Strategy"]:::auth
        B10["User Strategy"]:::auth
        B11["Admin Repository"]:::auth
        B12["User Repository"]:::auth
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        C1["Admin Service"]:::biz
        C2["Category Service (Admin)"]:::biz
        C3["Category Service"]:::biz
        C4["Order Service (Admin)"]:::biz
        C5["Order Service"]:::biz
        C6["Product Service (Admin)"]:::biz
        C7["Product Service"]:::biz
        C8["User Service"]:::biz
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        D1["Category Repository"]:::data
        D2["Image Repository"]:::data
        D3["Order Repository"]:::data
        D4["Product Repository"]:::data
        D5["Admin Entity"]:::data
        D6["Category Entity"]:::data
        D7["Image Entity"]:::data
        D8["Order-Item Entity"]:::data
        D9["Order Entity"]:::data
        D10["Payment Entity"]:::data
        D11["Product Entity"]:::data
        D12["User Entity"]:::data
        D13["Variant Entity"]:::data
    end

    %% Database Layer
    subgraph "Database Layer"
        E1["Migration Scripts"]:::db
        E2["Database Configuration"]:::db
    end

    %% Infrastructure & Configuration
    subgraph "Infrastructure & Configuration"
        F1["Nest CLI Configuration"]:::infra
        F2["Package Files"]:::infra
        F3["ESLint Configuration"]:::infra
        F4["Prettier Configuration"]:::infra
        F5["CI/CD Pipelines"]:::infra
        F6["Deployment Config"]:::infra
        F7["TSConfig"]:::infra
        F8["TSConfig Build"]:::infra
    end

    %% Relationships between layers
    %% API Layer to Authentication & Business Logic
    A1 -->|"starts"| A3
    A2 -->|"routes"| A3
    A3 -->|"validates"| B2
    A3 -->|"delegates"| C1
    A4 -->|"delegates"| C1
    A5 -->|"delegates"| C3
    A6 -->|"delegates"| C5
    A7 -->|"delegates"| C7
    A8 -->|"delegates"| C8

    %% Authentication integration
    B1 -->|"uses"| B2
    B2 -->|"employs"| B3
    B2 -->|"employs"| B4
    B2 -->|"secures with"| B5
    B2 -->|"secures with"| B6
    B2 -->|"secures with"| B7
    B2 -->|"uses"| B8
    B2 -->|"uses"| B9
    B2 -->|"uses"| B10
    B2 -->|"accesses"| B11
    B2 -->|"accesses"| B12

    %% Business Logic calling Data Access
    C1 -->|"calls"| D1
    C1 -->|"calls"| D2
    C1 -->|"calls"| D3
    C1 -->|"calls"| D4
    C2 -->|"calls"| D1
    C3 -->|"calls"| D1
    C4 -->|"calls"| D3
    C5 -->|"calls"| D3
    C6 -->|"calls"| D4
    C7 -->|"calls"| D4
    C8 -->|"calls"| D1

    %% Data Access using Entities
    D1 -->|"maps to"| D6
    D2 -->|"maps to"| D7
    D3 -->|"maps to"| D9
    D4 -->|"maps to"| D11
    D1 ---|"associated with"| D5
    D3 ---|"associated with"| D8
    D3 ---|"associated with"| D10
    D4 ---|"associated with"| D13
    D12 ---|"represents"| A8

    %% Data store connection
    D5 -->|"persisted in"| E1
    D6 -->|"persisted in"| E1
    D7 -->|"persisted in"| E1
    D8 -->|"persisted in"| E1
    D9 -->|"persisted in"| E1
    D10 -->|"persisted in"| E1
    D11 -->|"persisted in"| E1
    D12 -->|"persisted in"| E1
    D13 -->|"persisted in"| E1
    E1 -->|"configured by"| E2

    %% Infrastructure supports all layers
    F1 --- A1
    F1 --- B1
    F1 --- C1
    F2 --- A1
    F2 --- B1
    F2 --- C1
    F5 --- A1
    F5 --- B1
    F5 --- C1
    F6 --- A1
    F7 --- A1
    F8 --- A1

    %% Click Events
    click A1 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/main.ts"
    click A2 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/api/index.ts"
    click A3 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/auth.controller.ts"
    click A4 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/admin/admin.controller.ts"
    click A5 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/category/category.controller.ts"
    click A6 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/order/order.controller.ts"
    click A7 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/product/product.controller.ts"
    click A8 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/user/user.controller.ts"

    click B1 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/auth.module.ts"
    click B2 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/auth.service.ts"
    click B3 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/dto/signin.dto.ts"
    click B4 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/dto/signup.dto.ts"
    click B5 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/guard/admin.guard.ts"
    click B6 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/guard/role.guard.ts"
    click B7 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/guard/user.guard.ts"
    click B8 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/decorator/role.ts"
    click B9 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/strategy/admin.strategy.ts"
    click B10 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/strategy/user.strategy.ts"
    click B11 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/repository/admin.repository.ts"
    click B12 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/auth/repository/user.repository.ts"

    click C1 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/admin/admin.service.ts"
    click C2 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/admin/category/category.service.ts"
    click C3 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/category/category.service.ts"
    click C4 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/admin/order/order.service.ts"
    click C5 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/order/order.service.ts"
    click C6 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/admin/product/product.service.ts"
    click C7 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/product/product.service.ts"
    click C8 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/module/user/user.service.ts"

    click D1 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/repository/category.repository.ts"
    click D2 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/repository/image.repository.ts"
    click D3 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/repository/order.repository.ts"
    click D4 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/repository/product.repository.ts"
    click D5 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/entities/admin.entity.ts"
    click D6 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/entities/category.entity.ts"
    click D7 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/entities/image.entity.ts"
    click D8 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/entities/order-item.entity.ts"
    click D9 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/entities/order.entity.ts"
    click D10 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/entities/payment.entity.ts"
    click D11 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/entities/product.entity.ts"
    click D12 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/entities/user.entity.ts"
    click D13 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/src/entities/variant.entity.ts"

    click E1 "https://github.com/nhtbao101/be-ecormerce-supabase/tree/main/migrations/"
    click E2 "https://github.com/nhtbao101/be-ecormerce-supabase/tree/main/src/database"

    click F1 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/nest-cli.json"
    click F2 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/package.json"
    click F3 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/.eslintrc.js"
    click F4 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/.prettierrc"
    click F5 "https://github.com/nhtbao101/be-ecormerce-supabase/tree/main/.github/workflows"
    click F6 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/vercel.json"
    click F7 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/tsconfig.json"
    click F8 "https://github.com/nhtbao101/be-ecormerce-supabase/blob/main/tsconfig.build.json"

    %% Styles
    classDef api fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef auth fill:#A9DFBF,stroke:#145A32,stroke-width:2px;
    classDef biz fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef data fill:#FAD7A0,stroke:#A04000,stroke-width:2px;
    classDef db fill:#E6B0AA,stroke:#922B21,stroke-width:2px;
    classDef infra fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
```
