2016-04-23 09:04:33 +00:00
|
|
|
|
module Cti
|
|
|
|
|
class Log < ApplicationModel
|
2018-10-03 06:23:22 +00:00
|
|
|
|
include HasSearchIndexBackend
|
|
|
|
|
|
2016-04-23 09:04:33 +00:00
|
|
|
|
self.table_name = 'cti_logs'
|
|
|
|
|
|
2016-04-29 08:23:12 +00:00
|
|
|
|
store :preferences
|
|
|
|
|
|
2018-09-25 14:52:46 +00:00
|
|
|
|
validates :state, format: { with: /\A(newCall|answer|hangup)\z/, message: 'newCall|answer|hangup is allowed' }
|
|
|
|
|
|
2018-08-17 14:12:28 +00:00
|
|
|
|
after_commit :push_incoming_call, :push_caller_list_update
|
2016-04-23 09:04:33 +00:00
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
Cti::Log.create!(
|
2016-04-23 09:04:33 +00:00
|
|
|
|
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',
|
|
|
|
|
)
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
Cti::Log.create!(
|
2016-04-23 09:04:33 +00:00
|
|
|
|
direction: 'in',
|
|
|
|
|
from: '007',
|
|
|
|
|
from_comment: '',
|
|
|
|
|
to: '008',
|
|
|
|
|
to_comment: '',
|
|
|
|
|
call_id: '2',
|
|
|
|
|
comment: '',
|
|
|
|
|
state: 'answer',
|
|
|
|
|
)
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
Cti::Log.create!(
|
2016-04-23 09:04:33 +00:00
|
|
|
|
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
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
Cti::Log.create!(
|
2016-05-31 08:56:13 +00:00
|
|
|
|
direction: 'in',
|
|
|
|
|
from: '4930609854180',
|
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
|
to: '4930609811111',
|
|
|
|
|
to_comment: 'Bob Smith',
|
2018-08-17 07:48:12 +00:00
|
|
|
|
call_id: '435452113',
|
2016-05-31 08:56:13 +00:00
|
|
|
|
comment: '',
|
|
|
|
|
state: 'newCall',
|
|
|
|
|
done: false,
|
|
|
|
|
preferences: {
|
|
|
|
|
from: [
|
|
|
|
|
{
|
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
|
comment: nil,
|
|
|
|
|
level: 'known',
|
|
|
|
|
object: 'User',
|
|
|
|
|
o_id: 2,
|
|
|
|
|
user_id: 2,
|
|
|
|
|
}
|
|
|
|
|
]
|
2018-10-18 09:42:37 +00:00
|
|
|
|
},
|
|
|
|
|
created_at: Time.zone.now,
|
2016-05-31 08:56:13 +00:00
|
|
|
|
)
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
Cti::Log.create!(
|
2016-05-31 08:56:13 +00:00
|
|
|
|
direction: 'out',
|
|
|
|
|
from: '4930609854180',
|
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
|
to: '4930609811111',
|
|
|
|
|
to_comment: 'Bob Smith',
|
2018-08-17 07:48:12 +00:00
|
|
|
|
call_id: rand(999_999_999),
|
2016-05-31 08:56:13 +00:00
|
|
|
|
comment: '',
|
|
|
|
|
state: 'newCall',
|
|
|
|
|
preferences: {
|
|
|
|
|
to: [
|
|
|
|
|
{
|
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
|
comment: nil,
|
|
|
|
|
level: 'known',
|
|
|
|
|
object: 'User',
|
|
|
|
|
o_id: 2,
|
|
|
|
|
user_id: 2,
|
|
|
|
|
}
|
|
|
|
|
]
|
2018-10-18 09:42:37 +00:00
|
|
|
|
},
|
|
|
|
|
created_at: Time.zone.now - 20.seconds,
|
2016-05-31 08:56:13 +00:00
|
|
|
|
)
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
Cti::Log.create!(
|
2016-05-31 08:56:13 +00:00
|
|
|
|
direction: 'in',
|
|
|
|
|
from: '4930609854180',
|
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
|
to: '4930609811111',
|
|
|
|
|
to_comment: 'Bob Smith',
|
2018-08-17 07:48:12 +00:00
|
|
|
|
call_id: rand(999_999_999),
|
2016-05-31 08:56:13 +00:00
|
|
|
|
comment: '',
|
|
|
|
|
state: 'answer',
|
|
|
|
|
preferences: {
|
|
|
|
|
from: [
|
|
|
|
|
{
|
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
|
comment: nil,
|
|
|
|
|
level: 'known',
|
|
|
|
|
object: 'User',
|
|
|
|
|
o_id: 2,
|
|
|
|
|
user_id: 2,
|
|
|
|
|
}
|
|
|
|
|
]
|
2018-10-18 09:42:37 +00:00
|
|
|
|
},
|
|
|
|
|
initialized_at: Time.zone.now - 20.seconds,
|
|
|
|
|
start_at: Time.zone.now - 30.seconds,
|
|
|
|
|
duration_waiting_time: 20,
|
|
|
|
|
created_at: Time.zone.now - 20.seconds,
|
2016-05-31 08:56:13 +00:00
|
|
|
|
)
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
Cti::Log.create!(
|
2016-05-31 08:56:13 +00:00
|
|
|
|
direction: 'in',
|
|
|
|
|
from: '4930609854180',
|
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
|
to: '4930609811111',
|
|
|
|
|
to_comment: 'Bob Smith',
|
2018-08-17 07:48:12 +00:00
|
|
|
|
call_id: rand(999_999_999),
|
2016-05-31 08:56:13 +00:00
|
|
|
|
comment: '',
|
|
|
|
|
state: 'hangup',
|
|
|
|
|
comment: 'normalClearing',
|
|
|
|
|
done: false,
|
|
|
|
|
preferences: {
|
|
|
|
|
from: [
|
|
|
|
|
{
|
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
|
comment: nil,
|
|
|
|
|
level: 'known',
|
|
|
|
|
object: 'User',
|
|
|
|
|
o_id: 2,
|
|
|
|
|
user_id: 2,
|
|
|
|
|
}
|
|
|
|
|
]
|
2018-10-18 09:42:37 +00:00
|
|
|
|
},
|
|
|
|
|
initialized_at: Time.zone.now - 80.seconds,
|
|
|
|
|
start_at: Time.zone.now - 45.seconds,
|
|
|
|
|
end_at: Time.zone.now,
|
|
|
|
|
duration_waiting_time: 35,
|
|
|
|
|
duration_talking_time: 45,
|
|
|
|
|
created_at: Time.zone.now - 80.seconds,
|
2016-05-31 08:56:13 +00:00
|
|
|
|
)
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
Cti::Log.create!(
|
2016-05-31 08:56:13 +00:00
|
|
|
|
direction: 'in',
|
|
|
|
|
from: '4930609854180',
|
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
|
to: '4930609811111',
|
|
|
|
|
to_comment: 'Bob Smith',
|
2018-08-17 07:48:12 +00:00
|
|
|
|
call_id: rand(999_999_999),
|
2016-05-31 08:56:13 +00:00
|
|
|
|
comment: '',
|
|
|
|
|
state: 'hangup',
|
2018-08-09 21:06:34 +00:00
|
|
|
|
start_at: Time.zone.now - 15.seconds,
|
|
|
|
|
end_at: Time.zone.now,
|
2016-05-31 08:56:13 +00:00
|
|
|
|
preferences: {
|
|
|
|
|
from: [
|
|
|
|
|
{
|
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
|
comment: nil,
|
|
|
|
|
level: 'known',
|
|
|
|
|
object: 'User',
|
|
|
|
|
o_id: 2,
|
|
|
|
|
user_id: 2,
|
|
|
|
|
}
|
|
|
|
|
]
|
2018-10-18 09:42:37 +00:00
|
|
|
|
},
|
|
|
|
|
initialized_at: Time.zone.now - 5.minutes,
|
|
|
|
|
start_at: Time.zone.now - 3.minutes,
|
|
|
|
|
end_at: Time.zone.now - 20.seconds,
|
|
|
|
|
duration_waiting_time: 120,
|
|
|
|
|
duration_talking_time: 160,
|
|
|
|
|
created_at: Time.zone.now - 5.minutes,
|
2016-05-31 08:56:13 +00:00
|
|
|
|
)
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
Cti::Log.create!(
|
2016-05-31 08:56:13 +00:00
|
|
|
|
direction: 'in',
|
|
|
|
|
from: '4930609854180',
|
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
|
to: '4930609811111',
|
|
|
|
|
to_comment: '',
|
2018-08-17 07:48:12 +00:00
|
|
|
|
call_id: rand(999_999_999),
|
2016-05-31 08:56:13 +00:00
|
|
|
|
comment: '',
|
|
|
|
|
state: 'hangup',
|
2018-08-09 21:06:34 +00:00
|
|
|
|
start_at: Time.zone.now - 15.seconds,
|
|
|
|
|
end_at: Time.zone.now,
|
2016-05-31 08:56:13 +00:00
|
|
|
|
preferences: {
|
|
|
|
|
from: [
|
|
|
|
|
{
|
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
|
comment: nil,
|
|
|
|
|
level: 'known',
|
|
|
|
|
object: 'User',
|
|
|
|
|
o_id: 2,
|
|
|
|
|
user_id: 2,
|
|
|
|
|
}
|
|
|
|
|
]
|
2018-10-18 09:42:37 +00:00
|
|
|
|
},
|
|
|
|
|
initialized_at: Time.zone.now - 60.minutes,
|
|
|
|
|
start_at: Time.zone.now - 59.minutes,
|
|
|
|
|
end_at: Time.zone.now - 2.minutes,
|
|
|
|
|
duration_waiting_time: 60,
|
|
|
|
|
duration_talking_time: 3420,
|
|
|
|
|
created_at: Time.zone.now - 60.minutes,
|
2016-05-31 08:56:13 +00:00
|
|
|
|
)
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
Cti::Log.create!(
|
2016-05-31 08:56:13 +00:00
|
|
|
|
direction: 'in',
|
|
|
|
|
from: '4930609854180',
|
|
|
|
|
from_comment: 'Franz Bauer',
|
|
|
|
|
to: '4930609811111',
|
|
|
|
|
to_comment: 'Bob Smith',
|
2018-08-17 07:48:12 +00:00
|
|
|
|
call_id: rand(999_999_999),
|
2016-05-31 08:56:13 +00:00
|
|
|
|
comment: '',
|
|
|
|
|
state: 'hangup',
|
2018-08-09 21:06:34 +00:00
|
|
|
|
start_at: Time.zone.now - 15.seconds,
|
|
|
|
|
end_at: Time.zone.now,
|
2016-05-31 08:56:13 +00:00
|
|
|
|
preferences: {
|
|
|
|
|
from: [
|
|
|
|
|
{
|
|
|
|
|
caller_id: '4930726128135',
|
|
|
|
|
comment: nil,
|
|
|
|
|
level: 'maybe',
|
|
|
|
|
object: 'User',
|
|
|
|
|
o_id: 2,
|
|
|
|
|
user_id: 2,
|
|
|
|
|
}
|
|
|
|
|
]
|
2018-10-18 09:42:37 +00:00
|
|
|
|
},
|
|
|
|
|
initialized_at: Time.zone.now - 240.minutes,
|
|
|
|
|
start_at: Time.zone.now - 235.minutes,
|
|
|
|
|
end_at: Time.zone.now - 222.minutes,
|
|
|
|
|
duration_waiting_time: 300,
|
|
|
|
|
duration_talking_time: 1080,
|
|
|
|
|
created_at: Time.zone.now - 240.minutes,
|
2016-05-31 08:56:13 +00:00
|
|
|
|
)
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
Cti::Log.create!(
|
2017-01-14 05:23:58 +00:00
|
|
|
|
direction: 'in',
|
|
|
|
|
from: '4930609854180',
|
|
|
|
|
to: '4930609811112',
|
2018-08-17 07:48:12 +00:00
|
|
|
|
call_id: rand(999_999_999),
|
2017-01-14 05:23:58 +00:00
|
|
|
|
comment: '',
|
|
|
|
|
state: 'hangup',
|
2018-08-09 21:06:34 +00:00
|
|
|
|
start_at: Time.zone.now - 20.seconds,
|
|
|
|
|
end_at: Time.zone.now,
|
2018-10-18 09:42:37 +00:00
|
|
|
|
preferences: {},
|
|
|
|
|
initialized_at: Time.zone.now - 1440.minutes,
|
|
|
|
|
start_at: Time.zone.now - 1430.minutes,
|
|
|
|
|
end_at: Time.zone.now - 1429.minutes,
|
|
|
|
|
duration_waiting_time: 600,
|
|
|
|
|
duration_talking_time: 660,
|
|
|
|
|
created_at: Time.zone.now - 1440.minutes,
|
2017-01-14 05:23:58 +00:00
|
|
|
|
)
|
|
|
|
|
|
2016-04-23 09:04:33 +00:00
|
|
|
|
=end
|
|
|
|
|
|
2016-04-29 08:23:12 +00:00
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
Cti::Log.log
|
|
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
|
|
{
|
2018-08-17 07:48:12 +00:00
|
|
|
|
list: [log_record1, log_record2, log_record3],
|
|
|
|
|
assets: {...},
|
2016-04-29 08:23:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def self.log
|
2018-08-17 07:48:12 +00:00
|
|
|
|
list = Cti::Log.log_records
|
2016-04-29 08:23:12 +00:00
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
|
list: list,
|
2016-04-29 08:23:12 +00:00
|
|
|
|
assets: assets,
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
Cti::Log.log_records
|
|
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
|
|
[log_record1, log_record2, log_record3]
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
def self.log_records
|
2019-04-07 15:23:03 +00:00
|
|
|
|
Cti::Log.order(created_at: :desc).limit(60)
|
2018-08-17 07:48:12 +00:00
|
|
|
|
end
|
|
|
|
|
|
2017-09-05 19:53:37 +00:00
|
|
|
|
=begin
|
|
|
|
|
|
|
|
|
|
processes a incoming event
|
|
|
|
|
|
|
|
|
|
Cti::Log.process(
|
2018-10-16 14:35:31 +00:00
|
|
|
|
cause: '',
|
|
|
|
|
event: 'newCall',
|
|
|
|
|
user: 'user 1',
|
|
|
|
|
from: '4912347114711',
|
|
|
|
|
to: '4930600000000',
|
|
|
|
|
callId: '43545211', # or call_id
|
|
|
|
|
direction: 'in',
|
|
|
|
|
queue: 'helpdesk', # optional
|
2017-09-05 19:53:37 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
2016-11-29 20:12:27 +00:00
|
|
|
|
def self.process(params)
|
2018-09-25 14:52:46 +00:00
|
|
|
|
cause = params['cause']
|
2016-11-29 20:12:27 +00:00
|
|
|
|
event = params['event']
|
|
|
|
|
user = params['user']
|
2018-10-16 14:35:31 +00:00
|
|
|
|
queue = params['queue']
|
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
|
2018-08-17 07:48:12 +00:00
|
|
|
|
done = true
|
2016-11-29 20:12:27 +00:00
|
|
|
|
if params['direction'] == 'in'
|
2018-10-18 09:42:37 +00:00
|
|
|
|
if user.present?
|
|
|
|
|
to_comment = user
|
|
|
|
|
elsif queue.present?
|
|
|
|
|
to_comment = queue
|
|
|
|
|
end
|
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
|
|
|
|
|
|
2018-09-25 14:52:46 +00:00
|
|
|
|
log = find_by(call_id: call_id)
|
|
|
|
|
|
2016-11-29 20:12:27 +00:00
|
|
|
|
case event
|
|
|
|
|
when 'newCall'
|
2018-08-17 07:48:12 +00:00
|
|
|
|
if params['direction'] == 'in'
|
|
|
|
|
done = false
|
|
|
|
|
end
|
2018-09-25 14:52:46 +00:00
|
|
|
|
raise "call_id #{call_id} already exists!" if log
|
2018-10-09 06:17:41 +00:00
|
|
|
|
|
2016-11-30 07:04:35 +00:00
|
|
|
|
create(
|
2018-12-19 17:31:51 +00:00
|
|
|
|
direction: params['direction'],
|
|
|
|
|
from: params['from'],
|
|
|
|
|
from_comment: from_comment,
|
|
|
|
|
to: params['to'],
|
|
|
|
|
to_comment: to_comment,
|
|
|
|
|
call_id: call_id,
|
|
|
|
|
comment: cause,
|
|
|
|
|
queue: queue,
|
|
|
|
|
state: event,
|
2018-08-09 21:06:34 +00:00
|
|
|
|
initialized_at: Time.zone.now,
|
2018-12-19 17:31:51 +00:00
|
|
|
|
preferences: preferences,
|
|
|
|
|
done: done,
|
2016-11-29 20:12:27 +00:00
|
|
|
|
)
|
|
|
|
|
when 'answer'
|
2018-05-28 23:45:29 +00:00
|
|
|
|
raise "No such call_id #{call_id}" if !log
|
2018-09-25 14:52:46 +00:00
|
|
|
|
return if log.state == 'hangup' # call is already hangup, ignore answer
|
2018-10-09 06:17:41 +00:00
|
|
|
|
|
2018-09-25 14:52:46 +00:00
|
|
|
|
log.with_lock do
|
|
|
|
|
log.state = 'answer'
|
|
|
|
|
log.start_at = Time.zone.now
|
|
|
|
|
log.duration_waiting_time = log.start_at.to_i - log.initialized_at.to_i
|
|
|
|
|
if user
|
|
|
|
|
log.to_comment = user
|
|
|
|
|
end
|
|
|
|
|
log.done = true
|
|
|
|
|
log.comment = cause
|
|
|
|
|
log.save
|
2016-11-29 20:12:27 +00:00
|
|
|
|
end
|
|
|
|
|
when 'hangup'
|
2018-05-28 23:45:29 +00:00
|
|
|
|
raise "No such call_id #{call_id}" if !log
|
2018-10-09 06:17:41 +00:00
|
|
|
|
|
2018-09-25 14:52:46 +00:00
|
|
|
|
log.with_lock do
|
|
|
|
|
log.done = done
|
|
|
|
|
if params['direction'] == 'in'
|
|
|
|
|
if log.state == 'newCall' && cause != 'forwarded'
|
|
|
|
|
log.done = false
|
|
|
|
|
elsif log.to_comment == 'voicemail'
|
|
|
|
|
log.done = false
|
|
|
|
|
end
|
2018-08-17 07:48:12 +00:00
|
|
|
|
end
|
2018-09-25 14:52:46 +00:00
|
|
|
|
log.state = 'hangup'
|
|
|
|
|
log.end_at = Time.zone.now
|
|
|
|
|
if log.start_at
|
2018-10-18 09:42:37 +00:00
|
|
|
|
log.duration_talking_time = log.end_at.to_i - log.start_at.to_i
|
2018-09-25 14:52:46 +00:00
|
|
|
|
elsif !log.duration_waiting_time && log.initialized_at
|
|
|
|
|
log.duration_waiting_time = log.end_at.to_i - log.initialized_at.to_i
|
|
|
|
|
end
|
|
|
|
|
log.comment = cause
|
|
|
|
|
log.save
|
2016-11-29 20:12:27 +00:00
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
def push_incoming_call
|
2018-08-17 14:12:28 +00:00
|
|
|
|
return true if destroyed?
|
|
|
|
|
return true if state != 'newCall'
|
|
|
|
|
return true if direction != 'in'
|
|
|
|
|
|
|
|
|
|
# send notify about 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
|
|
|
|
Sessions.send_to(
|
|
|
|
|
user.id,
|
|
|
|
|
{
|
|
|
|
|
event: 'cti_event',
|
2018-12-19 17:31:51 +00:00
|
|
|
|
data: self,
|
2016-04-23 09:04:33 +00:00
|
|
|
|
},
|
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
|
end
|
2018-08-17 14:12:28 +00:00
|
|
|
|
true
|
2016-04-23 09:04:33 +00:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-17 07:48:12 +00:00
|
|
|
|
def self.push_caller_list_update?(record)
|
|
|
|
|
list_ids = Cti::Log.log_records.pluck(:id)
|
2018-08-17 14:12:28 +00:00
|
|
|
|
return true if list_ids.include?(record.id)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
|
2018-08-17 14:12:28 +00:00
|
|
|
|
false
|
2018-08-17 07:48:12 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def push_caller_list_update
|
2018-08-17 14:12:28 +00:00
|
|
|
|
return false if !Cti::Log.push_caller_list_update?(self)
|
2016-04-23 09:04:33 +00:00
|
|
|
|
|
2018-08-17 14:12:28 +00:00
|
|
|
|
# send notify on create/update/delete
|
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
|
|
|
|
Sessions.send_to(
|
|
|
|
|
user.id,
|
|
|
|
|
{
|
|
|
|
|
event: 'cti_list_push',
|
|
|
|
|
},
|
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
|
end
|
2018-08-17 07:48:12 +00:00
|
|
|
|
true
|
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,
|
2018-12-19 17:31:51 +00:00
|
|
|
|
'to_pretty' => to_pretty,
|
2018-05-28 06:42:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|