# thanhhai1011/watch-store architecture

> Architecture diagram of the GitHub repository thanhhai1011/watch-store, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thanhhai1011/watch-store
- Repository: https://github.com/thanhhai1011/watch-store

## Overview

##### Project Type and Purpose
- This is a full-stack e-commerce application ("watch-store") with two main user roles (customers & admin).
- Frontend: a React/Redux single-page app with public pages, user account flows, shopping cart, chat, and an admin console.
- Backend: Node.js/Express REST + WebSocket API, MVC pattern, with controllers/routers/models, connecting to a database and a few external services (Cloudinary, GHN shipping, VnPay payment gateway).

##### Main Components
a. Frontend (React/Redux SPA)
- UI components: pages (Home, ProductDetail, Cart, Order, Chat, Admin), shared components (Header, Footer, Alerts).
- State management: Redux store + actions + reducers.
- API client services: config.services.js, Axios (or fetch) wrappers.
- WebSocket client for real-time chat (socket.io).
b. Backend (Node.js & Express)
- API server entrypoint: index.js
- Configuration layers: db/db.js (database connection), cloudinary.js (media upload), socket.js (WebSocket setup).
- Controllers: business logic per domain (UserController, ProductController, OrderController, ChatController, etc.).
- Routers: REST endpoints organized by resource (UserRouter, ProductRouter, OrderRouter, etc.).
- Models: database schemas for Users, Products, Orders, Messages, Conversations, ListTypeProduct, SelectList.
- Utils: shared helpers (until.js).
c. Database
- Primary data store (e.g., MongoDB or SQL) for all models.
d. WebSocket Service
- Real-time channel (socket.io) for chat messages and conversation events.
e. External Services
- Cloudinary: image/media storage.
- GHN: shipping API integration.
- VnPay: payment gateway (redirect flow, success callback).

##### Relationships & Data Flow
- The React client issues HTTP requests (REST/JSON) to the Express API for CRUD operations (products, users, orders, lists).
- On checkout, the backend coordinates with VnPay (redirect → callback) and GHN (shipping rates & label creation).
- File uploads from the client go through the API to Cloudinary (images for products).
- The client opens a WebSocket connection to the same server (socket.js) to send/receive chat messages; the server persists messages to the DB.
- Admin and customer UIs share the API but enforce role-based access (via JWT/session middleware in UserController).

##### Architectural Patterns & Design Principles
- MVC on the server: routers → controllers → models.
- Three-tier architecture: Presentation (React), Application (Express business logic + WebSocket), Data (DB).
- Redux on the client: unidirectional data flow for UI state.
- Layered configuration: separation of concerns for database, cloudinary, and socket.
- REST for resource operations, WebSocket for real-time.
- Integration points clearly externalized (GHN & VnPay clients encapsulated in controllers).

##### Final Advice
- Separation of concerns (frontend vs. backend vs. services).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend (React/Redux Client)" 
        direction TB
        UI["UI Pages\n(Home, ProductDetail, Cart, Order, Chat, Admin)"]:::frontend
        Shared["Shared Components\n(Header, Footer, Alerts)"]:::frontend
        Redux["Redux Store & State\n(Actions, Reducers, Store)"]:::frontend
        APIClient["API Client Services\n(Axios Config)"]:::frontend
        WSClient["WebSocket Client\n(socket.io chat)"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend (Express API & Socket.io)" 
        direction TB
        API["Express API Server\n(index.js)"]:::backend
        subgraph "Configurations"
            DBConfig["Database Config"]:::backend
            CloudConfig["Cloudinary Config"]:::backend
            SocketConfig["Socket.io Config"]:::backend
        end
        subgraph "Application Layer"
            Routers["Routers\n(UserRouter, ProductRouter, ...)"]:::backend
            Controllers["Controllers\n(UserController, ProductController, ...)"]:::backend
            Models["Models\n(UserModel, ProductModel, ...)"]:::backend
            Utils["Utilities\n(until.js)"]:::backend
        end
    end

    %% Database
    DB["MongoDB"]:::db

    %% External Services
    Cloudinary["Cloudinary"]:::external
    GHN["GHN Shipping API"]:::external
    VnPay["VnPay Gateway"]:::external

    %% Data Flows
    UI -->|HTTP/REST| API
    Redux -->|dispatch actions| API
    APIClient -->|HTTP/REST| API
    WSClient -->|WS| SocketConfig
    SocketConfig -->|WS| WSClient
    API -->|CRUD| DB
    API -->|HTTP| Cloudinary
    API -->|HTTP| GHN
    API -->|HTTP| VnPay

    %% Internal Flow
    API --> Routers
    Routers --> Controllers
    Controllers --> Models
    Controllers --> Utils

    %% Click Events - Frontend
    click UI "https://github.com/thanhhai1011/watch-store/tree/master/frontend/src/pages"
    click Shared "https://github.com/thanhhai1011/watch-store/blob/master/frontend/src/components/header/Header.js"
    click Shared "https://github.com/thanhhai1011/watch-store/blob/master/frontend/src/components/footer/Footer.js"
    click Shared "https://github.com/thanhhai1011/watch-store/blob/master/frontend/src/components/Alert/Alert.js"
    click Redux "https://github.com/thanhhai1011/watch-store/tree/master/frontend/src/actions"
    click Redux "https://github.com/thanhhai1011/watch-store/tree/master/frontend/src/reducers"
    click Redux "https://github.com/thanhhai1011/watch-store/blob/master/frontend/src/store.js"
    click APIClient "https://github.com/thanhhai1011/watch-store/blob/master/frontend/src/services/config.services.js"
    click WSClient "https://github.com/thanhhai1011/watch-store/blob/master/frontend/src/actions/ChatAction.js"
    click WSClient "https://github.com/thanhhai1011/watch-store/tree/master/frontend/src/components/AppChat"

    %% Click Events - Backend
    click API "https://github.com/thanhhai1011/watch-store/blob/master/backend/index.js"
    click DBConfig "https://github.com/thanhhai1011/watch-store/blob/master/backend/config/db/db.js"
    click CloudConfig "https://github.com/thanhhai1011/watch-store/blob/master/backend/config/cloudinary/cloudinary.js"
    click SocketConfig "https://github.com/thanhhai1011/watch-store/blob/master/backend/config/socket/socket.js"
    click Controllers "https://github.com/thanhhai1011/watch-store/blob/master/backend/controllers/UserController.js"
    click Controllers "https://github.com/thanhhai1011/watch-store/blob/master/backend/controllers/ProductController.js"
    click Controllers "https://github.com/thanhhai1011/watch-store/blob/master/backend/controllers/OrderController.js"
    click Controllers "https://github.com/thanhhai1011/watch-store/blob/master/backend/controllers/ChatController.js"
    click Controllers "https://github.com/thanhhai1011/watch-store/blob/master/backend/controllers/PaymentController.js"
    click Controllers "https://github.com/thanhhai1011/watch-store/blob/master/backend/controllers/ListTypeProductController.js"
    click Controllers "https://github.com/thanhhai1011/watch-store/blob/master/backend/controllers/SelectListController.js"
    click Routers "https://github.com/thanhhai1011/watch-store/blob/master/backend/routers/UserRouter.js"
    click Routers "https://github.com/thanhhai1011/watch-store/blob/master/backend/routers/ProductRouter.js"
    click Routers "https://github.com/thanhhai1011/watch-store/blob/master/backend/routers/OrderRouter.js"
    click Routers "https://github.com/thanhhai1011/watch-store/blob/master/backend/routers/ChatRouter.js"
    click Routers "https://github.com/thanhhai1011/watch-store/blob/master/backend/routers/PaymentRouter.js"
    click Routers "https://github.com/thanhhai1011/watch-store/blob/master/backend/routers/ListTypeProductRouter.js"
    click Routers "https://github.com/thanhhai1011/watch-store/blob/master/backend/routers/SelectListRouter.js"
    click Models "https://github.com/thanhhai1011/watch-store/blob/master/backend/models/UserModel.js"
    click Models "https://github.com/thanhhai1011/watch-store/blob/master/backend/models/ProductModel.js"
    click Models "https://github.com/thanhhai1011/watch-store/blob/master/backend/models/OrderModel.js"
    click Models "https://github.com/thanhhai1011/watch-store/blob/master/backend/models/ConversationModel.js"
    click Models "https://github.com/thanhhai1011/watch-store/blob/master/backend/models/MessageModel.js"
    click Models "https://github.com/thanhhai1011/watch-store/blob/master/backend/models/ListTypeProductModel.js"
    click Models "https://github.com/thanhhai1011/watch-store/blob/master/backend/models/SelectListModel.js"
    click Utils "https://github.com/thanhhai1011/watch-store/blob/master/backend/untils/until.js"

    %% Click Events - External Integrations
    click GHN "https://github.com/thanhhai1011/watch-store/blob/master/frontend/src/actions/GhnAction.js"
    click VnPay "https://github.com/thanhhai1011/watch-store/blob/master/frontend/src/components/order/VnPay.js"
    click VnPay "https://github.com/thanhhai1011/watch-store/blob/master/frontend/src/components/order/VnPaySuccess.js"

    %% Styles
    classDef frontend fill:#cfe2ff,stroke:#084298,color:#084298
    classDef backend fill:#d1e7dd,stroke:#0f5132,color:#0f5132
    classDef db fill:#d1ecf1,stroke:#0c5460,color:#0c5460
    classDef external fill:#fff3cd,stroke:#856404,color:#856404
```
