Class: Audience::PartyBackfill

Inherits:
Object
  • Object
show all
Defined in:
app/services/audience/party_backfill.rb

Overview

Resolves and persists the customer (party) for each unresolved audience_member on
a static email list by matching its email_address against the CRM:

  1. Contacts first — the most recently created Contact (that has a customer)
    carrying this email; we store that Contact's customer_id, i.e. its
    implicit customer.
  2. Customers next — if no Contact matches, the most recently created
    Customer holding this email directly; we store its own id.

"Most recent wins" breaks ties when several parties of the same type share
one email. Only unresolved (customer_id IS NULL), live (archived_at IS NULL) audience_members are touched, so the pass is idempotent and re-runnable.

Writing customer_id onto email audience_members is safe because migration
20260630053002 scoped the customer-uniqueness index to email-less rows — two
contacts under one customer can co-exist on the same email list, deduped by
email rather than customer.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(audience) ⇒ PartyBackfill

Returns a new instance of PartyBackfill.

Parameters:



28
29
30
# File 'app/services/audience/party_backfill.rb', line 28

def initialize(audience)
  @audience = audience
end

Instance Method Details

#callResult

Returns:



33
34
35
36
37
# File 'app/services/audience/party_backfill.rb', line 33

def call
  candidates = unresolved.count
  resolved   = candidates.zero? ? 0 : run_resolution
  Result.new(candidates:, resolved:, unmatched: candidates - resolved)
end