Class: MassSearch::CustomerCreateSubscriberListWorker
- Inherits:
-
Object
- Object
- MassSearch::CustomerCreateSubscriberListWorker
- Includes:
- Sidekiq::Status::Worker, Sidekiq::Worker
- Defined in:
- app/workers/mass_search/customer_create_subscriber_list_worker.rb
Overview
Creates a SubscriberList from the selected Customers in the background.
action_params: { subscriber_list: { subscriber_list_name:, subscriber_list_type:, add_all_emails: } }
Instance Method Summary collapse
Instance Method Details
#perform(args = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 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 |
# File 'app/workers/mass_search/customer_create_subscriber_list_worker.rb', line 11 def perform(args = {}) args = args.with_indifferent_access search = Search.find(args[:search_id]) CurrentScope.locale = args[:locale]&.to_sym || :en params = (args[:action_params] || {}).with_indifferent_access sl_params = params[:subscriber_list] || {} subscriber_list = SubscriberList.new( name: sl_params[:subscriber_list_name], list_type: sl_params[:subscriber_list_type] ) pinned = search.pinned_query .where.not(resource_id: nil) .joins("join parties p on p.id = search_results.resource_id and p.type = 'Customer' and p.state NOT IN ('closed','bankrupt','guest')") require 'activerecord-import/base' require 'activerecord-import/active_record/adapters/postgresql_adapter' case subscriber_list.list_type when 'email_static' emails = [] pinned.each do |sr| if sl_params[:add_all_emails].to_b sr.resource.all_emails.each { |e| emails << e unless e.blank? } elsif sr.resource.email.present? emails << sr.resource.email end end Subscriber.import(emails.uniq.map { |e| { active: true, email_address: e, subscriber_list_id: subscriber_list.id } }, validate: false) if subscriber_list.save when 'email_dynamic' subscriber_list.customer_search_params = search.query_params subscriber_list.add_all_emails = sl_params[:add_all_emails].to_b subscriber_list.save when 'customer' if subscriber_list.save customer_ids = pinned.distinct.pluck(:resource_id) Subscriber.import(customer_ids.map { |cid| { active: true, customer_id: cid, subscriber_list_id: subscriber_list.id } }, validate: false) end end if subscriber_list.persisted? store info_message: "Subscriber List #{subscriber_list.name} [#{subscriber_list.id}] created.", redirect_to: UrlHelper.instance.subscriber_list_path(subscriber_list.id) else store error_message: "Unable to save Subscriber List: #{subscriber_list.errors_to_s}.", redirect_to: Rails.application.routes.url_helpers.search_path(search.id) end end |