Skip to content

Sale Service

@nx/sale sở hữu vòng đời giỏ hàng và đơn hàng: một SaleOrder đảm nhận cả hai vai trò (giỏ hàng DRAFT + đơn hàng đã chốt). Nó nhận webhook MQ-Pay cho sự kiện thanh toán, điều phối luồng kitchen ticket, quản lý allocation và reservation cho ăn tại chỗ, đồng thời phát các topic Kafka + sự kiện WebSocket tới inventory, finance và các UI phía sau.

1. Tham chiếu nhanh

Thuộc tínhGiá trị
Package@nx/sale
CodeSVC-00030-SALE
LoạiMicroservice
RuntimeBun
Base ClassVerifierApplication
Vị trípackages/sale
Base Path/v1/api/sale
Dev Port31030
Container Port3000 (external 31030)
Snowflake ID3
DB Schemasale (đa bảng - xem Domain Model)
Binding Namespace@nx/sale

2. Mục đích & Phạm vi

Bao gồmLoại trừ
Một entity SaleOrder cho giỏ hàng + đơn hàng (DRAFT → … → COMPLETED / CANCELLED)Thay đổi tồn kho (uỷ quyền cho @nx/inventory)
Chế độ item: PRODUCT (tự gộp) và CUSTOM (luôn tạo dòng mới)Tích hợp nhà cung cấp thanh toán (uỷ quyền cho @nx/mq-pay)
Định giá lúc checkout qua HTTP call tới @nx/pricingBộ máy tính thuế
Xử lý webhook thanh toán (MQ-Pay HTTP webhook)In hoá đơn (thuộc về frontend)
Tách bill: SaleCheck (nhóm thanh toán độc lập)Bộ máy tích điểm loyalty
Gộp / tách order (có thể đảo ngược)Menu hướng tới khách hàng
Vòng đời kitchen ticket (FOH/BOH) - void, rush, ready, servedDữ liệu công thức / BOM (thuộc @nx/inventory)
Sử dụng allocation (bàn, khu vực, unit cho DINE_IN)
Reservation (đặt trước rồi đến)
Quản lý ca POS - Shift v2 (drawer, enrollment, báo cáo X/Z)
Cộng điểm khách hàng (loyalty) khi thanh toán thành công
Báo cáo bán hàng (tổng hợp ngày, theo sản phẩm/danh mục, tổng kết mua hàng)
Sự kiện WebSocket thời gian thực (phân phối nhiều room)

3. Tech Stack

External:

LibraryMục đích
@venizia/ignisIoC container, DI, BaseService, BaseController, ControllerFactory
@venizia/ignis-helpersLogger, KafkaProducerHelper, Redis helper
@platformatic/kafkaKafka client (sale chỉ dùng producer với mặc định của helper - không cấu hình idempotent/lz4 tường minh)
honoHTTP server (qua IGNIS)
@hono/zod-openapiSinh OpenAPI từ Zod schema
@scalar/hono-api-referenceOpenAPI explorer tại /doc
drizzle-ormTruy cập DB qua PostgresCoreDataSource
pgPostgreSQL driver
lodashTiện ích

Internal:

PackageMục đích
@nx/coreSchema, repository (re-export), VerifierApplication, KafkaTopics, SaleOrderStatuses, SaleOrderItemModes, SaleConstraints
@nx/pricingChỉ HTTP - PricingNetworkService gọi pricing service để lấy giá động lúc checkout

4. Cấu trúc Dự án

packages/sale/
├── src/
│   ├── application.ts                # VerifierApplication subclass
│   ├── index.ts                      # bootstrapApplication()
│   ├── migrate.ts                    # bootstrapMigration()
│   ├── common/
│   │   ├── constants.ts              # SaleConstraints, ApplicationRoles
│   │   ├── keys.ts                   # BindingKeys
│   │   ├── rest-paths.ts             # 13 RestPaths
│   │   ├── webhook-types.ts          # PaymentWebhookEventTypes + zod schemas
│   │   ├── websocket.ts              # 7 WebSocket topics + room helpers
│   │   ├── sale-check.constants.ts   # check splitting helpers
│   │   ├── shift.types.ts            # shift actor + context types
│   │   └── response.helper.ts        # response shaping
│   ├── components/
│   │   ├── kafka/                    # ApplicationKafkaComponent (chỉ producer)
│   │   └── websocket/                # ApplicationWebSocketComponent
│   ├── controllers/                  # 13 thư mục controller → 16 lớp controller được đăng ký
│   ├── datasources/                  # PostgresCoreDataSource
│   ├── migrations/processes/         # 3 migration (seed permission + role-permission, migration grant)
│   ├── models/                       # zod request/response schemas
│   ├── repositories/                 # re-exports từ @nx/core
│   └── services/                     # 20 services
├── package.json
└── tsconfig.json

5. Kiến trúc

Chi tiết: xem Architecture.

6. Tổng quan miền

ERD đầy đủ + bảng theo entity: xem Domain Model.

7. Tóm tắt Bề mặt

REST controllers - tham chiếu đầy đủ render trực tiếp từ /v1/api/sale/doc/openapi.json (live spec - Scalar viewer tại /doc, gateway portal):

ControllerBase pathGhi chú
SaleOrderController/sale-orders+ lifecycle (draft, items, checkout, revert, cancel, merge, split)
SaleOrderItemController/sale-order-itemsCRUD + override để validate quantity
SaleCheckController/sale-checks+ split, split-equal, merge, rollback
SaleOrderCheckController/sale-orders/:id/checksnested dưới order
KitchenStationController/kitchen-stationsCRUD
KitchenTicketController/kitchen-ticketsCRUD
KitchenTicketSaleOrderController/sale-orders/:id/kitchen-tickets/...sendToKitchen
KitchenTicketActionController/kitchen-tickets/:id/...void / rush / mark-ready / mark-served
KitchenTicketItemActionController/kitchen-ticket-items/:id/...start-cooking / mark-ready / mark-served / void
AllocationUsageController/allocation-usagesCRUD + batch complete/cancel
ReservationController/reservations+ create with allocation
ShiftController/shiftsopen / current / list / cash-movement / close / reconcile drawer / báo cáo X+Z
CustomerController/customersCRUD
PointTransactionController/point-transactionsCRUD (đa số là đọc)
SalesReportController/reports/salesTổng hợp ngày, theo product, theo category, purchase-summary
PaymentWebhookController/webhooks/paymentTiếp nhận sự kiện MQ-Pay (no auth)

Bề mặt bất đồng bộ - tham chiếu đầy đủ tại API Events:

HướngKênhSố lượng
ĐếnHTTP webhook1 endpoint × 6 loại sự kiện
Đi raKafka2 (PAYMENT_SUCCESS, KITCHEN_TICKET_ITEM_STATUS_CHANGED)
Đi raWebSocket7 topic × nhiều room helper (merchant/order/check/kitchen/allocation/reservation)

8. Components

ComponentFileMục đích
ApplicationKafkaComponentsrc/components/kafka/component.tsIdempotent producer; phát PAYMENT_SUCCESS + KITCHEN_TICKET_ITEM_STATUS_CHANGED (không có consumer)
ApplicationWebSocketComponentsrc/components/websocket/component.tsPhát WebSocket qua signal service; phân phối tới room
Redis cache (tùy chọn)bind tại BindingKeys.APPLICATION_REDIS_CACHECache permission cho authorization

9. Services

ServiceFileMô tả ngắn
SaleOrderServicesale.service.tsCRUD đơn DRAFT, add/clear item, cancel, archive
SaleOrderItemServicesale-order-item.service.tsCập nhật item theo lô với khoá cấp dòng + tính lại tổng kết
CheckoutServicecheckout.service.tsDRAFT → PROCESSING; pricing v1+v2; revert
SaleCheckServicesale-check.service.tsTách bill: split, split-equal, merge, rollback
OrderMergeServiceorder-merge.service.tsGộp nhiều draft order; có thể đảo ngược
OrderSplitServiceorder-split.service.tsTách một order thành nhiều; phân bổ lại item + allocation + pricing
KitchenTicketServicekitchen-ticket.service.tsGửi xuống bếp, void, rush, đánh dấu ready/served
KitchenTicketItemServicekitchen-ticket-item.service.tsChuyển trạng thái theo dòng + phát Kafka
AllocationUsageServiceallocation-usage.service.tsDINE_IN/TAKEAWAY/DELIVERY; reserved → completed/cancelled
ReservationServicereservation.service.tsLuồng đặt chỗ kèm giữ chỗ allocation
ShiftServiceshift.service.tsShift v2: open/join, close, ngữ cảnh ca theo thiết bị, list, validate; điều phối enrollment + drawer + report
ShiftActorServiceshift-actor.service.tsPhân giải ngữ cảnh actor (user / device / merchant / saleChannel) từ request
ShiftDrawerServiceshift-drawer.service.tsDrawer theo thiết bị: ghi nhận cash movement, đối chiếu, settle tiền mặt
ShiftPolicyServiceshift-policy.service.tsPhân giải policy ca của merchant; kiểm soát bật/tắt quản lý ca
ShiftReportServiceshift-report.service.tsSinh báo cáo X/Z + snapshot (theo thiết bị + toàn ca)
CustomerServicecustomer.service.tsCRUD khách hàng
CustomerPointServicecustomer-point.service.tsCộng điểm loyalty khi thanh toán thành công (idempotent)
PaymentWebhookServicepayment-webhook.service.tsBộ điều phối sự kiện webhook (định tuyến tới handler SaleOrder vs SaleCheck)
SaleOrderPaymentWebhookServicesale-order-payment-webhook.service.tsXử lý sự kiện attempt + transaction cho SaleOrder; phát Kafka
SaleCheckPaymentWebhookServicesale-check-payment-webhook.service.tsCùng luồng cho SaleCheck
PricingNetworkServicepricing-network.service.tsHTTP client gọi @nx/pricing cho calculate() + calculateV2()
AllocationSnapshotServiceallocation-snapshot.service.tsSnapshot trạng thái allocation unit/zone trên sự kiện order để audit
ProductVariantSnapshotServiceproduct-variant-snapshot.service.tsSnapshot metadata product variant lúc add order-item
SalesReportServicesales-report.service.tsTruy vấn tổng hợp daily / product / category / purchase

10. Repositories

RepositoryNguồnGhi chú
SaleOrderRepository@nx/core+ updateSummaryFromItems, getDailySummary, getProductSales, getCategorySales
SaleOrderItemRepository@nx/core+ updateAll, deleteAll
SaleCheckRepository@nx/core+ recalculateTotals
SaleCheckItemRepository@nx/core+ createAll
KitchenTicketRepository@nx/core+ findByIdempotencyKey, getNextSequence, evaluateTicketAutoProgression
KitchenTicketItemRepository@nx/core+ createAll
KitchenStationRepository@nx/core-
AllocationUsageRepository@nx/core+ existsBySaleOrderId
AllocationUnitRepository / AllocationZoneRepository@nx/core-
ReservationRepository@nx/core-
ShiftRepository@nx/core+ findLatestByChannel, tổng hợp cash-flow / order-stats / finance-section
ShiftEnrollmentRepository@nx/core+ findActiveByPrincipal, findActiveByDevice, tổng hợp enrollment
ShiftDrawerRepository@nx/core+ findByShiftEnrollmentId
ShiftEnrollmentEventRepository@nx/coreLedger sự kiện chỉ-ghi-thêm
ShiftReportRepository@nx/core+ getNextReportNo, hasZReport, findZReport, findLatestZReportByDevice
PointTransactionRepository@nx/core+ existsBySaleOrderId
CustomerRepository@nx/core-
SaleChannelRepository@nx/core-
ProductVariantRepository, ProductIdentifierRepository, ProductInfoRepository@nx/coreĐọc liên package
ConfigurationRepository@nx/coreĐọc cấu hình thanh toán đã mã hóa
FinanceTransactionRepository, FinanceWalletRepository@nx/coreĐọc liên package cho metadata finance lúc checkout
PurchaseOrderRepository@nx/coreĐọc liên package cho báo cáo bán hàng
MetaLinkRepository, SettingRepository, DeviceRepository@nx/coreLiên package

11. Điểm vào

FileMục đích
src/index.tsService entry → bootstrapApplication()
src/migrate.tsMigration entry → bootstrapMigration()
src/application.tsApplication extends VerifierApplication

12. Cấu hình

Env vars + dữ liệu seed: xem Configuration.

13. Vận hành

Triển khai + observability + bảo mật + runbook: xem Operations.

14. Trang liên quan

Concepts - vì sao/cách thức:

Reference - tra cứu:

Features - đào sâu:

Decisions:

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