Sha256: b2f454abed3d9caab59e6c930737dd287009ad363ff4e657a80f9c556c168589

Contents?: true

Size: 1.96 KB

Versions: 5

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Noticent
  # should be used only for testing
  class ActiveRecordOptInProvider
    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

    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

    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

    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

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

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

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

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
noticent-0.0.5 lib/noticent/active_record_opt_in_provider.rb
noticent-0.0.4 lib/noticent/active_record_opt_in_provider.rb
noticent-0.0.3 lib/noticent/active_record_opt_in_provider.rb
noticent-0.0.2 lib/noticent/active_record_opt_in_provider.rb
noticent-0.0.1 lib/noticent/active_record_opt_in_provider.rb