Skip to main content

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

  1. Create the Complete Task Usecase:

    • Add a new Command API usecase named complete task. This usecase will handle marking tasks as complete.

    Learn About Usecases
    Naming Conventions for Usecases

  2. 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.

    Usecase Schema Details
    Data Flows for Usecases

  3. 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.
    tip

    Ensure the status is set to "open" in the add task usecase before proceeding.

Generating the Code

  1. Set Usecase Status to Ready:

    • Change the status of the complete task usecase to Ready to prepare it for code generation.

    Usecase Status Documentation

  2. Generate the Source Code:

    • Verify that your local code generator is active and generate the source code for the updated usecase.

    Code Generation Instructions

Customizing the Code

  1. 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.
    tip

    Enable Overwrite Protection for any custom code snippets to prevent unintended changes during future code generations.

Testing the Code

  1. Run the Tests:

    • Execute the tests to ensure that your application behaves as expected.
    tip

    We 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