Class: Phone::CallRecordMatcher

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

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

#processObject



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

def process
  records_processed = 0
  records_matched = 0
  records_failed = 0
  messages = []
  call_records_without_origin = CallRecord.where(origin_party_id: nil)
  total_call_records_without_origin = call_records_without_origin.size
  logger.info "Processing #{total_call_records_without_origin} call records without origin"
  call_records_without_origin.find_each do |cr|
    cr.match_origin_party
    records_processed += 1
    if cr.save
      records_matched += 1 if cr.origin_party_id
    else
      records_failed += 1
      msg = "Call Record #{cr.id} failed to save: #{cr.errors_to_s}"
      messages << msg
      logger.error msg
    end
  end

  call_records_without_destination = CallRecord.where(destination_party_id: nil)
  total_call_records_without_destination = call_records_without_destination.size
  logger.info "Processing #{total_call_records_without_destination} call records without destination"
  CallRecord.where(destination_party_id: nil).find_each do |cr|
    cr.match_destination_party
    records_processed += 1
    if cr.save
      records_matched += 1 if cr.destination_party_id
    else
      records_failed += 1
      msg = "Call Record #{cr.id} failed to save: #{cr.errors_to_s}"
      messages << msg
      logger.error msg
    end
  end
  result_hsh = {
                records_processed: records_processed,
                records_matched: records_matched,
                records_failed: records_failed,
                messages: messages
              }
  logger.debug("Call Record Matching complete")
  Result.new(**result_hsh)
end