Class: PartyResearch::AutoEnricher

Inherits:
Object
  • Object
show all
Defined in:
app/services/party_research/auto_enricher.rb

Overview

Automatic Lead Enrichment trigger fired by the Customer state machine
when a record transitions into lead_qualify. Drops a fresh
PartyResearchRun onto Sidekiq with every configured + relevant
adapter so the sales manager sees curated findings already waiting
on the duplicates tab the next morning.

Idempotent at the day-grain level — won't re-run if an enrichment
kicked off in the last DEBOUNCE window. Sidekiq retry safety lives
in the Orchestrator (per-adapter resume guard).

Constant Summary collapse

DEBOUNCE =
6.hours
SETTLE_DELAY =

Wait this long before the worker runs so writes that follow lead
creation in the same session — name cleanup, profile
classification, contact attachment — commit first. Enriching the
instant a guest hits lead_qualify ran against a still-raw record
(placeholder name, default Homeowner profile, no Contact) and
wasted the run.

10.minutes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(party, force: false, only: nil) ⇒ AutoEnricher

Returns a new instance of AutoEnricher.



41
42
43
44
45
# File 'app/services/party_research/auto_enricher.rb', line 41

def initialize(party, force: false, only: nil)
  @party = party
  @force = force
  @only = only&.map(&:to_s).presence
end

Class Method Details

.enqueue(party, force: false, only: nil) ⇒ Object

Public entry point. Returns the PartyResearchRun that was
enqueued, or nil when skipped (not enrichable / debounced /
already running / nothing configured).

Parameters:

  • force (Boolean) (defaults to: false)

    bypass the time-based debounce — used when a
    material identity change (e.g. a homeowner ↔ non-homeowner
    reclassification) makes a fresh run worthwhile even within the
    debounce window. A forced run still refuses to stack on an
    in-flight one.

  • only (Array<String>, nil) (defaults to: nil)

    restrict the run to these adapter
    names instead of the full configured+relevant set. Used by the
    customer-creation hook to fire the cheap logo_dev lookup alone,
    long before the lead is qualified.



37
38
39
# File 'app/services/party_research/auto_enricher.rb', line 37

def self.enqueue(party, force: false, only: nil)
  new(party, force: force, only: only).enqueue
end

Instance Method Details

#enqueueObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/party_research/auto_enricher.rb', line 47

def enqueue
  return nil unless @party.enrichable_via_research?

  adapters = adapters_for_auto_run
  return nil if adapters.empty?
  return nil if skip_for_existing_run?(adapters)

  run = PartyResearchRun.create!(party: @party, adapters: adapters)
  jid = PartyEnrichmentWorker.perform_in(SETTLE_DELAY, run.id)
  run.update!(sidekiq_jid: jid)
  run
rescue StandardError => e
  # Auto-enrichment is best-effort — never block the state
  # transition (or whatever else triggered it) on a queueing
  # hiccup.
  Rails.logger.warn("[PartyResearch::AutoEnricher] #{@party.class.name}##{@party.id} failed: #{e.class}: #{e.message}")
  nil
end