diff --git a/app/models/concerns/assignment_handler.rb b/app/models/concerns/assignment_handler.rb index a6b1422b7..5e57d985c 100644 --- a/app/models/concerns/assignment_handler.rb +++ b/app/models/concerns/assignment_handler.rb @@ -3,7 +3,7 @@ module AssignmentHandler include Events::Types included do - before_save :ensure_assignee_is_from_team + before_validation :ensure_assignee_is_from_team after_commit :notify_assignment_change, :process_assignment_changes end @@ -14,10 +14,6 @@ module AssignmentHandler validate_current_assignee_team self.assignee ||= find_assignee_from_team - return if assignee_id.blank? - - self.status = :open if assignee_agent_bot_id.present? && pending? - self.assignee_agent_bot = nil end def validate_current_assignee_team diff --git a/app/models/conversation.rb b/app/models/conversation.rb index d9c06c4d8..22d5761bd 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -274,6 +274,7 @@ class Conversation < ApplicationRecord def reset_agent_bot_when_assignee_present return if assignee_id.blank? + self.status = :open if assignee_agent_bot_id.present? && pending? self.assignee_agent_bot_id = nil end diff --git a/app/services/conversations/assignment_service.rb b/app/services/conversations/assignment_service.rb index d8f647aba..c73d41342 100644 --- a/app/services/conversations/assignment_service.rb +++ b/app/services/conversations/assignment_service.rb @@ -14,9 +14,8 @@ class Conversations::AssignmentService attr_reader :conversation, :assignee_id, :assignee_type def assign_agent - conversation.status = :open if assignee && conversation.assignee_agent_bot_id.present? && conversation.pending? conversation.assignee = assignee - conversation.assignee_agent_bot = nil + conversation.assignee_agent_bot = nil if assignee.blank? conversation.save! assignee end diff --git a/spec/services/action_service_spec.rb b/spec/services/action_service_spec.rb index 99ac2afc7..08e89cc09 100644 --- a/spec/services/action_service_spec.rb +++ b/spec/services/action_service_spec.rb @@ -57,6 +57,18 @@ describe ActionService do action_service.assign_agent([agent.id]) expect(conversation.reload.assignee).to eq(agent) end + + it 'opens bot-owned pending conversations when assigning the agent' do + inbox_member + conversation.update!(assignee: nil, assignee_agent_bot: create(:agent_bot, account: account), status: :pending) + + action_service.assign_agent([agent.id]) + + conversation.reload + expect(conversation.assignee).to eq(agent) + expect(conversation.assignee_agent_bot).to be_nil + expect(conversation.status).to eq('open') + end end context 'when agent is unconfirmed' do