Chapter 2
In this chapter, you will:
- Add a second usecase for completing tasks.
- Learn how to work with evolving aggregate states and enforce business rules.
- Generate and customize code, and write unit tests for your aggregate.
Adding the Second Usecase
-
Create the Complete Task Usecase:
- Add a new Command API usecase named complete task. This usecase will handle marking tasks as complete.
-
Define Request Properties:
- Add the following properties to the request:
task id
(UUID): The unique identifier for the task.completion node
(string): An optional detail about the completion node.
- Add the following properties to the request:
-
Introduce Task Status:
- To ensure that only open tasks can be completed, add a task status enum to the aggregate state. This helps in tracking and validating task states.
tipEnsure the status is set to "open" in the add task usecase before proceeding.
Generating the Code
-
Set Usecase Status to Ready:
- Change the status of the complete task usecase to Ready to prepare it for code generation.
-
Generate the Source Code:
- Verify that your local code generator is active and generate the source code for the updated usecase.
Customizing the Code
-
Implement Business Rules:
- Apply the following rules:
completionNode
values must be longer than 3 characters.- Ensure only tasks with an "open" status can be marked as completed.
tipEnable Overwrite Protection for any custom code snippets to prevent unintended changes during future code generations.
- Apply the following rules:
Testing the Code
-
Run the Tests:
- Execute the tests to ensure that your application behaves as expected.
tipWe highly recommend adding unit tests specifically for the aggregate to cover edge cases and verify its functionality.
npm run test-integration
npm run test-unit