# ssr707/e-commerce-backend architecture

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

- Interactive diagram: https://gitdiagram.com/ssr707/e-commerce-backend
- Repository: https://github.com/ssr707/e-commerce-backend

## Overview

##### Main system components
- API Layer: Represent the REST API endpoints grouped under “src/api”. Each sub-directory (address, auth, etc.) includes its own controller, service, and module. Controllers handle incoming HTTP requests and delegate tasks to services.
- Service Layer (Business Logic): Each module has corresponding service classes that encapsulate the business logic.
- Data Access Layer: Within “src/core”, the project defines entities that represent the domain models and repositories to abstract database operations. These components represent the communication between the service layer and the database.
- Common Utilities & Helpers: The “src/common” directory includes shared functionality such as decorators (for authentication and user management), guard implementations (JWT, admin, self guard, etc.), and common enums.
- Infrastructure Libraries: The “src/infrastructure” directory collects custom middleware and external integrations. For example, there are modules handling bcrypt (for secure password hashing), custom JWT (for token management), email service (for notifications), and file handling. These components serve as utility libraries that underpin authentication, messaging, and media management.
- Configuration & Environment: Look at “.env.example”, “nest-cli.json”, and “src/config” which handle environment variables and other configuration settings.
- External Deployment/CI Tools: The presence of GitHub Action workflows (in .github/workflows) and CircleCI images in the README hints at the automated testing, continuous integration, and deployment pipeline.

##### Relationships and Interactions
- API Controllers (in “src/api”) interact with corresponding Service classes to process HTTP requests.
- Service classes then call the Repository methods (in “src/core/repository”) to fetch or persist data from the database. Although the physical database isn’t explicitly shown in the file tree, it is implied by the domain entity files.
- Infrastructure modules (like JWT, email, and file handling) are utilized by services or guards to encrypt data, manage sessions, trigger notifications, or handle file operations.
- Guards and decorators (in “src/common”) wrap around controllers or methods to enforce security and manage user roles before business logic is executed.

##### Architectural Patterns and Design Principles
- Modular Architecture: The project uses a modular structure where each domain (address, auth, cart, etc.) has its own sub-directory with controllers, services, and Data Transfer Objects (DTOs). This reflects a separation-of-concerns and ease in scaling functionality.
- Layered Architecture: Clear separation between API, Business Logic, and Data Access layers.
- Dependency Injection: As recommended in NestJS practices, employ dependency injection between controllers, services, and repositories.
- Reusability via Common Modules: Utility functions and shared functionality are centralized in the common and infrastructure folders.

##### Relevant Technologies, Frameworks, and Libraries
- NestJS and TypeScript for building the server-side application.
- Node.js is the underlying runtime platform.
- bcrypt, custom-jwt, and email libraries for security and messaging.
- Testing frameworks (likely Jest, as seen in the “test” directory) for unit and end-to-end testing.
- External CI/CD integrations via GitHub Actions and CircleCI.

##### Final Touches
- Lastly, annotate any external integrations such as the deployment step instructions using Mau (as described in the README) to show how the application is deployed on AWS or other platforms.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% API Layer
    subgraph "API Layer"
        API_Layer["API Layer"]
        Auth_Controller["Auth Controller"]
        Address_Controller["Address Controller"]
        Banner_Controller["Banner Controller"]
        Cart_Controller["Cart Controller"]
        Category_Controller["Category Controller"]
        Order_Controller["Order Controller"]
        Product_Controller["Product Controller"]
        Reviews_Controller["Reviews Controller"]
        User_Controller["User Controller"]
    end

    %% Service Layer
    subgraph "Service Layer"
        Auth_Service["Auth Service"]
        Address_Service["Address Service"]
        Banner_Service["Banner Service"]
        Cart_Service["Cart Service"]
        Category_Service["Category Service"]
        Order_Service["Order Service"]
        Product_Service["Product Service"]
        Reviews_Service["Reviews Service"]
        User_Service["User Service"]
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        subgraph "Entities"
            User_Entity["User Entity"]
        end
        subgraph "Repositories"
            User_Repository["User Repository"]
        end
    end

    %% Common Utilities & Helpers
    subgraph "Common Utilities"
        Common_Utilities["Common Utilities & Helpers"]
    end

    %% Infrastructure Libraries
    subgraph "Infrastructure Libraries"
        Bcrypt["Bcrypt"]
        Custom_JWT["Custom JWT"]
        Email_Service["Email Service"]
        File_Handler["File Handler"]
    end

    %% Configuration & CI/CD
    Config["Configuration & Environment"]
    CI_CD_Pipeline["CI/CD Pipeline"]

    %% Relationships between API Controllers and Services
    Auth_Controller -->|"calls"| Auth_Service
    Address_Controller -->|"calls"| Address_Service
    Banner_Controller -->|"calls"| Banner_Service
    Cart_Controller -->|"calls"| Cart_Service
    Category_Controller -->|"calls"| Category_Service
    Order_Controller -->|"calls"| Order_Service
    Product_Controller -->|"calls"| Product_Service
    Reviews_Controller -->|"calls"| Reviews_Service
    User_Controller -->|"calls"| User_Service

    %% Service Layer calls to Data Access (Repositories)
    Auth_Service -->|"accesses"| User_Repository
    Product_Service -->|"accesses"| User_Repository
    Order_Service -->|"accesses"| User_Repository

    %% Service Layer using Infrastructure Libraries
    Auth_Service -->|"uses"| Custom_JWT
    Cart_Service -->|"uses"| File_Handler
    Product_Service -->|"uses"| Email_Service

    %% Common Utilities used by API Controllers
    Common_Utilities -->|"secures"| Auth_Controller

    %% Configuration applied to layers
    Config -->|"configures"| API_Layer
    Config -->|"configures"| Auth_Service
    Config -->|"configures"| User_Repository

    %% CI/CD Pipeline triggers configuration/deployment
    CI_CD_Pipeline -->|"deploys"| Config

    %% Class Definitions for styling and colors
    classDef api fill:#ffefef,stroke:#cc0000,stroke-width:2px;
    classDef service fill:#eef9ff,stroke:#0066cc,stroke-width:2px;
    classDef data fill:#efffef,stroke:#339933,stroke-width:2px;
    classDef common fill:#fff7e6,stroke:#cc7a00,stroke-width:2px;
    classDef infra fill:#e6f7ff,stroke:#008080,stroke-width:2px;
    classDef config fill:#fbeffb,stroke:#cc3399,stroke-width:2px;
    classDef cicd fill:#f0f0f0,stroke:#808080,stroke-width:2px;

    %% Assign styles
    class API_Layer,Auth_Controller,Address_Controller,Banner_Controller,Cart_Controller,Category_Controller,Order_Controller,Product_Controller,Reviews_Controller,User_Controller api;
    class Auth_Service,Address_Service,Banner_Service,Cart_Service,Category_Service,Order_Service,Product_Service,Reviews_Service,User_Service service;
    class User_Entity,User_Repository data;
    class Common_Utilities common;
    class Bcrypt,Custom_JWT,Email_Service,File_Handler infra;
    class Config config;
    class CI_CD_Pipeline cicd;

    %% Click Events
    click API_Layer "https://github.com/ssr707/e-commerce-backend/tree/main/src/api"
    click Auth_Service "https://github.com/ssr707/e-commerce-backend/blob/main/src/api/auth/auth.service.ts"
    click Product_Service "https://github.com/ssr707/e-commerce-backend/blob/main/src/api/product/product.service.ts"
    click User_Entity "https://github.com/ssr707/e-commerce-backend/blob/main/src/core/entity/user.entity.ts"
    click User_Repository "https://github.com/ssr707/e-commerce-backend/blob/main/src/core/repository/user.repository.ts"
    click Common_Utilities "https://github.com/ssr707/e-commerce-backend/tree/main/src/common"
    click Bcrypt "https://github.com/ssr707/e-commerce-backend/tree/main/src/infrastructure/lib/bcrypt"
    click Custom_JWT "https://github.com/ssr707/e-commerce-backend/tree/main/src/infrastructure/lib/custom-jwt"
    click Email_Service "https://github.com/ssr707/e-commerce-backend/tree/main/src/infrastructure/lib/email"
    click File_Handler "https://github.com/ssr707/e-commerce-backend/tree/main/src/infrastructure/lib/file"
    click Config "https://github.com/ssr707/e-commerce-backend/blob/main/src/config/index.ts"
    click CI_CD_Pipeline "https://github.com/ssr707/e-commerce-backend/blob/main/.github/workflows/ci-cd.yml"
```
