chat_transfer.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. class Sessions::Event::ChatTransfer < Sessions::Event::ChatBase
  2. def run
  3. return super if super
  4. return if !permission_check('chat.agent', 'chat')
  5. # find chat session
  6. chat_session = Chat::Session.find_by(id: @payload['session_id'])
  7. if !chat_session
  8. return {
  9. event: 'chat_session_start',
  10. data: {
  11. state: 'failed',
  12. message: 'No session available.',
  13. },
  14. }
  15. end
  16. chat_ids_to_notify = [chat_session.chat_id, @payload['chat_id']]
  17. chat_session.chat_id = @payload['chat_id']
  18. chat_session.state = 'waiting'
  19. chat_session.save
  20. # send state update with sessions to agents
  21. Chat.broadcast_agent_state_update(chat_ids_to_notify)
  22. # send transfer message to client
  23. message = {
  24. event: 'chat_session_notice',
  25. data: {
  26. session_id: chat_session.session_id,
  27. message: 'Conversation transfered into other chat. Please stay tuned.',
  28. },
  29. }
  30. chat_session.send_to_recipients(message, @client_id)
  31. nil
  32. end
  33. end