Skip to content

System Model (DocHub V3)

Mô hình 3 tầng của hệ thống FitZalo V2. Tham chiếu quyết định: DEC-001 (Auth/Tenant), DEC-003 (RBAC).

Architecture Overview

┌─────────────────────────────────────────────────────────────────┐
│                     PORTAL GOVERNOR (Web)                       │
│  React/Vite · apps/v2-portal · 60+ pages                       │
│  Roles: System Admin, Tenant Owner/Admin/Operator               │
│  Auth: Email/Password + Zalo OAuth → select tenant → operate   │
├─────────────────────────────────────────────────────────────────┤
│                    UTILITY RUNTIME (API)                         │
│  NestJS · apps/api · 22 modules · 30 controllers                │
│  Guard Stack: JwtAuth → Tenant → Permissions                    │
│  Data: MongoDB (Mongoose) · 57+ schemas                         │
│  Bus: EventEmitter2 (in-process events)                         │
├─────────────────────────────────────────────────────────────────┤
│                    MINIAPP (Zalo Platform)                       │
│  Zalo Mini App · miniapp_v2                                     │
│  Auth: Zalo OAuth → tenant bind → end-user features             │
│  Scope: Customer-facing (catalog browse, order, loyalty, ...)   │
└─────────────────────────────────────────────────────────────────┘

Layer 1: Portal Governor

Entry point: apps/v2-portal/ (React + Vite + TypeScript)

ResponsibilityDetails
AuthenticationLogin → JWT (no tenant) → Workspace (select tenant) → Dashboard
Tenant ManagementCreate/switch/suspend tenants. Route: /workspace
Business OperationsCatalog, Orders, Inventory, CRM, CMS, Vouchers, ...
System AdminMDM, Billing, Plugin Store, Global Settings
RBACUI guards based on PermissionString. Hide/show features per role

Key Pages (60+)

Dashboard, Login, Workspace, ProductList/Form, OrderList/Detail, InventoryDashboard, TeamManagement, VoucherList/Form, CustomerList/Detail, PartnerList/Detail, CMS, EmailSettings, UnifiedInbox, InternalComms, ZaloCenter, PluginStore, Settings, Admin/*

Layer 2: Utility Runtime (Backend API)

Entry point: apps/api/ (NestJS + MongoDB)

Module Map (22 modules in libs/modules/)

DomainModuleControllersKey Entities
IAMiamauth, tenant, user, role, membership, invitationUser, Tenant, TenantSettings, Role, Permission, Membership, Identity, Invitation
Catalogcatalogproduct, categoryProduct, Category
Commerceecommerceorder, cart, refundOrder, Cart, Refund
VouchervouchervoucherVoucher
InventoryinventoryinventoryInventory, StockAdjustment, StockLocation, StockMove, StockPicking, StockQuant
ProcurementprocurementprocurementPurchaseOrder
ProductionproductionproductionProductionOrder
ShippingshippingshippingShippingOrder
CMScmscmsArticle
Comms (ESN)commscommsCommsPost, CommsEvent, CommsKnowledge
Emailemailemail-account, email-template, notificationEmailAccount, EmailTemplate, EmailOutbox, InAppNotification
Partner/CRMpartnerpartnerPartner
InboxinboxinboxConversation, Message
AIaiaiKnowledgeBase, AiDocument, AiProfile, DataSourceConfig
LoyaltyloyaltyloyaltyCommissionRule, Payout, PointTransaction, TierConfig
Customer Loyaltycustomer-loyaltycustomer-loyaltyMembershipCondition
BalancebalancebalanceWallet, Transaction
Billingbillingsubscription, usageSubscription, Usage, Plan, Feature
MDMmdmmdmGlobalAttribute, GlobalCarrier, GlobalUom, Location, Registry
PluginpluginpluginPluginDef, InstalledPlugin
ZalozalozaloZaloConfig
Auditaudit(interceptor)AuditLog
Socketsocket(gateway)

Auth Flow (xem DEC-001)

POST /v2/iam/auth/login          → JWT {sub: userId}        (no tenant)
POST /v2/iam/auth/switch-tenant  → JWT {sub, tenantId, role} (tenant set)
GET  /v2/iam/auth/me             → Current user info
POST /v2/iam/auth/zalo-login     → Portal Zalo flow         (DEC-002)
POST /v2/iam/auth/zalo-tenant-login → MiniApp Zalo flow     (DEC-002)

Guard Stack

Request → JwtAuthGuard → TenantGuard → PermissionsGuard → Controller
           │                │               │
           │                │               └─ Check @Permissions()
           │                └─ Skip if @SkipTenantCheck()
           └─ Skip if @Public()

Layer 3: MiniApp (Zalo Platform)

Entry point: miniapp_v2/

ResponsibilityDetails
AuthZalo OAuth → tenant bind → end-user JWT (DEC-002)
Catalog BrowseView products, categories, promotions
OrderingCart, checkout, order tracking
LoyaltyPoints, tiers, rewards
ProfileUser profile, order history

Boundaries & Rules

  1. Portal ↔ API: Portal calls API via HTTP. Auth via JWT Bearer token.
  2. MiniApp ↔ API: MiniApp calls same API but with tenant-bound JWT.
  3. Tenant Isolation: All business data is tenant-scoped. TenantGuard enforces.
  4. Cross-Tenant: Only SYSTEM_ADMIN can access cross-tenant data (with @SkipTenantCheck()).
  5. Event Bus: Internal events (EventEmitter2) — not cross-service.

Tech Stack

LayerTechnology
FrontendReact 18, Vite, TypeScript, Ant Design
BackendNestJS 10, TypeScript, Mongoose/MongoDB
AuthPassport JWT, bcrypt
BuildNx Monorepo
MiniAppZalo Mini App SDK

FitZalo Platform Documentation