123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- module Cti
- class Log < ApplicationModel
- self.table_name = 'cti_logs'
- store :preferences
- 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',
- from_comment: 'AAA',
- to: '008',
- to_comment: 'BBB',
- 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',
- )
- 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,
- }
- ]
- }
- )
- 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: {}
- )
- =end
- =begin
- Cti::Log.log
- returns
- {
- list: [...]
- assets: {...}
- }
- =end
- def self.log
- list = Cti::Log.order('created_at DESC, id DESC').limit(60)
- # add assets
- 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) }
- {
- list: list,
- assets: assets,
- }
- end
- =begin
- processes a incoming event
- Cti::Log.process(
- 'cause' => '',
- 'event' => 'newCall',
- 'user' => 'user 1',
- 'from' => '4912347114711',
- 'to' => '4930600000000',
- 'callId' => '4991155921769858278-1', # or call_id
- 'direction' => 'in',
- )
- =end
- def self.process(params)
- comment = params['cause']
- event = params['event']
- user = params['user']
- call_id = params['callId'] || params['call_id']
- if user.class == Array
- user = user.join(', ')
- end
- from_comment = nil
- to_comment = nil
- preferences = nil
- if params['direction'] == 'in'
- to_comment = user
- from_comment, preferences = CallerId.get_comment_preferences(params['from'], 'from')
- else
- from_comment = user
- to_comment, preferences = CallerId.get_comment_preferences(params['to'], 'to')
- end
- case event
- when 'newCall'
- create(
- direction: params['direction'],
- from: params['from'],
- from_comment: from_comment,
- to: params['to'],
- to_comment: to_comment,
- call_id: call_id,
- comment: comment,
- state: event,
- preferences: preferences,
- )
- when 'answer'
- log = find_by(call_id: call_id)
- raise "No such call_id #{call_id}" if !log
- log.state = 'answer'
- log.start = Time.zone.now
- if user
- log.to_comment = user
- end
- log.comment = comment
- log.save
- when 'hangup'
- log = find_by(call_id: call_id)
- raise "No such call_id #{call_id}" if !log
- 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
- raise ArgumentError, "Unknown event #{event.inspect}"
- end
- end
- def push_event
- users = User.with_permissions('cti.agent')
- users.each do |user|
- # send notify about event
- Sessions.send_to(
- user.id,
- {
- event: 'cti_event',
- data: self,
- },
- )
- end
- end
- def push_caller_list
- list = Cti::Log.log
- users = User.with_permissions('cti.agent')
- users.each do |user|
- # send notify on create/update/delete
- Sessions.send_to(
- user.id,
- {
- event: 'cti_list_push',
- data: list,
- },
- )
- end
- end
- =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
- # 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
- end
- end
|