Class: CallLog

Inherits:
ApplicationRecord show all
Defined in:
app/models/call_log.rb

Overview

== Schema Information

Table name: call_logs
Database name: primary

id :integer not null, primary key
campaign :string
from :string
from_number :string
origination :string
start_date_cst :date
start_time :datetime
talk_duration :integer default(0), not null
to :string
to_ext :integer
to_number :string
total_duration :integer default(0), not null
callrail_id :string
cdr_call_id :string
from_account_id :integer
from_party_id :integer
original_id :integer
source_id :integer
to_account_id :integer
to_party_id :integer

Indexes

idx_call_logs_start_origination (start_time,origination)
index_call_logs_on_cdr_call_id (cdr_call_id) UNIQUE
index_call_logs_on_from_account_id (from_account_id)
index_call_logs_on_from_party_id (from_party_id)
index_call_logs_on_origination (origination)
index_call_logs_on_start_date_cst (start_date_cst) USING brin
index_call_logs_on_to_account_id (to_account_id)
index_call_logs_on_to_ext (to_ext)
index_call_logs_on_to_party_id (to_party_id)

Foreign Keys

fk_rails_... (from_party_id => parties.id)
fk_rails_... (to_party_id => parties.id)

Constant Summary

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Has many collapse

Belongs to collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.last_log_import_datetimeObject



52
53
54
55
56
57
58
59
# File 'app/models/call_log.rb', line 52

def self.last_log_import_datetime
  # start_time is a naive `timestamp without time zone` that already stores
  # Central wall-clock (the PBX import writes local time). AR reads it back as
  # UTC, preserving those digits, so we format with no zone conversion. The
  # old `.localtime` shifted UTC->Central and rendered 5h early (e.g. 2:51 PM
  # shown as 9:51 AM). `&.` guards the empty-table case (maximum -> nil).
  CallLog.maximum(:start_time)&.to_fs(:crm_default)
end

.parse_phone_number(number) ⇒ Object



65
66
67
68
69
70
# File 'app/models/call_log.rb', line 65

def self.parse_phone_number(number)
  return if number.blank?

  n = number.match(/\d+/).to_s.match(/^9?1?(\d{10})$/).try(:[], 1) || number
  PhoneNumber.parse_and_format(n)
end

.valid_account_idsObject



61
62
63
# File 'app/models/call_log.rb', line 61

def self.
  EmployeeRecord.where.not(switchvox_account_id: nil).pluck(:switchvox_account_id)
end

Instance Method Details

#call_log_eventsActiveRecord::Relation<CallLogEvent>

Returns:

See Also:



47
# File 'app/models/call_log.rb', line 47

has_many :call_log_events, dependent: :destroy, primary_key: :cdr_call_id, foreign_key: :cdr_call_id

#find_party_using_account_id(account_id) ⇒ Object



96
97
98
99
100
# File 'app/models/call_log.rb', line 96

def ()
  return unless 

  EmployeePhoneStatus.where(switchvox_account_id: ).first.try(:employee)
end

#find_party_using_number(number) ⇒ Object



102
103
104
105
106
107
108
# File 'app/models/call_log.rb', line 102

def find_party_using_number(number)
  return if number.blank?

  Party.joins(:contact_points)
       .merge(ContactPoint.voice_callable)
       .where(contact_points: { detail: number }).first
end

#from_partyParty

Returns:

See Also:



48
# File 'app/models/call_log.rb', line 48

belongs_to :from_party, class_name: 'Party', inverse_of: :origin_call_logs, optional: true

#match_fromObject



81
82
83
84
85
86
87
# File 'app/models/call_log.rb', line 81

def match_from
  return if from_party

  # First look at account id
  self.from_party = ()
  self.from_party ||= find_party_using_number(from_number)
end

#match_partiesObject



72
73
74
75
# File 'app/models/call_log.rb', line 72

def match_parties
  match_from
  match_to
end

#match_toObject



89
90
91
92
93
94
# File 'app/models/call_log.rb', line 89

def match_to
  return if to_party

  self.to_party = ()
  self.to_party ||= find_party_using_number(to_number)
end

#sourceSource

Returns:

See Also:



50
# File 'app/models/call_log.rb', line 50

belongs_to :source, optional: true

#to_partyParty

Returns:

See Also:



49
# File 'app/models/call_log.rb', line 49

belongs_to :to_party, class_name: 'Party', inverse_of: :destination_call_logs, optional: true

#to_sObject



77
78
79
# File 'app/models/call_log.rb', line 77

def to_s
  "Call Log #{id} from #{from_number} to #{to_number} on #{start_time.to_fs(:crm_default)}"
end