Class: CommandTower::Messaging::Planner::Planner

Inherits:
Object
  • Object
show all
Defined in:
app/services/command_tower/messaging/planner/planner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notification_type_key:, recipient_id:, preference_state: nil, platform_enabled_channels: [], message_overrides: nil) ⇒ Planner

Returns a new instance of Planner.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/services/command_tower/messaging/planner/planner.rb', line 11

def initialize(
  notification_type_key:,
  recipient_id:,
  preference_state: nil,
  platform_enabled_channels: [],
  message_overrides: nil
)
  @notification_type_key = notification_type_key.to_s
  @recipient_id = recipient_id
  @preference_state = preference_state
  @platform_enabled_channels = Array(platform_enabled_channels).map(&:to_s)
  @message_overrides = MessageOverrides.normalize(message_overrides)
end

Class Method Details

.callObject



7
8
9
# File 'app/services/command_tower/messaging/planner/planner.rb', line 7

def self.call(...)
  new(...).call
end

Instance Method Details

#callObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/services/command_tower/messaging/planner/planner.rb', line 25

def call
  unless @preference_state.nil?
    raise InvalidEvaluationError,
          "preference_state must not be supplied to Planner; use Preferences::Store"
  end

  declaration = lookup_declaration!
  evaluation = evaluate_preferences!
  readiness = evaluate_readiness!

  selected_channels = declaration.default_channels.select do |channel|
    evaluation.permitted_channels.include?(channel)
  end
  inbox_selected = declaration.inbox_available && evaluation.inbox_permitted

  excluded = build_initial_exclusions(
    declaration:,
    evaluation:,
    selected_channels:,
    inbox_selected:,
  )

  selected_channels, excluded = apply_readiness_intersection(
    selected_channels:,
    excluded:,
    readiness:,
  )

  selected_channels, inbox_selected, excluded =
    apply_overrides(
      declaration:,
      evaluation:,
      readiness:,
      selected_channels:,
      inbox_selected:,
      excluded:,
    )

  if declaration.mandatory && selected_channels.empty? && !inbox_selected
    raise ImpossibleMandatoryPlanError,
          "mandatory notification type has no selectable destinations: #{@notification_type_key}"
  end

  DestinationPlan.build(
    notification_type_key: @notification_type_key,
    recipient_id: @recipient_id,
    selected_channels:,
    inbox_selected:,
    excluded_destinations: excluded,
    mandatory: declaration.mandatory,
    preference_evaluation: evaluation,
  )
end