Class: Phone::CallRailPartyMatcher

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

Defined Under Namespace

Classes: Result

Constant Summary collapse

CALLRAIL_SOURCE_ID =
4661

Instance Method Summary collapse

Instance Method Details

#processObject



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

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
    @party = Visit.find_by(gclid: call.gclid)&.party
    #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)

      #Update the source in all orders created after the call
      matching_orders = @party.orders.where('created_at > (?)', call.start_time)
      if matching_orders.present?
        matching_orders.each do |order|
          order.update_column(:source_id, CALLRAIL_SOURCE_ID)
        end
      end

      #Update the source in all opportunities created after the call
      matching_opportunities = @party.opportunities.where('created_at > (?)', call.start_time)
      if matching_opportunities.present?
        matching_opportunities.each do |opp|
          opp.update_column(:source_id, CALLRAIL_SOURCE_ID)
        end
      end

      #Update the source in all invoices created after the call
      matching_invoices = @party.invoices.where('created_at > (?)', call.start_time)
      if matching_invoices.present?
        matching_invoices.each do |inv|
          inv.update_column(:source_id, CALLRAIL_SOURCE_ID)
        end
      end
    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