2016-04-23 09:04:33 +00:00
|
|
|
module Cti
|
|
|
|
class Log < ApplicationModel
|
|
|
|
self.table_name = 'cti_logs'
|
|
|
|
|
2016-04-29 08:23:12 +00:00
|
|
|
store :preferences
|
|
|
|
|
2016-04-23 09:04:33 +00:00
|
|
|
after_create :push_event, :push_caller_list
|
|
|
|
after_update :push_event, :push_caller_list
|
|
|
|
after_destroy :push_event, :push_caller_list
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
Cti::Log.create(
|
|
|
|
direction: 'in',
|
|
|
|
from: '007',
|
2016-04-27 12:35:36 +00:00
|
|
|
from_comment: 'AAA',
|
2016-04-23 09:04:33 +00:00
|
|
|
to: '008',
|
2016-04-27 12:35:36 +00:00
|
|
|
to_comment: 'BBB',
|
2016-04-23 09:04:33 +00:00
|
|
|
call_id: '1',
|
|
|
|
comment: '',
|
|
|
|
state: 'newCall',
|
|
|
|
)
|
|
|
|
|
|
|
|
Cti::Log.create(
|
|
|
|
direction: 'in',
|
|
|
|
from: '007',
|
|
|
|
from_comment: '',
|
|
|
|
to: '008',
|
|
|
|
to_comment: '',
|
|
|
|
call_id: '2',
|
|
|
|
comment: '',
|
|
|
|
state: 'answer',
|
|
|
|
)
|
|
|
|
|
|
|
|
Cti::Log.create(
|
|
|
|
direction: 'in',
|
|
|
|
from: '009',
|
|
|
|
from_comment: '',
|
|
|
|
to: '010',
|
|
|
|
to_comment: '',
|
|
|
|
call_id: '3',
|
|
|
|
comment: '',
|
|
|
|
state: 'hangup',
|
|
|
|
)
|
|
|
|
|
2016-05-31 08:56:13 +00:00
|
|
|
example data, can be used for demo
|
|
|
|
|
|
|
|
Cti::Log.create(
|
|
|
|
direction: 'in',
|
|
|
|
from: '4930609854180',
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
to: '4930609811111',
|
|
|
|
to_comment: 'Bob Smith',
|
|
|
|
call_id: '00001',
|
|
|
|
comment: '',
|
|
|
|
state: 'newCall',
|
|
|
|
done: false,
|
|
|
|
preferences: {
|
|
|
|
from: [
|
|
|
|
{
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
comment: nil,
|
|
|
|
level: 'known',
|
|
|
|
object: 'User',
|
|
|
|
o_id: 2,
|
|
|
|
user_id: 2,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
Cti::Log.create(
|
|
|
|
direction: 'out',
|
|
|
|
from: '4930609854180',
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
to: '4930609811111',
|
|
|
|
to_comment: 'Bob Smith',
|
|
|
|
call_id: '00002',
|
|
|
|
comment: '',
|
|
|
|
state: 'newCall',
|
|
|
|
preferences: {
|
|
|
|
to: [
|
|
|
|
{
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
comment: nil,
|
|
|
|
level: 'known',
|
|
|
|
object: 'User',
|
|
|
|
o_id: 2,
|
|
|
|
user_id: 2,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
Cti::Log.create(
|
|
|
|
direction: 'in',
|
|
|
|
from: '4930609854180',
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
to: '4930609811111',
|
|
|
|
to_comment: 'Bob Smith',
|
|
|
|
call_id: '00003',
|
|
|
|
comment: '',
|
|
|
|
state: 'answer',
|
|
|
|
preferences: {
|
|
|
|
from: [
|
|
|
|
{
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
comment: nil,
|
|
|
|
level: 'known',
|
|
|
|
object: 'User',
|
|
|
|
o_id: 2,
|
|
|
|
user_id: 2,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
Cti::Log.create(
|
|
|
|
direction: 'in',
|
|
|
|
from: '4930609854180',
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
to: '4930609811111',
|
|
|
|
to_comment: 'Bob Smith',
|
|
|
|
call_id: '00004',
|
|
|
|
comment: '',
|
|
|
|
state: 'hangup',
|
|
|
|
comment: 'normalClearing',
|
|
|
|
done: false,
|
|
|
|
preferences: {
|
|
|
|
from: [
|
|
|
|
{
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
comment: nil,
|
|
|
|
level: 'known',
|
|
|
|
object: 'User',
|
|
|
|
o_id: 2,
|
|
|
|
user_id: 2,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
Cti::Log.create(
|
|
|
|
direction: 'in',
|
|
|
|
from: '4930609854180',
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
to: '4930609811111',
|
|
|
|
to_comment: 'Bob Smith',
|
|
|
|
call_id: '00005',
|
|
|
|
comment: '',
|
|
|
|
state: 'hangup',
|
|
|
|
start: Time.zone.now - 15.seconds,
|
|
|
|
'end': Time.zone.now,
|
|
|
|
preferences: {
|
|
|
|
from: [
|
|
|
|
{
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
comment: nil,
|
|
|
|
level: 'known',
|
|
|
|
object: 'User',
|
|
|
|
o_id: 2,
|
|
|
|
user_id: 2,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
Cti::Log.create(
|
|
|
|
direction: 'in',
|
|
|
|
from: '4930609854180',
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
to: '4930609811111',
|
|
|
|
to_comment: '',
|
|
|
|
call_id: '00006',
|
|
|
|
comment: '',
|
|
|
|
state: 'hangup',
|
|
|
|
start: Time.zone.now - 15.seconds,
|
|
|
|
'end': Time.zone.now,
|
|
|
|
preferences: {
|
|
|
|
from: [
|
|
|
|
{
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
comment: nil,
|
|
|
|
level: 'known',
|
|
|
|
object: 'User',
|
|
|
|
o_id: 2,
|
|
|
|
user_id: 2,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
Cti::Log.create(
|
|
|
|
direction: 'in',
|
|
|
|
from: '4930609854180',
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
to: '4930609811111',
|
|
|
|
to_comment: 'Bob Smith',
|
|
|
|
call_id: '00007',
|
|
|
|
comment: '',
|
|
|
|
state: 'hangup',
|
|
|
|
start: Time.zone.now - 15.seconds,
|
|
|
|
'end': Time.zone.now,
|
|
|
|
preferences: {
|
|
|
|
from: [
|
|
|
|
{
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
comment: nil,
|
|
|
|
level: 'maybe',
|
|
|
|
object: 'User',
|
|
|
|
o_id: 2,
|
|
|
|
user_id: 2,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-01-14 05:23:58 +00:00
|
|
|
Cti::Log.create(
|
|
|
|
direction: 'in',
|
|
|
|
from: '4930609854180',
|
|
|
|
to: '4930609811112',
|
|
|
|
call_id: '00008',
|
|
|
|
comment: '',
|
|
|
|
state: 'hangup',
|
|
|
|
start: Time.zone.now - 20.seconds,
|
|
|
|
'end': Time.zone.now,
|
|
|
|
preferences: {}
|
|
|
|
)
|
|
|
|
|
2016-04-23 09:04:33 +00:00
|
|
|
=end
|
|
|
|
|
2016-04-29 08:23:12 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Cti::Log.log
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
list: [...]
|
|
|
|
assets: {...}
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.log
|
|
|
|
list = Cti::Log.order('created_at DESC, id DESC').limit(60)
|
|
|
|
|
|
|
|
# add assets
|
2018-05-28 06:42:04 +00:00
|
|
|
assets = list.map(&:preferences)
|
|
|
|
.map { |p| p.slice(:from, :to) }
|
|
|
|
.map(&:values).flatten
|
|
|
|
.map { |caller_id| caller_id[:user_id] }.compact
|
|
|
|
.map { |user_id| User.lookup(id: user_id) }.compact
|
|
|
|
.each.with_object({}) { |user, a| user.assets(a) }
|
2016-04-29 08:23:12 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
list: list,
|
|
|
|
assets: assets,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-09-05 19:53:37 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
processes a incoming event
|
|
|
|
|
|
|
|
Cti::Log.process(
|
|
|
|
'cause' => '',
|
|
|
|
'event' => 'newCall',
|
|
|
|
'user' => 'user 1',
|
|
|
|
'from' => '4912347114711',
|
|
|
|
'to' => '4930600000000',
|
2018-05-28 23:45:29 +00:00
|
|
|
'callId' => '4991155921769858278-1', # or call_id
|
2017-09-05 19:53:37 +00:00
|
|
|
'direction' => 'in',
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-11-29 20:12:27 +00:00
|
|
|
def self.process(params)
|
|
|
|
comment = params['cause']
|
|
|
|
event = params['event']
|
|
|
|
user = params['user']
|
2018-05-28 23:45:29 +00:00
|
|
|
call_id = params['callId'] || params['call_id']
|
2016-11-30 07:04:35 +00:00
|
|
|
if user.class == Array
|
2016-11-29 20:12:27 +00:00
|
|
|
user = user.join(', ')
|
|
|
|
end
|
|
|
|
|
|
|
|
from_comment = nil
|
|
|
|
to_comment = nil
|
|
|
|
preferences = nil
|
|
|
|
if params['direction'] == 'in'
|
|
|
|
to_comment = user
|
2016-11-30 07:04:35 +00:00
|
|
|
from_comment, preferences = CallerId.get_comment_preferences(params['from'], 'from')
|
2016-11-29 20:12:27 +00:00
|
|
|
else
|
|
|
|
from_comment = user
|
2016-11-30 07:04:35 +00:00
|
|
|
to_comment, preferences = CallerId.get_comment_preferences(params['to'], 'to')
|
2016-11-29 20:12:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
case event
|
|
|
|
when 'newCall'
|
2016-11-30 07:04:35 +00:00
|
|
|
create(
|
2016-11-29 20:12:27 +00:00
|
|
|
direction: params['direction'],
|
|
|
|
from: params['from'],
|
|
|
|
from_comment: from_comment,
|
|
|
|
to: params['to'],
|
|
|
|
to_comment: to_comment,
|
2018-05-28 23:45:29 +00:00
|
|
|
call_id: call_id,
|
2016-11-29 20:12:27 +00:00
|
|
|
comment: comment,
|
|
|
|
state: event,
|
|
|
|
preferences: preferences,
|
|
|
|
)
|
|
|
|
when 'answer'
|
2018-05-28 23:45:29 +00:00
|
|
|
log = find_by(call_id: call_id)
|
|
|
|
raise "No such call_id #{call_id}" if !log
|
2016-11-29 20:12:27 +00:00
|
|
|
log.state = 'answer'
|
|
|
|
log.start = Time.zone.now
|
|
|
|
if user
|
|
|
|
log.to_comment = user
|
|
|
|
end
|
|
|
|
log.comment = comment
|
|
|
|
log.save
|
|
|
|
when 'hangup'
|
2018-05-28 23:45:29 +00:00
|
|
|
log = find_by(call_id: call_id)
|
|
|
|
raise "No such call_id #{call_id}" if !log
|
2016-11-29 20:12:27 +00:00
|
|
|
if params['direction'] == 'in' && log.state == 'newCall'
|
|
|
|
log.done = false
|
|
|
|
end
|
|
|
|
if params['direction'] == 'in' && log.to_comment == 'voicemail'
|
|
|
|
log.done = false
|
|
|
|
end
|
|
|
|
log.state = 'hangup'
|
|
|
|
log.end = Time.zone.now
|
|
|
|
log.comment = comment
|
|
|
|
log.save
|
|
|
|
else
|
2017-09-08 08:28:34 +00:00
|
|
|
raise ArgumentError, "Unknown event #{event.inspect}"
|
2016-11-29 20:12:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-23 09:04:33 +00:00
|
|
|
def push_event
|
2016-08-12 16:39:09 +00:00
|
|
|
users = User.with_permissions('cti.agent')
|
2017-10-01 12:25:52 +00:00
|
|
|
users.each do |user|
|
2016-04-23 09:04:33 +00:00
|
|
|
|
|
|
|
# send notify about event
|
|
|
|
Sessions.send_to(
|
|
|
|
user.id,
|
|
|
|
{
|
|
|
|
event: 'cti_event',
|
|
|
|
data: self,
|
|
|
|
},
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-04-23 09:04:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def push_caller_list
|
2016-04-29 08:23:12 +00:00
|
|
|
list = Cti::Log.log
|
2016-04-23 09:04:33 +00:00
|
|
|
|
2016-08-12 16:39:09 +00:00
|
|
|
users = User.with_permissions('cti.agent')
|
2017-10-01 12:25:52 +00:00
|
|
|
users.each do |user|
|
2016-04-23 09:04:33 +00:00
|
|
|
|
|
|
|
# send notify on create/update/delete
|
|
|
|
Sessions.send_to(
|
|
|
|
user.id,
|
|
|
|
{
|
|
|
|
event: 'cti_list_push',
|
|
|
|
data: list,
|
|
|
|
},
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-04-23 09:04:33 +00:00
|
|
|
end
|
2017-07-14 15:20:48 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
cleanup caller logs
|
|
|
|
|
|
|
|
Cti::Log.cleanup
|
|
|
|
|
|
|
|
optional you can put the max oldest chat entries as argument
|
|
|
|
|
|
|
|
Cti::Log.cleanup(12.months)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.cleanup(diff = 12.months)
|
|
|
|
Cti::Log.where('created_at < ?', Time.zone.now - diff).delete_all
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-05-28 06:42:04 +00:00
|
|
|
# adds virtual attributes when rendering #to_json
|
|
|
|
# see http://api.rubyonrails.org/classes/ActiveModel/Serialization.html
|
|
|
|
def attributes
|
|
|
|
virtual_attributes = {
|
|
|
|
'from_pretty' => from_pretty,
|
|
|
|
'to_pretty' => to_pretty,
|
|
|
|
}
|
|
|
|
|
|
|
|
super.merge(virtual_attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_pretty
|
|
|
|
parsed = TelephoneNumber.parse(from&.sub(/^\+?/, '+'))
|
|
|
|
parsed.send(parsed.valid? ? :international_number : :original_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_pretty
|
|
|
|
parsed = TelephoneNumber.parse(to&.sub(/^\+?/, '+'))
|
|
|
|
parsed.send(parsed.valid? ? :international_number : :original_number)
|
|
|
|
end
|
2016-04-23 09:04:33 +00:00
|
|
|
end
|
|
|
|
end
|