Class: Telephony::Agent

Inherits:
Base
  • Object
show all
Includes:
AgentStateMachine
Defined in:
app/models/telephony/agent.rb

Defined Under Namespace

Modules: PhoneType

Constant Summary collapse

OFFLINE =
"offline"
AVAILABLE =
"available"
NOT_AVAILABLE =
"not_available"
ON_A_CALL =
"on_a_call"
STATUS_SORT_ORDER =
{
  AVAILABLE => 0,
  ON_A_CALL => 1,
  NOT_AVAILABLE => 2,
  OFFLINE => 3
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_transferable_for_csr_id(csr_id) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/telephony/agent.rb', line 56

def self.all_transferable_for_csr_id csr_id
  agent = find_by_csr_id csr_id

  transferables = select([:id, :csr_id, :csr_type, :status, :name, :phone_ext, :phone_number])

  if agent && agent.transferable_agents.count > 0
    transferables = transferables.where(csr_id: agent.transferable_agents)
  end

  sort_by_status(transferables)
end

.find_with_lock(agent_id) ⇒ Object



84
85
86
87
88
# File 'app/models/telephony/agent.rb', line 84

def self.find_with_lock agent_id
  transaction do
    yield find agent_id, lock: true
  end
end

.sort_by_status(agents) ⇒ Object



52
53
54
# File 'app/models/telephony/agent.rb', line 52

def self.sort_by_status agents
  agents.sort_by { |agent| STATUS_SORT_ORDER[agent.status] }
end

.update_or_create_by_widget_data(data) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/telephony/agent.rb', line 68

def self.update_or_create_by_widget_data data
  agent = find_or_create_by_csr_id data[:csr_id]
  agent.update_attributes({
    csr_type: data[:csr_type],
    name: data[:csr_name],
    generate_caller_id: data[:csr_generate_caller_id],
    phone_number: data[:csr_phone_number],
    phone_ext: data[:csr_phone_ext],
    sip_number: data[:csr_sip_number],
    call_center_name: data[:csr_call_center_name],
    phone_type: data[:csr_phone_type].present? ? data[:csr_phone_type] : PhoneType::PHONE,
    transferable_agents: data[:csr_transferable_agents].present? ? JSON.parse(data[:csr_transferable_agents]) : []
  })
  agent
end

Instance Method Details

#active_callObject



118
119
120
# File 'app/models/telephony/agent.rb', line 118

def active_call
  calls.where("state <> 'terminated'").last
end

#active_conversation_idObject



122
123
124
125
126
# File 'app/models/telephony/agent.rb', line 122

def active_conversation_id
  return nil unless on_a_call?
  call = active_call
  call.conversation_id if call
end

#encode_with(coder) ⇒ Object



161
162
163
164
165
166
# File 'app/models/telephony/agent.rb', line 161

def encode_with coder
  coder['attributes'] = {
    'id'     => attributes['id'],
    'csr_id' => attributes['csr_id']
  }
end

#number(with_protocol = false) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'app/models/telephony/agent.rb', line 150

def number with_protocol = false
  case phone_type
  when PhoneType::SIP
    sip_string with_protocol
  when PhoneType::TWILIO_CLIENT
    client_string with_protocol
  else
    phone_number
  end
end

#process_presence_event(event, timestamp) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/telephony/agent.rb', line 94

def process_presence_event event, timestamp
  if event == 'member_added'
    update_status_by_event :came_online, timestamp
  else
    job = Telephony::Jobs::AgentOffline.new id, timestamp
    if Telephony::DELAYED_JOB.respond_to?(:enqueue)
      Telephony::DELAYED_JOB.enqueue job, run_at: 15.minutes.from_now
    else
      job.update_status
    end
  end
end

#publish_status_changeObject



141
142
143
144
145
146
147
148
# File 'app/models/telephony/agent.rb', line 141

def publish_status_change
  PusherEventPublisher.publish channel: "csrs-#{csr_id}",
    name: 'statusChange',
    data: {
      status: status,
      timestamp: Integer(Time.now.to_f * 1000)
    }
end

#terminate_active_callObject



114
115
116
# File 'app/models/telephony/agent.rb', line 114

def terminate_active_call
  active_call.terminate! if active_call
end

#transferrable?Boolean



90
91
92
# File 'app/models/telephony/agent.rb', line 90

def transferrable?
  available?
end

#update_status_by_event(presence_event, timestamp) ⇒ Object



107
108
109
110
111
112
# File 'app/models/telephony/agent.rb', line 107

def update_status_by_event presence_event, timestamp
  return if timestamp_of_last_presence_event > timestamp

  self.send presence_event.to_sym
  self.update_attribute :timestamp_of_last_presence_event, timestamp
end

#verify_status!Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/telephony/agent.rb', line 128

def verify_status!
  return unless on_a_call?
  call = active_call

  if call.nil?
    with_lock { call_ended! if (active_call.nil? and on_a_call?) }
  elsif call.created_at < 5.minutes.ago && Telephony.provider.call_ended?(call.sid)
    Conversation.find_with_lock(call.conversation_id) do
      call.reload.terminate
    end
  end
end