Class: CommandTower::Messaging::Execution::ResolveRecipientAddress

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

Overview

Transport address only — eligibility / endpoint selection is owned by RecipientReadiness.

Constant Summary collapse

E164 =
/\A\+[1-9]\d{1,14}\z/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(communication:, channel_key:, readiness_result: nil) ⇒ ResolveRecipientAddress

Returns a new instance of ResolveRecipientAddress.



14
15
16
17
18
# File 'app/services/command_tower/messaging/execution/resolve_recipient_address.rb', line 14

def initialize(communication:, channel_key:, readiness_result: nil)
  @communication = communication
  @channel_key = channel_key
  @readiness_result = readiness_result
end

Class Method Details

.call(communication:, channel_key:, readiness_result: nil) ⇒ Object



10
11
12
# File 'app/services/command_tower/messaging/execution/resolve_recipient_address.rb', line 10

def self.call(communication:, channel_key:, readiness_result: nil)
  new(communication:, channel_key:, readiness_result:).call
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/command_tower/messaging/execution/resolve_recipient_address.rb', line 20

def call
  case @channel_key.to_s
  when "sms"
    phone = @communication&.user&.phone_number.to_s.strip
    if blank_recipient?(phone)
      { address: nil, error_code: "recipient_missing" }
    elsif !phone.match?(E164)
      { address: nil, error_code: "invalid_recipient" }
    else
      { address: phone, error_code: nil }
    end
  when "email"
    address = @communication&.user&.email
    if blank_recipient?(address)
      { address: nil, error_code: "recipient_missing" }
    else
      { address:, error_code: nil }
    end
  when "pushover"
    endpoint_id = @readiness_result&.resolved_endpoint_id
    if endpoint_id.nil?
      { address: nil, error_code: "recipient_missing" }
    else
      { address: endpoint_id.to_s, error_code: nil }
    end
  else
    { address: nil, error_code: "adapter_unconfigured" }
  end
end