Class: Noticent::ActiveRecordOptInProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/noticent/active_record_opt_in_provider.rb

Overview

should be used only for testing

Instance Method Summary collapse

Instance Method Details

#add_alert(scope:, alert_name:, recipient_ids:, channel:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/noticent/active_record_opt_in_provider.rb', line 18

def add_alert(scope:, alert_name:, recipient_ids:, channel:)
  ActiveRecord::Base.transaction do
    now = Time.now.utc.to_s(:db)
    # fetch all permutations of recipient and entity id
    permutations = Noticent::OptIn.distinct
                                  .where('recipient_id IN (?)', recipient_ids)
                                  .pluck(:entity_id, :recipient_id)

    return if permutations.empty?

    values = permutations.map { |e, r| "('#{scope}','#{alert_name}', #{e}, #{r}, '#{channel}', '#{now}', '#{now}')" }.join(',')
    ActiveRecord::Base.connection.execute("INSERT INTO opt_ins (scope, alert_name, entity_id, recipient_id, channel_name, created_at, updated_at) VALUES #{values}")
  end
end

#opt_in(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) ⇒ Object



6
7
8
# File 'lib/noticent/active_record_opt_in_provider.rb', line 6

def opt_in(recipient_id:, scope:, entity_id:, alert_name:, channel_name:)
  Noticent::OptIn.create!(recipient_id: recipient_id, scope: scope, entity_id: entity_id, alert_name: alert_name, channel_name: channel_name)
end

#opt_out(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) ⇒ Object



10
11
12
# File 'lib/noticent/active_record_opt_in_provider.rb', line 10

def opt_out(recipient_id:, scope:, entity_id:, alert_name:, channel_name:)
  Noticent::OptIn.where(recipient_id: recipient_id, scope: scope, entity_id: entity_id, alert_name: alert_name, channel_name: channel_name).destroy_all
end

#opted_in?(recipient_id:, scope:, entity_id:, alert_name:, channel_name:) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/noticent/active_record_opt_in_provider.rb', line 14

def opted_in?(recipient_id:, scope:, entity_id:, alert_name:, channel_name:)
  Noticent::OptIn.where(recipient_id: recipient_id, scope: scope, entity_id: entity_id, alert_name: alert_name, channel_name: channel_name).count != 0
end

#remove_alert(scope:, alert_name:) ⇒ Object



33
34
35
# File 'lib/noticent/active_record_opt_in_provider.rb', line 33

def remove_alert(scope:, alert_name:)
  Noticent::OptIn.where('scope = ? AND alert_name = ?', scope, alert_name).destroy_all
end

#remove_entity(scope:, entity_id:) ⇒ Object



37
38
39
# File 'lib/noticent/active_record_opt_in_provider.rb', line 37

def remove_entity(scope:, entity_id:)
  Noticent::OptIn.where('scope = ? AND entity_id = ?', scope, entity_id).destroy_all
end

#remove_recipient(recipient_id:) ⇒ Object



41
42
43
# File 'lib/noticent/active_record_opt_in_provider.rb', line 41

def remove_recipient(recipient_id:)
Noticent::OptIn.where(recipient_id: recipient_id).destroy_all
end