# huonggiangtms/e_commerce_rails architecture

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

- Interactive diagram: https://gitdiagram.com/huonggiangtms/e_commerce_rails
- Repository: https://github.com/huonggiangtms/e_commerce_rails

## Overview

##### Xác định loại dự án và mục đích chính
- Đây là một ứng dụng web full-stack xây dựng trên Ruby on Rails, có:
– Giao diện khách hàng (public site + PWA/service worker)
– Giao diện quản trị (Admin dashboard)
– Module crawl sản phẩm từ bên ngoài (Product Crawler)
– Chatbot tương tác với người dùng (StimulusJS + AI service)
– Giỏ hàng, thanh toán (Stripe), email thông báo (ActionMailer)
– Xử lý bất đồng bộ (Sidekiq + Redis)

##### Phân tích cấu trúc thư mục
– app/controllers, app/models, app/views: pattern MVC điển hình của Rails
– app/frontend: StimulusJS controllers, entrypoints quản lý JS/CSS qua Vite
– app/services: business logic riêng biệt (AI Studio client, crawler, chat service…)
– app/jobs: background jobs cho crawl và lịch chạy định kỳ
– config: routes, database, sidekiq, credentials, deploy, puma
– bin/, .kamal/: script & hook để deploy/boot với công cụ kamal/docker
– db/: migration, schema, seeds
– public/: trang lỗi, robots.txt, manifest PWA, service-worker
– Gemfile, package.json: thư viện Ruby và JS

##### Tạo hệ thống thiết kế sơ đồ
a. Xác định các thành phần chính (Component)
1) Client Browser / PWA
2) Web Server (Puma)
3) Rails Application
- Controllers (MVC)
- Views (SLIM templates + StimulusJS)
- Models (ActiveRecord PostgreSQL)
- Services Layer (crawling, chat, payment)
- Mailers (email)
- Jobs (Sidekiq + Redis)
4) Database (PostgreSQL)
5) Redis (cache + queue)
6) Storage (Active Storage trên local/S3)
7) Third-party Services
- Stripe API (thanh toán)
- AI Studio / Chatbot API
- External ecommerce sites (crawler target)

b. Mối quan hệ và luồng dữ liệu
– Trình duyệt gửi HTTP(S) tới Web Server (Puma)
– Puma chuyển request vào Rails Router → Controller
– Controller tương tác Model (ActiveRecord → PostgreSQL) và/hoặc gọi Service/Job
– Views kết hợp dữ liệu từ Controller, StimulusJS bắt các sự kiện ở client
– StimulusJS gọi AJAX tới controller endpoint (chatbot, order form, cart…)
– Background Job (Sidekiq + Redis) thực hiện crawl, gửi email, chạy lịch định kỳ
– Storage lưu trữ file (ảnh sản phẩm, Active Storage)
– Rails App gọi Stripe API cho thanh toán
– Rails App gọi AI Studio / Chat Service cho chatbot

c. Các pattern & nguyên tắc kiến trúc
– MVC (Model-View-Controller)
– Service Objects tách biệt logic phức tạp ra khỏi controller/model
– Asynchronous Processing với Sidekiq/Redis
– StimulusJS cho progressive enhancement front-end
– PWA (service worker, manifest)
– Hook deploy tự động (kamal + Docker)

d. Công nghệ/Thư viện nổi bật
– Ruby on Rails, StimulusJS, Vite
– PostgreSQL, ActiveRecord, ActiveStorage
– Sidekiq, Redis
– Devise (Authentication), Rolify (Authorization)
– Stripe (Payment), ActionMailer
– Docker, Kamal (deployment), Puma

- Sử dụng hình hộp (rectangle) để biểu diễn mỗi thành phần (component)
- Gắn nhãn rõ ràng: tên component + công nghệ (vd: “Web Server (Puma)”)
- Dùng mũi tên có hướng để chỉ luồng dữ liệu / phụ thuộc giữa các thành phần
- Phân biệt bằng màu sắc hoặc hình dáng:
– Client-side (màu xanh)
– Backend Services (màu cam)
– Data Storage (màu vàng)
– Third-party (màu xám)
- Vẽ riêng phần Backend: chia nhỏ thành Web Process, Job Process, Service Layer, Data Layer
- Thêm chú thích ngắn gọn nếu cần: cron job/crawler schedule, hook deploy, env vars

##### Lưu ý khi trình bày
– Đảm bảo không bỏ sót thành phần quan trọng: Redis, Sidekiq workers, Service Objects
– Tách rõ ràng hai luồng chính:
1) Real-time request/response (HTTP + AJAX)
2) Background processing (jobs, scheduled tasks)
– Thể hiện quan hệ với external APIs: Stripe, AI Studio, các site cần crawl
– Giữ sơ đồ đủ chi tiết để dev team hiểu kiến trúc, nhưng không rối mắt

Khi hoàn thành, sơ đồ sẽ giúp mọi thành viên nhanh chóng hình dung được toàn cảnh hệ thống, luồng dữ liệu, và cách các thành phần phối hợp với nhau. Chúc bạn thành công!

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Side
    subgraph "Client Side"
        Browser["Client Browser / PWA"]:::client
    end

    %% Backend Services
    subgraph "Backend Services"
        subgraph "Web Process"
            Puma["Web Server (Puma)"]:::backend
            Controllers["Controllers (MVC)"]:::backend
            Views["Views (SLIM + StimulusJS)"]:::backend
            Models["Models (ActiveRecord/PostgreSQL)"]:::backend
            Services["Service Layer (crawler, chat, payment)"]:::backend
            Mailers["Mailers (ActionMailer)"]:::backend
        end
        subgraph "Job Process"
            Jobs["Jobs (Sidekiq + Redis)"]:::backend
        end
    end

    %% Data Layer
    subgraph "Data Layer"
        DB["Database (PostgreSQL)"]:::data
        Redis["Redis (cache + queue)"]:::data
        Storage["Storage (Active Storage)"]:::data
    end

    %% Third-party Services
    subgraph "Third-party Services"
        Stripe["Stripe API"]:::third
        AI["AI Studio / Chatbot API"]:::third
        External["External e-commerce Sites"]:::third
    end

    %% Relationships
    Browser -->|"HTTP requests"| Puma
    Puma -->|"routes"| Controllers
    Controllers -->|"fetch/store"| Models
    Controllers -->|"render/call"| Views
    Controllers -->|"invoke"| Services
    Controllers -->|"send email"| Mailers
    Controllers -->|"enqueue job"| Jobs
    Views -->|"HTML/CSS/JS"| Browser

    Jobs -->|"background crawl"| Services
    Jobs -->|"send email"| Mailers
    Jobs -->|"crawl tasks"| External
    Jobs -->|"use queue"| Redis

    Services -->|"payment calls"| Stripe
    Services -->|"chat calls"| AI

    Models -->|"persist data"| DB
    Models -->|"store files"| Storage

    %% Click Events
    click Browser "https://github.com/huonggiangtms/e_commerce_rails/blob/main/app/frontend/entrypoints/application.js"
    click Browser "https://github.com/huonggiangtms/e_commerce_rails/tree/main/app/frontend/javascript/controllers/"
    click Browser "https://github.com/huonggiangtms/e_commerce_rails/blob/main/app/views/pwa/manifest.json.slim"
    click Browser "https://github.com/huonggiangtms/e_commerce_rails/blob/main/app/views/pwa/service-worker.js"

    click Puma "https://github.com/huonggiangtms/e_commerce_rails/blob/main/config/puma.rb"
    click Puma "https://github.com/huonggiangtms/e_commerce_rails/blob/main/Procfile.dev"
    click Puma "https://github.com/huonggiangtms/e_commerce_rails/tree/main/Dockerfile"

    click Controllers "https://github.com/huonggiangtms/e_commerce_rails/blob/main/app/controllers/application_controller.rb"
    click Controllers "https://github.com/huonggiangtms/e_commerce_rails/tree/main/app/controllers/"

    click Views "https://github.com/huonggiangtms/e_commerce_rails/blob/main/app/views/layouts/application.html.slim"
    click Views "https://github.com/huonggiangtms/e_commerce_rails/tree/main/app/views/"

    click Models "https://github.com/huonggiangtms/e_commerce_rails/tree/main/app/models/"
    click Models "https://github.com/huonggiangtms/e_commerce_rails/blob/main/config/database.yml"

    click Services "https://github.com/huonggiangtms/e_commerce_rails/tree/main/app/services/"
    click Services "https://github.com/huonggiangtms/e_commerce_rails/blob/main/config/initializers/stripe.rb"

    click Mailers "https://github.com/huonggiangtms/e_commerce_rails/tree/main/app/mailers/"

    click Jobs "https://github.com/huonggiangtms/e_commerce_rails/tree/main/app/jobs/"
    click Jobs "https://github.com/huonggiangtms/e_commerce_rails/blob/main/config/initializers/sidekiq.rb"
    click Redis "https://github.com/huonggiangtms/e_commerce_rails/blob/main/dump.rdb"

    click Storage "https://github.com/huonggiangtms/e_commerce_rails/blob/main/config/storage.yml"

    click Stripe "https://github.com/huonggiangtms/e_commerce_rails/blob/main/config/initializers/stripe.rb"

    click AI "https://github.com/huonggiangtms/e_commerce_rails/blob/main/app/services/ai_studio_client.rb"
    click AI "https://github.com/huonggiangtms/e_commerce_rails/blob/main/app/services/chat_service.rb"

    click External "https://github.com/huonggiangtms/e_commerce_rails/blob/main/app/services/product_crawler.rb"
    click External "https://github.com/huonggiangtms/e_commerce_rails/blob/main/app/models/crawled_product.rb"

    %% Styles
    classDef client fill:#D0E8FF,stroke:#0366D6,color:#0366D6;
    classDef backend fill:#FFE4CC,stroke:#D2691E,color:#D2691E;
    classDef data fill:#FFF5BA,stroke:#E1AD01,color:#E1AD01;
    classDef third fill:#EDEDED,stroke:#6A737D,color:#6A737D;
```
