Class: Telephony::Events::Base

Inherits:
Base
  • Object
show all
Defined in:
app/models/telephony/events/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_last_for_agent(agent) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/telephony/events/base.rb', line 23

def self.find_last_for_agent(agent)
  return new_default_event unless agent.on_a_call?

  call = agent.active_call
  return new_default_event unless call

  events = call.conversation.events.order(:id).reverse_order

  events.detect do |event|
    event.for_agent?(agent)
  end || new_default_event
end

.log(args) ⇒ Object



16
17
18
19
20
21
# File 'app/models/telephony/events/base.rb', line 16

def self.log(args)
  klass = "Telephony::Events::#{args[:name].to_s.camelcase}".constantize
  event = klass.new args[:data]
  event.message_data = event.publishable? ? event.agent_messages : []
  event.save
end

.new_default_eventObject



36
37
38
# File 'app/models/telephony/events/base.rb', line 36

def self.new_default_event
  InitializeWidget.new
end

Instance Method Details

#active_agent2Object



91
92
93
94
95
96
# File 'app/models/telephony/events/base.rb', line 91

def active_agent2
  return @active_agent2 if @active_agent2
  leg = conversation.active_agent_legs.last
  agent = leg.agent if leg
  @active_agent2 = agent if agent != agent1
end

#agent1Object



83
84
85
# File 'app/models/telephony/events/base.rb', line 83

def agent1
  @agent1 ||= conversation.first_active_agent
end

#agent2Object



87
88
89
# File 'app/models/telephony/events/base.rb', line 87

def agent2
  @agent2 ||= conversation.calls.last.agent
end

#agent_messagesObject



62
63
64
# File 'app/models/telephony/events/base.rb', line 62

def agent_messages
  []
end

#callObject



40
41
42
# File 'app/models/telephony/events/base.rb', line 40

def call
  conversation.calls.detect { |call| call.id == call_id }
end

#default_dataObject



66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/telephony/events/base.rb', line 66

def default_data
  {
    event_id: id,
    conversation_id: conversation.id,
    conversation_state: conversation.state,
    call_id: call_id,
    number: conversation.customer.number,
    loan_id: conversation.loan_id,
    owner: true
  }
end

#for_agent?(agent) ⇒ Boolean



78
79
80
81
# File 'app/models/telephony/events/base.rb', line 78

def for_agent?(agent)
  # TODO Remove a week after the launch of the message_data field
  agent.in?((message_data || []).map { |message| message[:agent] })
end

#publishObject



44
45
46
47
48
# File 'app/models/telephony/events/base.rb', line 44

def publish
  each_message do |agent, data|
    PusherEventPublisher.publish event_publisher_data(agent, data)
  end
end

#publishable?Boolean



58
59
60
# File 'app/models/telephony/events/base.rb', line 58

def publishable?
  false
end

#republish_only_for(current_agent) ⇒ Object



50
51
52
53
54
55
56
# File 'app/models/telephony/events/base.rb', line 50

def republish_only_for(current_agent)
  each_message do |agent, data|
    if agent == current_agent
      PusherEventPublisher.publish event_publisher_data(agent, data)
    end
  end
end