agent.rb 915 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. class Chat::Agent < ApplicationModel
  2. def seads_available
  3. concurrent - active_chat_count
  4. end
  5. def active_chat_count
  6. Chat::Session.where(state: %w[waiting running], user_id: updated_by_id).count
  7. end
  8. def self.state(user_id, state = nil)
  9. chat_agent = Chat::Agent.find_by(
  10. updated_by_id: user_id
  11. )
  12. if state.nil?
  13. return false if !chat_agent
  14. return chat_agent.active
  15. end
  16. if chat_agent
  17. chat_agent.active = state
  18. chat_agent.updated_at = Time.zone.now
  19. chat_agent.save
  20. else
  21. Chat::Agent.create(
  22. active: state,
  23. updated_by_id: user_id,
  24. created_by_id: user_id,
  25. )
  26. end
  27. end
  28. def self.create_or_update(params)
  29. chat_agent = Chat::Agent.find_by(
  30. updated_by_id: params[:updated_by_id]
  31. )
  32. if chat_agent
  33. chat_agent.update!(params)
  34. else
  35. Chat::Agent.create(params)
  36. end
  37. end
  38. end