Toàn bộ công việc cần làm để chuyển hệ thống hiện tại sang nền tảng dữ liệu lao động sống multi-tenant SaaS, với AI Core, Trust Layer và Financial Services.
Hệ thống V3 đang production-stable, 13,500+ PHP files + 1,500+ TS/TSX files, 4 databases, 6 services độc lập.
Laravel 12 API · api.xanhvina.com.vn
Laravel 12 + React SPA · admin.xanhvina.com.vn
Next.js 16 · xanhvina.com.vn
Expo 55 + RN 0.83
NestJS 11 + MongoDB
React + zmp-sdk · Bắc Ninh only
VIỆC XANH X = nền tảng; VIỆC XANH 247 = app cho công nhân. Cùng lõi dữ liệu, khác trải nghiệm.
Multi-tenant SaaS cho nhà máy + nhà cung ứng
Nền tảng số trung tâm với kiến trúc 5 nhóm module + AI core cắm ngang + Trust Layer + Workflow Engine + Integration Hub.
Personal Labor OS — không chỉ app tìm việc
Ứng dụng trung tâm cho công nhân với 6 lớp sản phẩm. Hồ sơ lao động số là tài sản chiến lược lõi.
Mapping 36 modules V4 yêu cầu với hiện trạng: 10 HAVE + 18 PARTIAL + 8 MISSING.
Đã có production · cần adapt tenant layer
Có một phần · cần mở rộng/refactor
Chưa có · cần build from zero
| # | V4 Requirement | Hiện trạng | Severity |
|---|---|---|---|
| 1 | Multi-tenant SaaS (tenant_id trên mọi table) | ❌ Single-tenant hoàn toàn | CRITICAL |
| 2 | Trust Layer (audit, snapshots, disputes, version) | ⚠ 30% (ActivityLog only) | CRITICAL |
| 3 | Ứng lương (salary advance) trên mobile | ❌ Chưa có | CRITICAL |
| 4 | AI Advisor ngôn ngữ tự nhiên (mobile) | ❌ Chưa có | CRITICAL |
| 5 | Workflow Engine (state machine framework) | ⚠ 25% (FSM scattered) | HIGH |
| 6 | AI Core Central (routing + permission boundary) | ⚠ 40% (chat-service isolated) | HIGH |
| 7 | Integration Hub (cross-tenant data sync) | ❌ Chưa có | HIGH |
| 8 | Ví quyền lợi (unified wallet UI) | ⚠ Stubs only | HIGH |
| 9 | Hồ sơ số 9 cấu phần hoàn chỉnh | ⚠ 40% built | HIGH |
| 10 | Formal vs Seasonal job logic split | ❌ Chưa phân biệt | MEDIUM |
PostgreSQL 16 · Tất cả bảng có tenant_id (trừ worker_accounts cross-tenant) · Money = NUMERIC(19,4).
V4 change: 358 permissions từ config → DB
🟢 KEEP 70% từ V3 Worker (21 models, 23 services)
Scale: ~30M records/tháng · Partition by work_date
⚠ Financial-grade — NUMERIC(19,4), invariants enforced
Critical: có thể replay bất kỳ entity state tại thời điểm xưa — foundation cho financial disputes
Quyết định per-source · per-domain. Target ~60-65% code reuse thay vì greenfield 100%.
| Source | KEEP | ADAPT | REWRITE | DROP | Verdict |
|---|---|---|---|---|---|
| backend/ | 30% | 40% | 20% | 10% | Mostly adapt + reuse |
| backend-admin/ | 10% | 30% | 30% | 30% | Heavy rewrite + drop HRM |
| frontend/ | 40% | 35% | 15% | 10% | Reuse UI + BFF |
| xanhvina-mobile/ | 60% | 25% | 10% | 5% | Good foundation, expand |
| chat-service/ | 30% | 50% | 20% | 0% | Refactor → AI Core |
| zalo-mini-app/ | 0% | 0% | 0% | 100% | Sunset entirely |
| Domain | Quy mô | Decision | V4 Target |
|---|---|---|---|
| Job | 16 models · 26 services · 21 controllers | KEEP 80% | M3 Recruitment + M4 (Dispatch FSM 10 states đã tested) |
| Worker | 21 models · 23 services · 34 controllers | KEEP 70% | M4 Worker Lifecycle |
| Finance | 22 models · 28 services · 12 controllers | KEEP 80% | M6 Payroll + Gap E Financial Products |
| Incentive | 10 models · 9 services · commission engine | KEEP 80% | M8 Supplier + M10 Reconciliation |
| Attendance | 9 models · 4 services | KEEP 80% | M5 Attendance |
| Employer · EmployerUser · Cluster · Partner | Multiple | ADAPT | M1 Tenant + M2 IAM |
| WorkerManagement · Infrastructure | Shared utilities | ADAPT | M4 + M7 |
| Shared | Framework-level | KEEP | Shared kernel |
| Dashboard · Application | Single-tenant aggregators | REWRITE | M11 AI Analytics |
| Housing | 9 models · 14 services | DROP | Not in V4 brief |
| Community | 16 models · 18 services | DROP | Not in V4 brief |
| Content | 6 models · 5 services | DROP | Move to headless CMS |
| Factory (V2 alias) | Backward-compat | DROP | Không cần |
🟢 KEEP: Design system (306 components), form patterns, charts, DnD, TipTap, hooks boilerplate
🟡 ADAPT: 164 SWR hooks (add tenant), routing → /t/:slug/admin, auth store
🔴 REWRITE: Dashboard aggregation, PublicApiClient (20+ TODOs), permission seeding
⚫ DROP: HRM pages (60 pages, 35 models) — biggest drop
🟢 KEEP: Next.js 16 App Router, 251 UI components, BFF pattern, HttpOnly cookies, ISR
🟡 ADAPT: Routes Vietnamese → English, tenant context for employer ops, SWR hooks
🔴 REWRITE: 3 portal auth unification (single JWT)
⚫ DROP: Community pages, hardcoded route translations
🟢 KEEP: Expo Router, SWR, Zustand stores, SecureStore, MMKV, 61 tests, Firebase
🟡 ADAPT: Auth store (tenant context), API client (tenant header)
🔴 REWRITE: Finance screens, Documents screen
🆕 ADD: AI Advisor tab, Financial Services tab, Leave flow, QR profile
Split: VIỆC XANH 247 (worker) + VIỆC XANH Business (employer)
🟢 KEEP: NestJS + Fastify, Socket.io + Redis, BullMQ, MongoDB schema, 3 LLM providers, moderation, admin SPA
🟡 ADAPT: Rename → ai-service, unify JWT, tenant context, expose AI as REST
🆕 ADD: 5 new AI agents (recruitment, worker mgmt, payroll, analytics, executive)
⚫ DROP: Nothing — all valuable
Phase 1 là MVP có thể bán. Phase 4 mở financial services — doanh thu bền vững.
MVP bán được · Tuyển dụng + quản lý CN · Multi-tenant từ đầu
| Module | Tables | Mục tiêu | Reuse | Effort |
|---|---|---|---|---|
| M1 Tenant & Org | 9 | Tenant CRUD, RLS, sub-domain routing, onboarding | 0% (new) | 6-8w |
| M2 IAM | 7 | Sanctum 4-guard, JWT unified, permission DB | 60% | 4-5w |
| M3 Recruitment | 10 | Jobs, applications, interviews, Dispatch FSM port | 70% | 6-7w |
| M4 Worker Lifecycle | 8 | Workers, employments, milestones, leave, referral | 70% | 5-6w |
Dữ liệu lao động sống · Trust Layer · Workflow Engine
| Module | Tables | Mục tiêu | Reuse | Effort |
|---|---|---|---|---|
| M5 Attendance | 8 | Chấm công, bảng công, import, anomaly AI, partition | 80% | 5-6w |
| M6 Payroll | 8 | Tính lương, phiếu lương, ứng lương, approvals | 80% | 6-7w |
| M7 Platform Infra | 6 | Audit, events, notifications, system settings | 50% | 3-4w |
| Gap A Workflow Engine | 3 | State machine framework reusable | 0% (new) | 4-5w |
| Gap G Entity Versioning | 1 | Snapshot + replay foundation | 0% (new) | 2w |
Mở rộng NCC + Nhà máy · Đối soát tài chính · Integration Hub
| Module | Tables | Mục tiêu | Reuse | Effort |
|---|---|---|---|---|
| M8 Supplier Network | 6 | Recruiters, CTV, vendors, commission payouts | 70% | 5-6w |
| Gap H CTV Tree | 1 | Materialized path cho multi-level commission | 0% (new) | 2w |
| M9 Factory Operations | 5 | Manpower plan, supplier eval, raw clock data | 20% | 4-5w |
| M10 Reconciliation | 7 | Đối soát NM ↔ NCC, disputes, settlement | 60% | 5-6w |
| Gap B Integration Hub | 3 | Cross-tenant sync rules + webhooks | 0% (new) | 4-5w |
Ứng dụng lao động số hoàn chỉnh · AI Core · Financial Services
| Module | Tables | Mục tiêu | Reuse | Effort |
|---|---|---|---|---|
| M11 AI & Intelligence | 5 | Sessions, messages, recommendations, action logs | 80% | 3-4w |
| Gap C AI Knowledge Base | 3 | Documents, pgvector embeddings, AI tools registry | 30% | 3-4w |
| Gap D Chat & Messaging | 4 | Conversations, participants, messages, reactions | 70% | 3w |
| M12 Worker App + Gap F Digital Profile | 10 | VX247 mobile, 9 cấu phần hồ sơ số | 75% | 6-8w |
| Gap E Financial Products | 4 | Loans, insurance, wallet, ứng lương UI | 50% | 5-6w |
92 tables · 4 phases · 10-13 tháng · 7-8 devs · ~60-65% code reuse
Phase 1 là MVP có thể bán (32 tables · 3 tháng). Phase 2 hoàn thiện dữ liệu sống. Phase 3 mở ecosystem. Phase 4 tạo doanh thu bền vững từ tài chính + AI.
Chia work theo tracks độc lập để giảm contention và maximize throughput.
2 Backend + 1 QA · 4 months
2 Backend + 1 QA · 5 months
1 FE + 1 SPA + 2 Mobile + 1 QA · 6 months
Top risks với impact + probability + mitigation.
| # | Risk | Impact | Probability | Mitigation |
|---|---|---|---|---|
| 1 | Tenant data leak — query scoping bug cross-tenant | CRITICAL | MEDIUM | RLS policies + integration tests 2+ tenants + penetration test pre-launch |
| 2 | State machine refactor break existing dispatches | HIGH | MEDIUM | Event sourcing migration + replay capability + canary rollout |
| 3 | Permission config → DB migration break existing admin | HIGH | LOW | Dual-read pattern (DB first, fallback config) + feature flag rollback |
| 4 | Attendance partition performance trên 30M+ rows/tháng | MEDIUM | MEDIUM | Composite index (tenant_id, work_date) + query plan review + monitoring |
| 5 | AI cost spiral khi user tăng 10x | MEDIUM | HIGH | Per-tenant rate limits + cache aggressively + route cheap LLM (DeepSeek) |
| 6 | Financial services compliance (loans, insurance) | HIGH | MEDIUM | Partner-first launch (no license needed), legal review, conservative eligibility |
| 7 | Chat-service AI duplicate business logic với backend | MEDIUM | HIGH | Clear boundary: AI = advisory, backend = source of truth. Contract review. |
| 8 | Team scaling không kịp — hiện chỉ có ~3 devs | HIGH | HIGH | Phase-based hiring: 4 devs P1, peak 7-8 P2-P3, giảm dần P4 |
Sẵn sàng kick off ngay khi team + canary tenant được xác nhận.
| File | Nội dung |
|---|---|
| 00-cover-and-summary.md | Executive summary |
| 01-v4-complete-schema.md | 92 tables chi tiết + design decisions |
| 02-keep-drop-rewrite-map.md | Per-source per-domain decisions |
| visuals/v4-migration-work-breakdown.html | Tài liệu này — scrollable overview |
| /Users/trungnguyen/Sites/xanhvina/ | Backup 9.9GB · 6 git repos với full history |
Backup đã xong. Schema đã thiết kế. Kế hoạch đã rõ. Chỉ cần quyết team size và canary tenant để kick off Phase 1.
VIỆC XANH X V4 = hạ tầng dữ liệu lao động sống, AI-first, financial-ready.