Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ defmodule Commanded.Application do
- `causation_id` - an optional UUID used to identify the cause of the
command being dispatched.

- `command_uuid` - an optional UUID used to identify the command being
dispatched. When omitted, one will be generated automatically.

- `correlation_id` - an optional UUID used to correlate related
commands/events together.

Expand Down
18 changes: 16 additions & 2 deletions lib/commanded/commands/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ defmodule Commanded.Commands.Router do
command being dispatched.

- `command_uuid` - an optional UUID used to identify the command being
dispatched.
dispatched. When omitted, one will be generated automatically.

- `correlation_id` - an optional UUID used to correlate related
commands/events together.
Expand Down Expand Up @@ -546,7 +546,7 @@ defmodule Commanded.Commands.Router do

application = Keyword.fetch!(opts, :application)
causation_id = Keyword.get(opts, :causation_id)
command_uuid = Keyword.get_lazy(opts, :command_uuid, &UUID.uuid7/0)
command_uuid = Router.fetch_command_uuid(opts)
consistency = Keyword.fetch!(opts, :consistency)
correlation_id = Keyword.get_lazy(opts, :correlation_id, &UUID.uuid7/0)
metadata = Keyword.fetch!(opts, :metadata) |> validate_metadata()
Expand Down Expand Up @@ -648,6 +648,20 @@ defmodule Commanded.Commands.Router do
end
end

@doc false
def fetch_command_uuid(opts) do
case Keyword.fetch(opts, :command_uuid) do
{:ok, value} when is_binary(value) ->
value

:error ->
UUID.uuid7()

_ ->
raise ArgumentError, message: "command_uuid must be a binary"
end
end

@doc false
def get_opt(opts, name, default \\ nil) do
Keyword.get(opts, name) || Application.get_env(:commanded, name) || default
Expand Down
16 changes: 16 additions & 0 deletions test/commands/command_identity_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,21 @@ defmodule Commanded.Commands.CommandIdentityTest do
:ok = BankApp.dispatch(command, command_uuid: command_uuid)
assert [^command_uuid] = CommandAuditMiddleware.dispatched_commands(& &1.command_uuid)
end

test "should reject nil command_uuid" do
command = %OpenAccount{account_number: "ACC123", initial_balance: 1_000}

assert_raise ArgumentError, "command_uuid must be a binary", fn ->
BankApp.dispatch(command, command_uuid: nil)
end
end

test "should reject non-binary command_uuid" do
command = %OpenAccount{account_number: "ACC123", initial_balance: 1_000}

assert_raise ArgumentError, "command_uuid must be a binary", fn ->
BankApp.dispatch(command, command_uuid: 123)
end
end
end
end
Loading