Class: CommandTower::Messaging::Preferences::Evaluator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notification_type_key:, recipient_id:, preference_state:, platform_enabled_channels:) ⇒ Evaluator

Returns a new instance of Evaluator.



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

def initialize(
  notification_type_key:,
  recipient_id:,
  preference_state:,
  platform_enabled_channels:
)
  @notification_type_key = notification_type_key.to_s
  @recipient_id = recipient_id
  @raw_preference_state = preference_state
  @platform_enabled_channels = Array(platform_enabled_channels).map(&:to_s).freeze
end

Class Method Details

.callObject



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

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

Instance Method Details

#callObject



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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/services/command_tower/messaging/preferences/evaluator.rb', line 23

def call
  declaration = lookup_declaration!
  defaults = PreferenceState.from_declaration_default(declaration.default_preference_state)
  recipient_override = PreferenceState.normalize(@raw_preference_state)

  effective =
    if declaration.user_configurable && recipient_override
      recipient_override.merge_over(defaults)
    else
      defaults
    end

  permitted = []
  suppressed = []

  declaration.allowed_channels.each do |channel|
    if !@platform_enabled_channels.include?(channel)
      suppressed << SuppressedDestination.build(
        destination: channel,
        reason_class: ReasonClasses::SKIPPED_BY_POLICY,
      )
      next
    end

    if declaration.user_configurable && !effective.channel_enabled?(channel, default: true)
      suppressed << SuppressedDestination.build(
        destination: channel,
        reason_class: ReasonClasses::SUPPRESSED_BY_PREFERENCE,
      )
      next
    end

    # Not user-configurable: preference opt-outs ignored; still require default enablement
    if !declaration.user_configurable && !effective.channel_enabled?(channel, default: true)
      suppressed << SuppressedDestination.build(
        destination: channel,
        reason_class: ReasonClasses::SKIPPED_BY_POLICY,
      )
      next
    end

    permitted << channel
  end

  extras = effective.channels.keys - declaration.allowed_channels
  extras.each do |channel|
    suppressed << SuppressedDestination.build(
      destination: channel,
      reason_class: ReasonClasses::SKIPPED_BY_POLICY,
    )
  end

  inbox_permitted, inbox_suppressed = evaluate_inbox(declaration, effective)
  suppressed.concat(inbox_suppressed)

  mandatory_enforced = false
  if declaration.mandatory
    permitted, suppressed, inbox_permitted, mandatory_enforced =
      enforce_mandatory(
        declaration:,
        defaults:,
        permitted:,
        suppressed:,
        inbox_permitted:,
      )
  end

  EvaluationResult.build(
    notification_type_key: @notification_type_key,
    recipient_id: @recipient_id,
    permitted_channels: permitted.uniq,
    inbox_permitted:,
    suppressed_destinations: suppressed,
    mandatory: declaration.mandatory,
    mandatory_enforced:,
    stored_override_present: false,
    effective_preference_state: effective.to_raw_hash,
  )
end