The policy acts as intermediary between Event and Command. Similar to the Command API, which handles a client request and convert it into a Command, the Policy handels an Event and converts it into a Command. This process enables a system to react to events automatically and remain responsive and consistent across different services.
The Command Policy has a Source Event
The Source Event is connected to the Command
The CloseTaskPolicy
class illustrates how to implement a policy within the Typebricks framework. It listens for specific events, processes them, and triggers the corresponding commands.
It can be found under: Task/src/useCases/write/CloseTask/application/CloseTaskPolicy.ts
1import { Policy, ProcessMethods, OverwriteProtectionBody } from "@codebricks/typebricks";2import { CloseTaskCommand } from "./CloseTaskCommand";3import { CloseTaskCommandHandler } from "./CloseTaskCommandHandler";4import { CloseTaskPolicyRepository } from "../infrastructure/CloseTaskPolicyRepository";5import { UserDeletedEventMessage } from "shared/application/inboundEvents/UserDeletedEventMessage";6import { AssigneeIdValueObject } from "shared/domain/valueObjects/AssigneeIdValueObject";78export class CloseTaskPolicy extends Policy {9 readonly useCaseName: string = 'CloseTask';10 readonly processMethods: ProcessMethods = {11 'Taskbricks.User.UserDeleted': this.processTaskbricksUserUserDeleted.bind(this)12 };13 readonly streamNames: string[] = ['Taskbricks.User'];1415 constructor(readonly commandHandler: CloseTaskCommandHandler = new CloseTaskCommandHandler(), readonly repository: CloseTaskPolicyRepository = new CloseTaskPolicyRepository()) {16 super(repository);17 }1819 @OverwriteProtectionBody(false)20 async processTaskbricksUserUserDeleted(eventMessage: UserDeletedEventMessage): Promise<void> {21 try {22 const command: CloseTaskCommand = new CloseTaskCommand({23 assigneeId: new AssigneeIdValueObject(eventMessage.aggregateId),24 });25 await this.commandHandler.handleCloseTask(command);26 } catch (error) {27 console.log(error);28 }29 }30}
useCaseName
and streamNames
Properties: These properties help identify the events that the policy listens for.processMethods
Property: Maps events to their respective processing methods.processTaskbricksUserUserDeleted
handle specific events and generate commands based on the event data.Error Handling: Implement robust error handling to avoid silent failures in policy processing.
By default, all errors are caught and logged to ensure the processing of subsequent events continues. If you wish to stop processing upon encountering an error, modify the catch clause to handle that scenario.