Appearance
Tổng quan Hệ thống (Codebase Analysis)
Thông tin nhanh
- Dự án: service-crm (SaleHay)
- Loại hình: Nền tảng CRM + Affiliate Marketing cho ngành dịch vụ
- Ngôn ngữ: JavaScript, HTML, CSS, SQL
- Stack: Vite, React, TailwindCSS v4, Cloudflare Workers/Pages, Neon PostgreSQL, MCP SDK
- Quy mô: ~50.000+ dòng code, 5 ứng dụng, 30 migration files
Xem chi tiết: Kiến trúc · Database · Deployment
1. Kiến trúc 5 Tầng
Hệ thống gồm 5 ứng dụng phối hợp qua Cloudflare Workers API:
| Ứng dụng | Domain | Chức năng | Xác thực |
|---|---|---|---|
| Admin | app.salehay.com | CRM dashboard — leads, bookings, customers, reports | JWT (tenant-scoped) |
| Landing | salehay.com | Marketing — 8 industry pages, affiliate program, playbooks | Public |
| Portal | portal.salehay.com | Quản lý multi-org — billing, affiliates, invoices, tax | JWT (org-scoped) |
| Affiliate | aff.salehay.com | Cổng CTV — dashboard, referrals, earnings, links | JWT (affiliate-scoped) |
| MCP Server | Local (stdio) | AI Agent bridge — 7 CRM tools qua MCP Protocol | Agent Token |
2. Cấu trúc thư mục
service-crm/
├── admin/ # Admin Dashboard (React + Vite + TailwindCSS v4)
│ ├── src/pages/ # 16 pages + settings/ + landing/
│ ├── src/components/ # Shared components
│ └── src/lib/ # API client, auth context
├── landing/ # Marketing Landing (React + Vite)
│ └── src/pages/landing/ # 19 pages (8 industries + affiliate + playbooks)
├── portal/ # Multi-Org Portal (React + Vite)
│ └── src/pages/ # 11 pages (orgs, billing, affiliates, tax)
├── aff/ # Affiliate Portal (React + Vite)
│ └── src/pages/ # 7 pages (dashboard, referrals, earnings, links, profile)
├── api/ # Backend API (Cloudflare Workers)
│ ├── src/routes/ # 23 route files
│ ├── src/lib/ # webhook-emitter, tracking
│ ├── src/auth.js # JWT + API Key authentication
│ ├── src/agent-auth.js # Agent Token authentication
│ └── src/rbac.js # Role-Based Access Control
├── mcp-server/ # MCP Server cho AI Agent
├── openfang-hand/ # OpenFang HAND config
├── skills/ # CRM Skills (operations, outreach, analytics)
├── schema/ # Database migrations (001-030)
├── docs/ # Tài liệu nguồn
├── docs-public/ # Tài liệu công khai (Astro Starlight)
└── docs-internal/ # Tài liệu nội bộ (Astro Starlight)3. Các module Backend API (23 routes)
| Nhóm | Routes | Xác thực | Mô tả |
|---|---|---|---|
| Auth | auth, auth-advanced | JWT | Login, register, OAuth, OTP, team, roles |
| CRM Core | leads-v2, customers, bookings | JWT | CRUD + pipeline, assign, convert |
| Telesales | call-logs, call-scripts | JWT | Ghi nhận cuộc gọi, kịch bản |
| Catalog | catalog, services-list | JWT | Categories, services, products, packages |
| Payments | payments | JWT | Thanh toán, bank accounts, VietQR |
| Reports | dashboard, reports, activities | JWT | Funnel, cohort, marketing, export |
| Public | public-leads, public-pages, queue, reviews | API Key / Public | Lead intake, booking, queue, reviews |
| Agent | agent-api, agent-tokens, webhooks | Agent Token | 7 AI agent endpoints + webhooks |
| Resources | resources | JWT | Tài liệu download |
4. Database Schema (30 Migrations)
| Migration Group | Files | Tables chính |
|---|---|---|
| Core CRM | 001-009 | admin_users, tenants, customers, services, bookings, leads, reviews, queue, call_logs |
| Seed Data | 010-018 | Dữ liệu mẫu cho 7 ngành (dental, nail, hair, beauty, gym, pet, clinic, spa) |
| Auth & Public | 016-019 | organizations, users, public_pages |
| Agent Integration | 020 | agent_tokens, webhook_subscriptions, webhook_delivery_log |
| Billing | 021-023 | billing, einvoice, tax_reports |
| Security | 024 | audit_trail |
| Platform Admin | 025 | platform_admin tables |
| Affiliate | 026-029 | affiliates, aff_campaigns, aff_links, aff_clicks, aff_referrals, aff_commissions, aff_payouts, org_affiliates |
| Catalog | 030 | service_categories, products, service_packages, package_items, price_history |
Xem chi tiết: Database Schema
5. Luồng dữ liệu chính
6. Công nghệ sử dụng
| Thành phần | Stack | Giá trị |
|---|---|---|
| 4 Frontends | React / TailwindCSS v4 / Vite | SPA, deploy Cloudflare Pages |
| Backend | Cloudflare Workers | Edge computing, auto-scale, zero cold-start |
| Database | Neon PostgreSQL | Serverless, auto-scale, branching |
| MCP Server | @modelcontextprotocol/sdk | Chuẩn giao tiếp AI Agent ↔ tools |
| AI Agent | OpenFang / OpenClaw | Tự động hóa sales pipeline |
| Affiliate | Custom (7 tables) | CTV/KOL/KOC referral + commission tracking |
| Catalog | Custom (6 tables) | Categories, services, products, packages, price history |