Class: CommandTower::Messaging::Preferences::Store

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete!(recipient_id:, notification_type_key:) ⇒ Object



15
16
17
# File 'app/services/command_tower/messaging/preferences/store.rb', line 15

def self.delete!(recipient_id:, notification_type_key:)
  new.delete!(recipient_id:, notification_type_key:)
end

.find(recipient_id:, notification_type_key:) ⇒ Object



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

def self.find(recipient_id:, notification_type_key:)
  new.find(recipient_id:, notification_type_key:)
end

.upsert!(recipient_id:, notification_type_key:, preference_state:) ⇒ Object



11
12
13
# File 'app/services/command_tower/messaging/preferences/store.rb', line 11

def self.upsert!(recipient_id:, notification_type_key:, preference_state:)
  new.upsert!(recipient_id:, notification_type_key:, preference_state:)
end

Instance Method Details

#delete!(recipient_id:, notification_type_key:) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/command_tower/messaging/preferences/store.rb', line 52

def delete!(recipient_id:, notification_type_key:)
  key = notification_type_key.to_s
  record = Messaging::NotificationPreference.find_for(
    recipient_id:,
    notification_type_key: key,
  )
  return if record.nil?

  record.destroy!
  nil
rescue ActiveRecord::ActiveRecordError => error
  raise StoreError, "failed to delete notification preference: #{error.message}"
end

#find(recipient_id:, notification_type_key:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/command_tower/messaging/preferences/store.rb', line 19

def find(recipient_id:, notification_type_key:)
  record = Messaging::NotificationPreference.find_for(
    recipient_id:,
    notification_type_key:,
  )
  return nil if record.nil?

  PreferenceState.normalize(record.state)
rescue ActiveRecord::ActiveRecordError => error
  raise StoreError, "failed to load notification preference: #{error.message}"
rescue InvalidPreferenceStateError
  raise
end

#upsert!(recipient_id:, notification_type_key:, preference_state:) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/command_tower/messaging/preferences/store.rb', line 33

def upsert!(recipient_id:, notification_type_key:, preference_state:)
  key = notification_type_key.to_s
  declaration = lookup_declaration!(key)
  validate_writable!(declaration)

  normalized = PreferenceState.normalize(preference_state)
  if normalized.nil?
    raise InvalidPreferenceWriteError, "preference_state must be present"
  end

  validate_channels!(declaration, normalized)
  validate_inbox!(declaration, normalized)

  persist!(recipient_id:, notification_type_key: key, state: normalized.to_raw_hash)
  normalized
rescue ActiveRecord::ActiveRecordError => error
  raise StoreError, "failed to persist notification preference: #{error.message}"
end