How to avoid commands calling commands using CQRS in NestJs

Viewed 26

I have a use case with two modules :

  • Users
  • Projects

I’m stuck with this use case :

At register, I have to check if Users mail is already associated to a user. If not, create the user and then create a Projects using the created user id.

I have a route like users/create.

How to avoid calling Projects create command inside Users create command ?

Do I have to use saga or may I miss some CQRS fundamentals ?

1 Answers

In this case, I would use a Saga, because registering a user involves many steps, including things like:

  • Email validation,
  • Sending out welcome emails...
  • Checking for blocked users
  • Checking for duplicates

A single handler should not query the read side if you can avoid it. By delegating this task to a workflow, your simplify your aggregates/command-handlers.

Related