Class: Phone::CallRailPartyMatcher

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/phone/call_rail_party_matcher.rb

Overview

Service object: call rail party matcher.

Defined Under Namespace

Classes: Result

Constant Summary collapse

CALLRAIL_SOURCE_ID =

Callrail source id.

4661

Instance Method Summary collapse

Instance Method Details

#processObject



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
# File 'app/services/phone/call_rail_party_matcher.rb', line 13

def process
  CallRailData.not_processed.each do |call|
    # let's try to find the party.
    # First, try to use the gclid to locate the visit.
    # JSONB containment via marketing_meta (GIN-indexed by
    # index_visits_on_marketing_meta_jsonb_path_ops).
    @party = call.gclid.present? ? Visit.with_marketing_meta(gclid: call.gclid).first&.party : nil
    # Second, if we cannot locate the visit we try with the phone number
    @party ||= ContactPoint.where(detail: call.customer_number).first&.party

    if @party.present?
      @party.update_column(:source_id, CALLRAIL_SOURCE_ID)

      created_after = ->(model) { model.arel_table[:created_at].gt(call.start_time) }

      matching_orders = @party.orders.where(created_after.call(Order))
      matching_orders.each { |order| order.update_column(:source_id, CALLRAIL_SOURCE_ID) }

      matching_opportunities = @party.opportunities.where(created_after.call(Opportunity))
      matching_opportunities.each { |opp| opp.update_column(:source_id, CALLRAIL_SOURCE_ID) }

      matching_invoices = @party.invoices.where(created_after.call(Invoice))
      matching_invoices.each { |inv| inv.update_column(:source_id, CALLRAIL_SOURCE_ID) }
    else
      # We couldn't find a party in HW coming form that call. Let's create the guest party at least
      Party.new
    end
    call.update_column(:processed, true)
  end
end