fix: clear bot owner when humans open conversations

This commit is contained in:
Sojan Jose
2026-07-22 15:34:52 -07:00
parent c1ef2c3039
commit e19799d473
2 changed files with 9 additions and 10 deletions
@@ -88,7 +88,7 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro
else
@status = @conversation.toggle_status
end
assign_conversation if should_assign_conversation?
update_assignment_after_open if @conversation.open? && Current.user.is_a?(User)
end
def pending_to_open_by_bot?
@@ -97,10 +97,6 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro
@conversation.status == 'pending' && params[:status] == 'open'
end
def should_assign_conversation?
@conversation.status == 'open' && Current.user.is_a?(User) && Current.user&.agent?
end
def toggle_priority
@conversation.toggle_priority(params[:priority])
head :ok
@@ -183,8 +179,9 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro
@conversation.snoozed_until = parse_date_time(params[:snoozed_until].to_s) if params[:snoozed_until]
end
def assign_conversation
@conversation.assignee = current_user
def update_assignment_after_open
@conversation.assignee_agent_bot = nil
@conversation.assignee = current_user if current_user.agent?
@conversation.save!
end
@@ -565,15 +565,17 @@ RSpec.describe 'Conversations API', type: :request do
expect(conversation.reload.assignee_id).to eq(agent.id)
end
it 'disbale self assign if admin changes the conversation status to open' do
conversation.update!(status: 'pending')
conversation.update!(assignee_id: nil)
it 'does not self assign and clears the agent bot owner if admin changes the conversation status to open' do
conversation.update!(status: 'pending', assignee: nil, assignee_agent_bot: agent_bot)
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status",
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(conversation.reload.status).to eq('open')
expect(conversation.reload.assignee_id).not_to eq(administrator.id)
expect(conversation.reload.assignee_agent_bot).to be_nil
end
it 'toggles the conversation status to specific status when parameter is passed' do