Class: Campaign::AssignCustomersToSubscriberList

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/campaign/assign_customers_to_subscriber_list.rb

Overview

Assigns and merge a list a of customer_id into an existing subscriber list

Defined Under Namespace

Classes: Result

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#process(subscriber_list, customer_ids = []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'app/services/campaign/assign_customers_to_subscriber_list.rb', line 10

def process(subscriber_list, customer_ids = [])
  existing_customer_ids = subscriber_list.subscribers.pluck(:customer_id)
  new_customer_ids = (customer_ids - existing_customer_ids)
  # Per-record create runs Subscriber validations + before_save :populate_email_if_missing
  # so that email-type subscriber lists get email_address backfilled from the customer.
  new_customer_ids.each do |new_customer_id|
    Subscriber.create(active: true, subscriber_list_id: subscriber_list.id, customer_id: new_customer_id)
  end
  Result.new(subscribed: true, messages: [], new_subscribers: new_customer_ids.size, existing_subscribers: existing_customer_ids.size)
end