Class: Phone::CallRailImporter

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

Overview

Service object: call rail importer.

Constant Summary collapse

CALLRAIL_SOURCE_ID =

Callrail source id.

4661

Instance Method Summary collapse

Instance Method Details

#build_callrail_call(call_detail_record) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/phone/call_rail_importer.rb', line 23

def build_callrail_call(call_detail_record)
  {
    callrail_id: call_detail_record[:id],
    answered: call_detail_record[:answered],
    customer_number: PhoneNumber.parse_and_format(call_detail_record[:customer_phone_number]),
    customer_city: call_detail_record[:customer_city],
    customer_country: call_detail_record[:customer_country],
    customer_name: call_detail_record[:customer_name],
    customer_state: call_detail_record[:customer_state],
    tracking_phone_number: PhoneNumber.parse_and_format(call_detail_record[:tracking_phone_number]),
    start_time: call_detail_record[:start_time],
    campaign: call_detail_record[:campaign],
    keywords: call_detail_record[:keywords],
    gclid: call_detail_record[:gclid],
    landing_page_url: call_detail_record[:landing_page_url]
  }
end

#match_call_log_record(imported_calls) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'app/services/phone/call_rail_importer.rb', line 41

def match_call_log_record(imported_calls)
  imported_calls.each do |call_record|
    start_time = call_record['start_time']
    matching_calls = CallLog.where(from_number: call_record['customer_number'])
                            .where(CallLog[:start_time].gt(start_time))
                            .where(CallLog[:start_time].lt(start_time + 1.hour))
    matching_calls.presence&.update_all(source_id: CALLRAIL_SOURCE_ID, campaign: call_record['campaign'], callrail_id: call_record['id'])
  end
end

#processObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/services/phone/call_rail_importer.rb', line 7

def process
  call_summary = Api::Callrail.calls_summary('yesterday')
  call_summary.deep_symbolize_keys!
  total_number_of_calls = call_summary[:total_results][:total_calls]

  call_data = Api::Callrail.list_all_calls(total_number_of_calls, 1, 'yesterday')
  call_data.deep_symbolize_keys!
  calls = call_data[:calls]

  calls_to_import = calls.map { |call| build_callrail_call(call) }
  return if calls_to_import.empty?

  result = CallRailData.insert_all(calls_to_import, returning: %i[id customer_number start_time campaign])
  match_call_log_record(result.to_a)
end