# tienkim9920/cnpm-shopping architecture

> Architecture diagram of the GitHub repository tienkim9920/cnpm-shopping, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tienkim9920/cnpm-shopping
- Repository: https://github.com/tienkim9920/cnpm-shopping

## Overview

##### Project Type & Purpose
- This is a full-stack e-commerce web application for selling clothing, consisting of:
- An **Admin SPA** (ReactJS) for back-office management (admin_app).
- A **Client SPA** (ReactJS) for shoppers (client_app).
- A **RESTful API** and WebSocket server (NodeJS/Express, MongoDB via Mongoose, mailer, Socket.io) providing data, authentication, live chat, email notifications, and integrations (server_app).

##### Core Components
a. **Admin Frontend**
- Built with ReactJS (admin_app/src).
- Consumes admin-scoped API routes (/api/admin/…).
- Bundles static assets, SCSS, charts, maps, datatables for dashboards.
b. **Client Frontend**
- Built with ReactJS (client_app/src).
- Uses Redux for state (cart, session, search).
- Integrates with e-commerce APIs, payment UI (PayPal, MoMo), Google Maps for delivery cost, live chat (Messenger plugin).
c. **Backend API & Real-Time**
- Express server (server_app/index.js).
- Router modules under server_app/API/Router and controllers under server_app/API/Controller.
- Socket.io for live chat (as per “Socket” in README).
- Nodemailer for order confirmation emails.
d. **Database**
- MongoDB accessed via Mongoose models (server_app/Models/*.js).
- Implements entities: User, Product, Category, Order, Detail_Order, Comment, Coupon, Permission, Delivery, Payment, Favorite, Note.
e. **External Services**
- **Payment Gateways**: PayPal, MoMo.
- **Email**: SMTP via Nodemailer.
- **Maps**: Google Maps API for distance-based delivery cost.
- **Chat**: Facebook Messenger plugin + Socket.io.

##### Relationships & Data Flow
- **Client** and **Admin** frontends ←→ **API** (HTTPS REST calls + WebSocket).
- **API** ←→ **MongoDB** (CRUD via Mongoose).
- **API** → **SMTP Server** (order confirmation emails).
- **API** → **Payment Gateway** (redirect/callback).
- **Client** → **Maps API** (JS SDK) for delivery estimation.
- **Client** ↔ **Messenger Plugin** (embedded chat widget) or direct WebSocket.

##### Architectural Patterns & Principles
- **Three-Tier Architecture**: Presentation (SPAs) / Business Logic (Express) / Data (MongoDB).
- **Module Separation**: controllers, routers, models.
- **Context & State Management**: React Context for auth (admin), Redux for client shopping state.
- **RESTful Routing**: clearly namespaced endpoints (e.g., /api/product, /api/admin/product).
- **Real-Time Messaging**: Socket.io or FB Messenger for live chat.
- **Scalability**: separate frontends allow independent deployment; stateless API servers.
- **Security**: permission model (roles), JWT or session via Auth context.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Admin SPA"
        AdminUI["Admin SPA"]:::admin
        AdminStatic["Static Assets & Styles"]:::admin
    end

    subgraph "Client SPA"
        ClientUI["Client SPA"]:::client
        Redux["Redux State Management"]:::client
        subgraph "Payment UI"
            Paypal["PayPal Component"]:::client
            MoMo["MoMo Component"]:::client
        end
    end

    subgraph "API Server" 
        Index["Entry Point (index.js)"]:::backend
        subgraph "Routing Layer"
            Routers["API Routers"]:::backend
            AdminRouters["Admin-scoped Routers"]:::backend
        end
        subgraph "Controller Layer"
            Controllers["API Controllers"]:::backend
            AdminCtrl["Admin Controllers"]:::backend
        end
        Models["Mongoose Models"]:::backend
        Mailer["Email Service (Nodemailer)"]:::backend
        StaticHost["Static File Hosting"]:::backend
    end

    DB["MongoDB"]:::db

    externalServices["External Services"]
    subgraph "External Services"
        PaymentGate["Payment Gateways\n(PayPal & MoMo)"]:::ext
        SMTP["Email (SMTP)"]:::ext
        Maps["Google Maps API"]:::ext
        Chat["Messenger Plugin\n& Socket.io"]:::ext
    end

    AdminUI -->|"REST (HTTPS)"| Index
    ClientUI -->|"REST (HTTPS)"| Index
    AdminUI -->|"WebSocket (WSS)"| Chat
    ClientUI -->|"WebSocket (WSS)"| Chat
    Index -->|"uses"| Routers
    Routers -->|"calls"| Controllers
    AdminRouters -->|"calls"| AdminCtrl
    Controllers -->|"uses"| Models
    AdminCtrl -->|"uses"| Models
    Models -->|"CRUD"| DB
    Index -->|"sends email"| SMTP
    Index -->|"payment calls"| PaymentGate
    ClientUI -->|"JS SDK"| Maps
    Index -->|"serves static"| StaticHost
    Index -->|"WebSocket"| Chat

    click AdminUI "https://github.com/tienkim9920/cnpm-shopping/tree/master/admin_app/src"
    click AdminStatic "https://github.com/tienkim9920/cnpm-shopping/tree/master/admin_app/public"
    click ClientUI "https://github.com/tienkim9920/cnpm-shopping/tree/master/client_app/src"
    click Paypal "https://github.com/tienkim9920/cnpm-shopping/blob/master/client_app/src/Checkout/Paypal.jsx"
    click MoMo "https://github.com/tienkim9920/cnpm-shopping/blob/master/client_app/src/Checkout/MoMo.jsx"
    click Index "https://github.com/tienkim9920/cnpm-shopping/blob/master/server_app/index.js"
    click Routers "https://github.com/tienkim9920/cnpm-shopping/tree/master/server_app/API/Router"
    click AdminRouters "https://github.com/tienkim9920/cnpm-shopping/tree/master/server_app/API/Router/admin"
    click Controllers "https://github.com/tienkim9920/cnpm-shopping/tree/master/server_app/API/Controller"
    click AdminCtrl "https://github.com/tienkim9920/cnpm-shopping/tree/master/server_app/API/Controller/admin"
    click Models "https://github.com/tienkim9920/cnpm-shopping/blob/master/server_app/Models/*.js"
    click Mailer "https://github.com/tienkim9920/cnpm-shopping/blob/master/server_app/mailer.js"
    click StaticHost "https://github.com/tienkim9920/cnpm-shopping/tree/master/server_app/public"

    classDef admin fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef client fill:#ccf2ff,stroke:#333,stroke-width:1px
    classDef backend fill:#e0e0e0,stroke:#333,stroke-width:1px
    classDef db fill:#f2f2f2,stroke:#333,stroke-width:1px
    classDef ext fill:#ffffcc,stroke:#333,stroke-width:1px
```
