agent.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Chat::Agent < ApplicationModel
  3. include HasDefaultModelUserRelations
  4. def seads_available
  5. concurrent - active_chat_count
  6. end
  7. def active_chat_count
  8. Chat::Session.where(state: %w[waiting running], user_id: updated_by_id).count
  9. end
  10. def self.state(user_id, state = nil)
  11. chat_agent = Chat::Agent.find_by(
  12. updated_by_id: user_id
  13. )
  14. if state.nil?
  15. return false if !chat_agent
  16. return chat_agent.active
  17. end
  18. # ATTENTION: setter return value indicates whether `active` state has changed
  19. if chat_agent
  20. chat_agent.active = state
  21. # always update `updated_at` to inform other Agent sessions
  22. # that this Agent session is still active
  23. chat_agent.updated_at = Time.zone.now
  24. chat_agent.save
  25. chat_agent.active_previously_changed?
  26. else
  27. Chat::Agent.create(
  28. active: state,
  29. updated_by_id: user_id,
  30. created_by_id: user_id,
  31. )
  32. end
  33. end
  34. def self.create_or_update(params)
  35. chat_agent = Chat::Agent.find_by(
  36. updated_by_id: params[:updated_by_id]
  37. )
  38. if chat_agent
  39. chat_agent.update!(params)
  40. else
  41. Chat::Agent.create(params)
  42. end
  43. end
  44. end