# hotrucdiep21/e-commerce architecture

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

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

## Overview

##### Overall Project Type and Purpose
- This is a full-stack E-Commerce website that supports user authentication, product listings, shopping cart management, payment processing (via Stripe), sales analytics, category management, coupon systems, and an admin dashboard.
- The application emphasizes caching (Redis), cloud-based media storage (Cloudinary), and secure authentication using JWT with refresh and access tokens.

##### Project Structure
- The project is clearly divided into two main sections:
- Frontend: A React-based user interface built with Vite, styled with Tailwind, with components, pages, and state management (using custom stores).
- Backend: A Node.js/Express-based API handling core business logic, routes, controllers, models (for products, users, orders, coupons), middleware (e.g., authentication), and utility libraries for third-party integrations (MongoDB, Redis, Stripe, Cloudinary).

##### Identifying the Main Components
- Frontend:
- Client UI (React Components, Pages, and Stores)
- API Requests Layer (Axios integration for API calls)
- Routing & State Management (React pages, store files managing UI state)
- Backend:
- API Server (Express server in backend/server.js)
- Controllers (Handle business logic for analytics, authentication, cart, coupon, payment, and product operations)
- Routes (Mapping of URL endpoints to controller functions)
- Middleware (Authentication middleware for security)
- Models (Data schema definitions for coupons, orders, products, and users that interact with MongoDB)
- Libraries/Utils:
- Database Connection (MongoDB in backend/lib/db.js)
- Redis Integration (Caching logic in backend/lib/redis.js)
- Stripe Integration (Payment processing with backend/lib/stripe.js)
- Cloudinary (Image/media management via backend/lib/cloudinary.js)
- Auth Utilities (Helper functions related to authentication in backend/utils/auth.utils.js)
- Supporting Services & Databases:
- MongoDB for persistent data storage.
- Redis for caching.
- Stripe for processing payments.
- Cloudinary for handling media assets.

##### Determining Relationships and Interactions
- Backend routes map to specific controllers that contain the business logic.
- Controllers trigger interactions with:
1. Database: Read/write operations via models using MongoDB connection.
2. Cache: Save and retrieve data via Redis.
3. External Services:
- Payment (using Stripe)
- Media management (using Cloudinary)
- Authentication flows:
- Incoming requests pass through middleware that verifies JWT tokens.
- Auth controllers manage signup/login flows.
- Admin Dashboard: A dedicated path on the frontend interacts with backend analytics and administrative endpoints.

##### Architectural Patterns and Design Principles
- Clear separation of concerns: frontend and backend are distinct entities.
- MVC-like pattern in the backend where models, controllers, and routes are separated.
- Integration of third-party services (Stripe, Cloudinary, Redis) as separate utility modules.
- Use of middleware to handle cross-cutting concerns (security and authentication).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External User
    U("User"):::external

    %% Frontend Section
    subgraph "User Interaction & Frontend"
        direction TB
        FEComp("React Components"):::frontend
        FEPages("React Pages"):::frontend
        FEStores("State Management (Stores)"):::frontend
        APIReq("API Requests Layer (Axios)"):::frontend
    end

    %% Backend Section
    subgraph "Backend API & Business Logic"
        direction TB
        APIServer("API Server (Express)"):::backend
        subgraph "API Structure"
            direction TB
            Routes("API Routes"):::backend
            Controllers("Controllers"):::backend
            Middleware("Authentication Middleware"):::backend
            Models("Data Models (MongoDB Schemas)"):::backend
        end
        subgraph "Utilities & Integrations"
            direction TB
            DB("MongoDB Connection"):::backendUtil
            RedisInt("Redis Integration (Caching)"):::backendUtil
            StripeInt("Stripe Integration (Payment)"):::backendUtil
            CloudinaryInt("Cloudinary Integration"):::backendUtil
            AuthUtils("Auth Utilities"):::backendUtil
        end
    end

    %% External Services Section
    subgraph "External Services & Data Stores"
        direction TB
        MongoDB("MongoDB Database"):::externalSys
        RedisDB("Redis Cache"):::externalSys
        StripeExt("Stripe Payment Service"):::externalSys
        CloudinaryExt("Cloudinary Media Service"):::externalSys
    end

    %% Connections from User to Frontend
    U -->|"interacts"| FEComp
    U -->|"interacts"| FEPages
    U -->|"interacts"| FEStores

    %% Frontend internal flow
    FEComp -->|"UI action"| APIReq
    FEPages -->|"Page call"| APIReq
    FEStores -->|"State change"| APIReq

    %% Frontend to Backend API
    APIReq -->|"REST API call"| APIServer

    %% Backend internal flow
    APIServer -->|"routes request"| Routes
    APIServer -->|"validate token"| Middleware
    Routes -->|"calls"| Controllers
    Controllers -->|"uses schema"| Models

    %% Controllers using Utilities
    Controllers -->|"db operation"| DB
    Controllers -->|"cache operation"| RedisInt
    Controllers -->|"payment processing"| StripeInt
    Controllers -->|"media handling"| CloudinaryInt
    Controllers -->|"auth helpers"| AuthUtils

    %% Utilities to External Services
    DB -->|"persistent storage"| MongoDB
    RedisInt -->|"cache storage"| RedisDB
    StripeInt -->|"process payment"| StripeExt
    CloudinaryInt -->|"store media"| CloudinaryExt

    %% Click Events for Frontend
    click FEComp "https://github.com/hotrucdiep21/e-commerce/tree/master/frontend/src/components"
    click FEPages "https://github.com/hotrucdiep21/e-commerce/tree/master/frontend/src/pages"
    click FEStores "https://github.com/hotrucdiep21/e-commerce/tree/master/frontend/src/stores"
    click APIReq "https://github.com/hotrucdiep21/e-commerce/blob/master/frontend/src/lib/axios.js"

    %% Click Events for Backend
    click APIServer "https://github.com/hotrucdiep21/e-commerce/blob/master/backend/server.js"
    click Controllers "https://github.com/hotrucdiep21/e-commerce/tree/master/backend/controller"
    click Routes "https://github.com/hotrucdiep21/e-commerce/tree/master/backend/routes"
    click Middleware "https://github.com/hotrucdiep21/e-commerce/blob/master/backend/middleware/auth.middleware.js"
    click Models "https://github.com/hotrucdiep21/e-commerce/tree/master/backend/model"
    click DB "https://github.com/hotrucdiep21/e-commerce/blob/master/backend/lib/db.js"
    click RedisInt "https://github.com/hotrucdiep21/e-commerce/blob/master/backend/lib/redis.js"
    click StripeInt "https://github.com/hotrucdiep21/e-commerce/blob/master/backend/lib/stripe.js"
    click CloudinaryInt "https://github.com/hotrucdiep21/e-commerce/blob/master/backend/lib/cloudinary.js"
    click AuthUtils "https://github.com/hotrucdiep21/e-commerce/blob/master/backend/utils/auth.utils.js"

    %% Styles
    classDef frontend fill:#FFEEAD,stroke:#333,stroke-width:2px;
    classDef backend fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef backendUtil fill:#D6EAF8,stroke:#333,stroke-width:2px;
    classDef external fill:#C3FDB8,stroke:#333,stroke-width:2px;
    classDef externalSys fill:#C8E6C9,stroke:#333,stroke-width:2px;
```
