Class: CustomerSourceChangedHandler

Inherits:
ApplicationJob show all
Includes:
RailsEventStore::AsyncHandler
Defined in:
app/subscribers/customer_source_changed_handler.rb

Overview

Subscribes to Events::CustomerSourceChanged. Replaces the legacy
Customer#add_customer_to_campaign after_save callback. When a customer's
source has just changed to one that is linked to a campaign, the campaign's
add_customer is invoked.

Runs asynchronously via ActiveJob so the campaign assignment does not block
the HTTP request cycle.

Wired in config/initializers/event_store.rb. The publisher already filters
for source_id changes and linked_to_campaign?; this handler only needs to
resolve the records and delegate.

Instance Method Summary collapse

Instance Method Details

#perform(event) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'app/subscribers/customer_source_changed_handler.rb', line 18

def perform(event)
  customer = Customer.find_by(id: event.data[:customer_id])
  return unless customer

  source = Source.find_by(id: event.data[:source_id])
  return unless source

  source.add_customer_to_campaign(customer)
end