agent.rb 1.3 KB

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