# nnminh-sam/watch-e-commerce-nestjs architecture

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

- Interactive diagram: https://gitdiagram.com/nnminh-sam/watch-e-commerce-nestjs
- Repository: https://github.com/nnminh-sam/watch-e-commerce-nestjs

## Overview

##### Project type and purpose
- This is a monolithic back-end API service for an e-commerce store (Watch E-Commerce).
- Built with NestJS (TypeScript), it exposes REST endpoints for authentication, product/brand/category management, cart, orders and transactions.
- It persists data in MongoDB (replica set or single instance) and uses Redis for cart sessions and token black-listing.

##### File structure
- Top-level configuration: Dockerfile, docker-compose.yml (spins up API + Mongo replica set), docker-compose-single-mongo.yml available.
- src/commons: global decorators, DTOs, filters, guards, interceptors, pipes — cross-cutting concerns for request validation, auth, response shaping, logging.
- src/database: database.module bootstraps Mongoose connections; Redis services for cart and token black-list.
- src/environment: centralized environment variable management.
- src/models: Mongoose schema definitions for all domain entities (User, Product, Order, Cart, Transaction, etc.).
- src/modules: feature modules (auth, user, product, brand, category, cart, order, transaction, jwt-manager, logger). Each follows NestJS pattern: controller → service → repository (where needed) → DTOs → strategies.
- test: end-to-end tests.

##### Key insights from README
- Emphasis on MongoDB replication topology or single instance via docker-compose.
- No other external dependencies beyond Redis, Mongo and the NestJS API itself.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Clients
    Clients["Clients (Web/Mobile)"]:::external

    %% Deployment Environment
    subgraph "Docker Host"
        DockerConfig["Docker & Compose Config"]:::config
        click DockerConfig "https://github.com/nnminh-sam/watch-e-commerce-nestjs/tree/main/Dockerfile"
        click DockerConfig "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/docker-compose.yml"
        subgraph "Containers"
            API["NestJS API"]:::compute
            click API "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/main.ts"
            click API "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/app.module.ts"
            MongoPrimary["MongoDB Primary"]:::db
            MongoSecondary["MongoDB Secondary"]:::db
            Redis["Redis Cache"]:::cache
        end
    end

    %% External interactions
    Clients -->|"HTTP/HTTPS"| API

    %% Database replication
    API -->|"Mongoose CRUD"| MongoPrimary
    MongoPrimary -->|"replication"| MongoSecondary

    %% Cache interactions
    API -->|"Cart sessions"| Redis
    API -->|"Token black-list"| Redis

    %% Internal API Architecture
    subgraph "NestJS API"
        direction TB
        subgraph "Environment"
            EnvModule["Environment Module"]:::service
            EnvService["Environment Service"]:::service
            EnvDTO["Env DTO"]:::service
            click EnvModule "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/environment/environment.module.ts"
            click EnvService "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/environment/environment.service.ts"
            click EnvDTO "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/environment/env.dto.ts"
        end

        subgraph "Database Layer"
            DBIndex["Database Index"]:::service
            DatabaseModule["Database Module"]:::service
            RedisCart["Redis Cart Service"]:::service
            TokenBL["Token Black-list Service"]:::service
            click DBIndex "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/database/index.ts"
            click DatabaseModule "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/database/database.module.ts"
            click RedisCart "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/database/redis-cart.service.ts"
            click TokenBL "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/database/token-black-list.service.ts"
        end

        subgraph "Commons"
            Decorators["Decorators"]:::common
            DTOs["DTOs"]:::common
            Filters["Filters"]:::common
            Guards["Guards"]:::common
            Interceptors["Interceptors"]:::common
            Pipes["Pipes"]:::common
            click Decorators "https://github.com/nnminh-sam/watch-e-commerce-nestjs/tree/main/src/commons/decorators"
            click DTOs "https://github.com/nnminh-sam/watch-e-commerce-nestjs/tree/main/src/commons/dtos"
            click Filters "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/commons/filters/http-exception.filter.ts"
            click Guards "https://github.com/nnminh-sam/watch-e-commerce-nestjs/tree/main/src/commons/guards"
            click Interceptors "https://github.com/nnminh-sam/watch-e-commerce-nestjs/tree/main/src/commons/interceptors"
            click Pipes "https://github.com/nnminh-sam/watch-e-commerce-nestjs/tree/main/src/commons/pipes"
        end

        subgraph "Feature Modules"
            AuthModule["Auth Module"]:::service
            AuthController["Auth Controller"]:::service
            AuthService["Auth Service"]:::service
            AuthDTOs["Auth DTOs"]:::service
            JWTStrategy["JWT Strategy"]:::service
            LocalStrategy["Local Strategy"]:::service

            UserModule["User Module"]:::service
            ProductModule["Product Module"]:::service
            BrandModule["Brand Module"]:::service
            CategoryModule["Category Module"]:::service
            CartModule["Cart Module"]:::service
            OrderModule["Order Module"]:::service
            TransactionModule["Transaction Module"]:::service
            JWTManager["JWT-Manager Module"]:::service
            LoggerModule["Logger Module"]:::service

            click AuthModule "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/auth/auth.module.ts"
            click AuthController "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/auth/auth.controller.ts"
            click AuthService "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/auth/auth.service.ts"
            click AuthDTOs "https://github.com/nnminh-sam/watch-e-commerce-nestjs/tree/main/src/modules/auth/dtos"
            click JWTStrategy "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/auth/strategies/jwt.strategy.ts"
            click LocalStrategy "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/auth/strategies/local.strategy.ts"
            click UserModule "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/user/user.module.ts"
            click ProductModule "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/product/product.module.ts"
            click BrandModule "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/brand/brand.module.ts"
            click CategoryModule "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/category/category.module.ts"
            click CartModule "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/cart/cart.module.ts"
            click OrderModule "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/order/order.module.ts"
            click TransactionModule "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/transaction/transaction.module.ts"
            click JWTManager "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/jwt-manager/jwt-manager.module.ts"
            click LoggerModule "https://github.com/nnminh-sam/watch-e-commerce-nestjs/blob/main/src/modules/logger/logger.module.ts"
        end

        subgraph "Models"
            Models["Mongoose Models"]:::db
            click Models "https://github.com/nnminh-sam/watch-e-commerce-nestjs/tree/main/src/models"
        end

    end

    %% Internal connections
    EnvModule -->|configures| DatabaseModule
    DatabaseModule -->|connects| MongoPrimary
    RedisCart -->|caches| Redis
    TokenBL -->|black-lists| Redis

    AuthModule -->|uses| AuthController
    AuthController -->|calls| AuthService
    AuthService -->|validates| Guards
    AuthService -->|uses| JWTStrategy
    AuthService -->|uses| LocalStrategy
    AuthService -->|stores tokens| TokenBL

    CartModule -->|manages| RedisCart
    CartModule -->|persists| DatabaseModule

    OrderModule -->|persists| DatabaseModule
    TransactionModule -->|persists| DatabaseModule
    UserModule -->|persists| DatabaseModule
    ProductModule -->|persists| DatabaseModule
    BrandModule -->|persists| DatabaseModule
    CategoryModule -->|persists| DatabaseModule

    %% Styles
    classDef external fill:#888888,stroke:#444,color:#fff;
    classDef compute fill:#bbdefb,stroke:#1e88e5,color:#000;
    classDef db fill:#c8e6c9,stroke:#388e3c,color:#000;
    classDef cache fill:#ffe0b2,stroke:#f57c00,color:#000;
    classDef config fill:#d1c4e9,stroke:#6a1b9a,color:#000;
    classDef service fill:#e3f2fd,stroke:#1976d2,color:#000;
    classDef common fill:#f5f5f5,stroke:#9e9e9e,color:#000;
```
