領域事件與命令

預設範本的 Reaction 中已註冊的 Event、Command 及其處理方式。

一次業務變動會先產生 Event,再由 Event 建立一或多個 Command。Reaction 只處理已註冊至 src/core/reaction/events/index.ts 和 src/core/reaction/commands/index.ts 的定義。

Event

EventInput

Prop

Type

已註冊的 Event

Eventtype輸入要點產生的 Command
UserSignupEventuser_signed_upSubject、來源,以及選填的角色和註冊郵件grant_role、send_email
UserEmailVerifiedEventuser_email_verified使用者 Subject、verifiedEmail、emailHash、verifiedAt、purpose帳號電子郵件驗證成功,且電子郵件地址符合管理員名單時,產生 grant_role
PaymentSucceededEventpayment_succeededSubject、來源,以及選填的角色和權益grant_role、grant_entitlement
SubscriptionCreatedEventsubscription_createdSubject、訂閱來源,以及選填的角色和權益grant_role、grant_entitlement
SubscriptionUpdatedEventsubscription_updatedSubject、訂閱來源,以及選填的完整角色和權益設定先撤銷該來源的既有內容,再依新設定重新授予
SubscriptionEndedEventsubscription_endedSubject、訂閱來源,以及選填的角色和權益revoke_role、revoke_entitlement
UserEmailUpdatedEventuser_email_updated使用者 Subject、電子郵件地址摘要,以及選填的舊電子郵件地址通知資料同步付款服務供應商的電子郵件地址,並視需要向舊地址寄送安全性提醒
UserTwoFactorSecurityChangedEventuser_two_factor_security_changed使用者、電子郵件地址、姓名、語言、發生時間和安全性設定網址send_email
EntitlementCycleDueEvententitlement_cycle_due選填的下一筆週期權益紀錄process_due_entitlement_cycles
DataCleanupEventdata_cleanupolderThanpurge_stale_data
NotificationRequestedEventnotification_requested通知目標和訊息send_notification

SubscriptionUpdatedEvent 和 SubscriptionEndedEvent 處理 roles、entitlements 時,必須區分欄位不存在與空陣列。欄位不存在時,不處理這類資料。空陣列則會撤銷該來源下的全部角色或權益。

UserEmailVerifiedEvent 的 purpose 為 account 或 password_reset。只有本站帳號的電子郵件驗證流程,會依 adminEmails 的精確比對結果授予 admin 角色。密碼重設驗證不會授予角色,OAuth 供應商傳回的電子郵件已驗證宣告,也不能取代本站驗證。

UserEmailUpdatedEvent 預設設定 continueOnFailure: true。同步付款電子郵件地址與寄送安全性提醒彼此獨立,即使其中一項重試後仍失敗,另一項也會繼續執行。

Command

CommandDraft

Prop

Type

key 僅用於區分同一 Event 中的不同步驟,不決定執行順序,也不作為 Event 等冪鍵或 Command 執行 ID。

已註冊的 Command

Commandtype模式用途
GrantRoleCommandgrant_rolesync依來源為 Subject 授予角色
RevokeRoleCommandrevoke_rolesync依來源撤銷角色或靜態能力
GrantEntitlementCommandgrant_entitlementsync依來源為 Subject 授予權益
RevokeEntitlementCommandrevoke_entitlementsync依來源撤銷權益或動態能力
SendEmailCommandsend_emailasync依已註冊郵件範本產生內容並寄送郵件
SendNotificationCommandsend_notificationasync透過設定的通知管道傳送訊息
CreateUserNotificationCommandcreate_user_notificationasync建立已發布的站內通知,Command ID 同時作為通知 ID
SyncPaymentCustomerEmailCommandsync_payment_customer_emailasync將使用者最新的已驗證電子郵件地址同步至付款服務供應商的 Customer
ProcessDueEntitlementCyclesCommandprocess_due_entitlement_cyclesasync處理到期的週期權益,並安排下一次執行
PurgeStaleDataCommandpurge_stale_dataasync清理超過指定時間的資料

範本中所有 Command 的預設版本都是 1,最大嘗試次數都是 3。每個 Command 定義可以另外設定這兩個值。Event 建立 Command 時,也可以只修改本次執行的 maxAttempts。

sync Command 會在 Event 執行過程中立即處理,async Command 則交給 ASYNC_POLICY_TASK_QUEUE。無論採用哪種模式,執行紀錄都會寫入 command_execution。

執行狀態

EventExecutionStatus

值含義
processing未觸發終止條件,且仍有 Command 等待執行
completed沒有 Command 處於等待、業務失敗或例外狀態
partial_failedcontinueOnFailure 為 true,全部 Command 已結束,其中至少一個傳回業務失敗
failedcontinueOnFailure 為 false,且至少一個 Command 傳回業務失敗
errored至少一個 Command 執行時擲出例外

Event 狀態由其 Command 狀態計算得出,不另外儲存,以免兩份狀態不一致。

CommandExecutionStatus

pending、succeeded、failed、errored 分別表示等待執行、執行成功、傳回業務失敗和擲出例外。

註冊位置

新增 Event 後,必須加入 src/core/reaction/events/index.ts 的 events 陣列。新增 Command 後,必須加入 src/core/reaction/commands/index.ts 的 commands 陣列。若只建立檔案而未註冊,Reaction 就無法辨識這項定義。