Class: Yes::Core::CommandHandling::CommandGroupHandler

Inherits:
Object
  • Object
show all
Includes:
OpenTelemetry::Trackable
Defined in:
lib/yes/core/command_handling/command_group_handler.rb

Overview

High-level orchestrator for executing a Yes::Core::Commands::CommandGroup.

Mirrors CommandHandler but produces a Yes::Core::Commands::CommandGroupResponse carrying the array of published events. Group-level guards run; sub-command guards are bypassed (the executor publishes each sub-command’s event directly).

Examples:

handler = CommandGroupHandler.new(aggregate)
response = handler.call(:create_apprenticeship, {
  company_id:, user_id:, name:, description:
})

Instance Method Summary collapse

Constructor Details

#initialize(aggregate) ⇒ CommandGroupHandler

Returns a new instance of CommandGroupHandler.

Parameters:



22
23
24
25
26
# File 'lib/yes/core/command_handling/command_group_handler.rb', line 22

def initialize(aggregate)
  @aggregate = aggregate
  @command_utilities = aggregate.send(:command_utilities)
  @read_model = aggregate.read_model if aggregate.class.read_model_enabled?
end

Instance Method Details

#call(group_name, payload, guards: true, metadata: nil) ⇒ Yes::Core::Commands::CommandGroupResponse

Executes a command group and updates the aggregate’s read model.

Parameters:

  • group_name (Symbol)

    the command group name

  • payload (Hash)

    flat / partially-nested input payload

  • guards (Boolean) (defaults to: true)

    whether to evaluate the group’s guards

  • metadata (Hash, nil) (defaults to: nil)

    optional metadata to merge into each event

Returns:



35
36
37
38
39
40
41
42
43
44
# File 'lib/yes/core/command_handling/command_group_handler.rb', line 35

def call(group_name, payload, guards: true, metadata: nil)
  prepared = prepare_payload(payload, )
  cmd = command_utilities.build_group_command(group_name, prepared)
  guard_evaluator_class = command_utilities.fetch_guard_evaluator_class_for_group(group_name)

  ReadModelRecoveryService.check_and_recover_with_retries(read_model, aggregate:) if aggregate.class.read_model_enabled?

  CommandGroupExecutor.new(aggregate).
    call(cmd, group_name, guard_evaluator_class, skip_guards: !guards)
end