2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2015-11-13 14:15:44 +00:00
|
|
|
class Chat::Agent < ApplicationModel
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
belongs_to :created_by, class_name: 'User'
|
|
|
|
belongs_to :updated_by, class_name: 'User'
|
|
|
|
|
2015-11-13 14:15:44 +00:00
|
|
|
def seads_available
|
|
|
|
concurrent - active_chat_count
|
|
|
|
end
|
|
|
|
|
|
|
|
def active_chat_count
|
2017-11-23 08:09:44 +00:00
|
|
|
Chat::Session.where(state: %w[waiting running], user_id: updated_by_id).count
|
2015-11-13 14:15:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.state(user_id, state = nil)
|
|
|
|
chat_agent = Chat::Agent.find_by(
|
|
|
|
updated_by_id: user_id
|
|
|
|
)
|
|
|
|
if state.nil?
|
|
|
|
return false if !chat_agent
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-11-13 14:15:44 +00:00
|
|
|
return chat_agent.active
|
|
|
|
end
|
2020-11-27 13:08:30 +00:00
|
|
|
|
|
|
|
# ATTENTION: setter return value indicates whether `active` state has changed
|
2015-11-13 14:15:44 +00:00
|
|
|
if chat_agent
|
|
|
|
chat_agent.active = state
|
2020-11-27 13:08:30 +00:00
|
|
|
# always update `updated_at` to inform other Agent sessions
|
|
|
|
# that this Agent session is still active
|
2015-11-13 14:15:44 +00:00
|
|
|
chat_agent.updated_at = Time.zone.now
|
|
|
|
chat_agent.save
|
2020-11-27 13:08:30 +00:00
|
|
|
|
|
|
|
chat_agent.active_previously_changed?
|
2015-11-13 14:15:44 +00:00
|
|
|
else
|
|
|
|
Chat::Agent.create(
|
2018-12-19 17:31:51 +00:00
|
|
|
active: state,
|
2015-11-13 14:15:44 +00:00
|
|
|
updated_by_id: user_id,
|
|
|
|
created_by_id: user_id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_or_update(params)
|
|
|
|
chat_agent = Chat::Agent.find_by(
|
|
|
|
updated_by_id: params[:updated_by_id]
|
|
|
|
)
|
|
|
|
if chat_agent
|
2017-09-11 11:16:08 +00:00
|
|
|
chat_agent.update!(params)
|
2015-11-13 14:15:44 +00:00
|
|
|
else
|
|
|
|
Chat::Agent.create(params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|