Skip to content

Domain Model

Tất cả schema được định nghĩa trong @nx/core/src/models/schemas/{public,allocation}/ và được re-export. Bảng catalog nằm trong schema public; bảng allocation nằm trong schema allocation. Cột đa hình dùng generatePrincipalColumnDefs; cột chung qua generateCommonColumnDefs<TMetadata>().

1. ERD đầy đủ

2. Cột chung

CộtTypeGhi chú
idtextPK, Snowflake
identifiertextMã con người có tiền tố (P, PV, M, …)
createdAt / modifiedAttimestamptz
createdBy / modifiedBytextAudit người dùng
deletedAttimestamptzSoft-delete
metadatajsonbTúi mở rộng có kiểu ($type<TMetadata>())

3. Thực thể

3.1 Product

Thuộc tínhGiá trị
BảngProduct (public)
Nguồncore/.../public/product/schema.ts
Soft-delete
TrườngTypeBắt buộcMô tả
slugtextUnique partial theo merchantId
statustextProductStatuses; mặc định ACTIVATED
merchantIdtextChủ sở hữu
taxGroupIdtextRef nhóm thuế (được taxation tiêu thụ)
parentIdtextPhân cấp / nhóm variant
referenceIdtextRef ngoài / đồng bộ

3.2 ProductVariant

Thuộc tínhGiá trị
BảngProductVariant (public)
Nguồncore/.../public/product-variant/schema.ts
TrườngTypeBắt buộcMô tả
slugtext
isDefaultbooleanVariant mặc định của product (mặc định false)
dateFrom / dateTotimestamptzKhoảng hiệu lực
statustextProductVariantStatuses; mặc định ACTIVATED
typetextProductVariantTypes; mặc định STORABLE — bộ phân biệt cấu trúc
uomjsonbIUomRole { base, purchase, sale }
productIdtextProduct cha

type điều khiển tồn kho & gói — xem §4.1. Giá không lưu ở đây (nằm trong @nx/pricing).

3.3 ProductInfo / ProductIdentifier

ProductInfo (đa hình generatePrincipalColumnDefs — principal = Product hoặc ProductVariant): name (i18n), description (i18n).

ProductIdentifier (đa hình): scheme (TProductIdentifierSchemeSYSTEM / SKU / BARCODE / …), identifier (text). Scheme SYSTEM được tự sinh khi tạo aggregate.

3.4 ProductOption / ProductOptionValue / ProductVariantOption

BảngTrường chính
ProductOptionproductId, key, name (i18n), sequence
ProductOptionValueoptionId, value, name (i18n), sequence
ProductVariantOptionproductVariantId, optionId, optionValueId — liên kết một variant với một giá trị cho mỗi option

3.5 ProductBundler

Thuộc tínhGiá trị
BảngProductBundler (public)
Nguồncore/.../public/product-bundler/{schema,constants}.ts
TrườngTypeBắt buộcMô tả
typetextProductBundlerTypes COMBO / ADDON / FBT; mặc định COMBO
leadVariantIdtextVariant host / combo
relatedVariantIdtextThành phần / addon / gợi ý
quantitynumericMặc định 1
basistextProductBundlerBasises — ghi đè giá FBT; null trên COMBO/ADDON
basisValuenumericSố tiền/phần trăm ghi đè
sequenceintSắp xếp (mặc định 0)

Unique partial trên (type, leadVariantId, relatedVariantId). Bảng đơn này biểu diễn quan hệ COMBO/ADDON/FBT — chúng không phải là variant type. Xem ADR-0003.

3.6 Merchant

Thuộc tínhGiá trị
BảngMerchant (public)
Nguồncore/.../public/merchant/{schema,constants}.ts
TrườngTypeBắt buộcMô tả
slugtextUnique partial theo organizerId
name / descriptioni18nname ✓
statustextMerchantStatuses; mặc định ACTIVATED
currencytextMặc định VND
businessTypetextBusinessTypes; mặc định HOUSEHOLD
industrytextMerchantIndustry; mặc định FNB
isHeadquarterbooleanMặc định false
locationjsonbhelper location()
taxMethodtextTaxMethods (ví dụ DIRECT)
parentIdtextPhân cấp
organizerIdtextChủ sở hữu
metadata.taxjsonb{ taxCode, cityCode?, districtCode?, wardsCode?, fullName?, addressLine? } — thu thập MST (CDC → TaxInfo)
metadata.eInvoice[]jsonbthông tin đăng nhập provider hóa đơn điện tử
metadata.finance.accounts[]jsonbtài khoản tài chính được seed
metadata.onboardingjsonbPartial<Record<TMerchantOnboardingStep, boolean>>

3.7 Organizer

slug (unique), identifier, status (OrganizerStatuses; mặc định ACTIVATED), name (i18n), description (i18n), location, parentId, headquarterMerchantId.

3.8 SaleChannel / SaleChannelProduct

SaleChannel: slug, identifier, status (mặc định ACTIVATED), name (i18n), description (i18n), merchantId, parentId.

SaleChannelProduct (join): productId, saleChannelId. Bị xóa cascade bởi DeletionPolicyService khi một kênh bị gỡ.

3.9 Category

TrườngTypeBắt buộcMô tả
name / descriptioni18nname ✓
statustextCategoryStatuses
discriminationTypetextTCategoryDiscriminationType (nhãn nhóm FE)
identifiertext
merchantIdtextnull ⇒ category SYSTEM (dùng chung theo businessType)
parentIdtextPhân cấp tự tham chiếu

Category.type chỉ là nhóm FE — nó không điều khiển hành vi tồn kho/bán.

3.10 Configuration

TrườngTypeBắt buộcMô tả
principalType / principalIdtextPhạm vi chủ sở hữu (null ⇒ SYSTEM)
codetextMã config; định dạng tích hợp provider {type}:{provider}:{action}:{credentialType}
grouptextConfigurationGroups
statustextConfigurationStatuses
environmenttextPhạm vi theo môi trường
credentialtextCiphertext AES-256-GCM (che trong response)

Unique partial trên (group, code, principalId, principalType, environment).

3.11 Device / DiscriminationType / ReceiptTemplate / Setting

BảngTrường chính
Devicestatus (mặc định NEW), name (i18n), code, type, merchantId, hardwareInfo/softwareInfo (jsonb), pin, vendor
DiscriminationTypescope, status, name (i18n), type, parentId, merchantId
ReceiptTemplateprincipal đa hình, name, locale, paperWidth, fontSize, isDefault, content (jsonb TReceiptContent)
Settingprincipal đa hình, status, túi giá trị metadata có kiểu

3.12 Allocation (schema allocation)

BảngTrường chính
AllocationLayoutsơ đồ mặt bằng cấp cao nhất
AllocationZonename (i18n), layoutId, parentId (tự tham chiếu), style (jsonb)
AllocationUnitname (i18n), zoneId, placement/style (jsonb), capacity, status

AllocationUsage (mức chiếm dụng) do @nx/sale sở hữu và thay đổi; commerce chỉ sở hữu layout tĩnh.

4. Enum Status & Type

4.1 ProductVariantTypes

Nguồn: core/.../public/product-variant/constants.ts.

Giá trịConstLưu khoBOM
000_STORABLESTORABLE
100_CONSUMABLECONSUMABLE
200_SERVICESERVICE
300_KITKIT✓ (qua Material)
301_COMBOCOMBO✓ (qua ProductVariant)
400_MANUFACTUREDMANUFACTURED

Tập helper: STOCKABLE_SET = {STORABLE, MANUFACTURED}, BOMABLE_SET = {KIT, COMBO, MANUFACTURED}.

4.2 ProductBundlerTypes

Giá trịConstÝ nghĩa
000_COMBOCOMBOrelatedVariantId là thành phần của combo
100_ADDONADDONaddon miễn phí gắn vào variant host
200_FBTFBTgợi ý "thường được mua cùng nhau" (có hướng)

4.3 Status vòng đời

EnumGiá trị
ProductStatuses / ProductVariantStatusesDRAFT, ACTIVATED, DEACTIVATED, ARCHIVED
MerchantStatuses / OrganizerStatuses / SaleChannelStatusestái dùng Statuses gồm ACTIVATED (mặc định)
MerchantOnboardingStepsBUSINESS, FINANCE_ACCOUNT (300), TAX_INFO (400), PRODUCT

5. Bất biến xuyên thực thể

Bất biếnThực thi
Product aggregate là atomic (Product + Info + SYSTEM identifier + liên kết kênh + variant mặc định)Một TX duy nhất trong ProductCreateService
Merchant aggregate atomic (merchant + policy + identifier + categories + channels)Một TX duy nhất trong MerchantService
Onboarding tạo Organizer + Merchant + 2 PolicyDefinitions + SaleChannel mặc định một cách atomicTX OrganizerService.onBoarding
Product.slug unique theo merchantId; Merchant.slug unique theo organizerIdDB unique partial (deletedAt IS NULL)
Sao chép sản phẩm đa merchant chỉ chạy khi syncMerchantIds.length > 0Bảo vệ ProductAggregate*Listener.pushJobToQueue
Thông tin thuế merchant nằm trong metadata.tax, không phải cột; TaxInfo có thẩm quyền downstreamMerge metadata MerchantService → CDC
Thông tin đăng nhập provider không bao giờ trả về plaintextEncryptService + giá trị hiển thị được che
Tính duy nhất của Configurationunique partial (group, code, principalId, principalType, environment)
Hàng ProductBundler unique theo (type, leadVariantId, relatedVariantId)DB unique partial

6. Hành vi Soft-delete

Thực thểSoft-deleteGhi chú
Tất cả thực thể commerceđánh dấu deletedAt; unique partial loại trừ hàng đã soft-delete
Bảo vệ xóaDeletionPolicyService chặn xóa khi có lịch sử bán / chính sách nghiêm ngặt; archive thay vì xóa

7. Trang liên quan

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.