Domain events and commands

Events and Commands registered in the default template's Reaction system and how they are processed.

A business change first produces an Event, which then creates one or more Commands. Reaction only processes definitions registered in src/core/reaction/events/index.ts and src/core/reaction/commands/index.ts.

Event

EventInput

Prop

Type

Registered Events

EventtypeKey inputsCommands created
UserSignupEventuser_signed_upSubject, source, and optional roles and signup emailgrant_role, send_email
UserEmailVerifiedEventuser_email_verifiedUser subject, verifiedEmail, emailHash, verifiedAt, and purposeCreates grant_role when account email verification succeeds and the email matches the administrator list
PaymentSucceededEventpayment_succeededSubject, source, and optional roles and entitlementsgrant_role, grant_entitlement
SubscriptionCreatedEventsubscription_createdSubject, subscription source, and optional roles and entitlementsgrant_role, grant_entitlement
SubscriptionUpdatedEventsubscription_updatedSubject, subscription source, and optional complete role and entitlement configurationsRevokes existing grants from this source, then grants them again using the new configuration
SubscriptionEndedEventsubscription_endedSubject, subscription source, and optional roles and entitlementsrevoke_role, revoke_entitlement
UserEmailUpdatedEventuser_email_updatedUser subject, email hash, and optional notification data for the old email addressSynchronizes the email address with payment providers and sends a security alert to the old address as needed
UserTwoFactorSecurityChangedEventuser_two_factor_security_changedUser, email, name, language, occurrence time, and security settings URLsend_email
EntitlementCycleDueEvententitlement_cycle_dueOptional next recurring entitlement recordprocess_due_entitlement_cycles
DataCleanupEventdata_cleanupolderThanpurge_stale_data
NotificationRequestedEventnotification_requestedNotification destination and messagesend_notification

When SubscriptionUpdatedEvent and SubscriptionEndedEvent process roles and entitlements, an absent field differs from an empty array. An absent field leaves that category of data untouched. An empty array revokes all roles or entitlements from that source.

The purpose of UserEmailVerifiedEvent is either account or password_reset. Only the site's account email verification flow grants the admin role based on an exact match in adminEmails. Password reset verification does not grant this role, and an OAuth provider's claim that an email is verified cannot replace verification on this site.

UserEmailUpdatedEvent defaults to continueOnFailure: true. Synchronizing the payment email and sending the security alert are independent. Even if one still fails after retries, the other continues to execute.

Command

CommandDraft

Prop

Type

key only distinguishes steps within the same Event. It does not determine execution order or serve as the Event's idempotency key or the Command execution ID.

Registered Commands

CommandtypeModePurpose
GrantRoleCommandgrant_rolesyncGrant roles to a subject by source
RevokeRoleCommandrevoke_rolesyncRevoke roles or static capabilities by source
GrantEntitlementCommandgrant_entitlementsyncGrant entitlements to a subject by source
RevokeEntitlementCommandrevoke_entitlementsyncRevoke entitlements or dynamic capabilities by source
SendEmailCommandsend_emailasyncRender a registered email template and send the email
SendNotificationCommandsend_notificationasyncSend a message through the configured notification channels
CreateUserNotificationCommandcreate_user_notificationasyncCreate a published in-app notification, using the Command ID as the notification ID
SyncPaymentCustomerEmailCommandsync_payment_customer_emailasyncSynchronize the user's latest verified email address with the payment provider's Customer
ProcessDueEntitlementCyclesCommandprocess_due_entitlement_cyclesasyncProcess recurring entitlements that are due and schedule the next execution
PurgeStaleDataCommandpurge_stale_dataasyncDelete data older than the specified time

All Commands in the template default to version 1 and a maximum of 3 attempts. Each Command definition can set these values independently. When an Event creates a Command, it can also override maxAttempts for that execution alone.

sync Commands are processed immediately during Event execution. async Commands are sent to ASYNC_POLICY_TASK_QUEUE. Both modes write execution records to command_execution.

Execution statuses

EventExecutionStatus

ValueMeaning
processingNo termination condition has been met, and Commands are still waiting to execute
completedNo Commands are pending or in a business failure or exception state
partial_failedcontinueOnFailure is true, all Commands have finished, and at least one returned a business failure
failedcontinueOnFailure is false, and at least one Command returned a business failure
erroredAt least one Command threw an exception during execution

An Event's status is calculated from its Command statuses rather than stored separately, avoiding inconsistencies between two copies of the status.

CommandExecutionStatus

pending, succeeded, failed, and errored mean waiting to execute, successful execution, a returned business failure, and a thrown exception, respectively.

Registration locations

Add each new Event to the events array in src/core/reaction/events/index.ts and each new Command to the commands array in src/core/reaction/commands/index.ts. Reaction will not recognize a definition if you only create its file without registering it.