From 4c25024f7404fa57e0b24333aa65ecc367a3fd02 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Thu, 2 Jul 2026 23:10:28 -0700 Subject: [PATCH] fix: emit bot handoff on assignment takeover --- app/models/conversation.rb | 15 +++++++++++++-- spec/services/action_service_spec.rb | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/models/conversation.rb b/app/models/conversation.rb index a4672edac..e401aacbd 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -121,7 +121,7 @@ class Conversation < ApplicationRecord before_create :determine_conversation_status before_create :ensure_waiting_since - after_update_commit :execute_after_update_commit_callbacks + after_update_commit :execute_after_update_commit_callbacks, :dispatch_agent_bot_handoff after_create_commit :notify_conversation_creation after_create_commit :load_attributes_created_by_db_triggers before_destroy :set_unread_count_deletion_data @@ -274,7 +274,12 @@ class Conversation < ApplicationRecord def handle_agent_bot_takeover_by_assignee return if assignee_id.blank? || assignee_agent_bot_id.blank? - self.status = :open if pending? + if pending? + self.status = :open + self.waiting_since ||= Time.current + @agent_bot_handoff_by_assignee = true + end + self.assignee_agent_bot_id = nil end @@ -341,6 +346,12 @@ class Conversation < ApplicationRecord end end + def dispatch_agent_bot_handoff + return unless @agent_bot_handoff_by_assignee + + dispatcher_dispatch(CONVERSATION_BOT_HANDOFF) + end + def dispatcher_dispatch(event_name, changed_attributes = nil) Rails.configuration.dispatcher.dispatch(event_name, Time.zone.now, conversation: self, notifiable_assignee_change: notifiable_assignee_change?, changed_attributes: changed_attributes, diff --git a/spec/services/action_service_spec.rb b/spec/services/action_service_spec.rb index 08e89cc09..e2d910fbd 100644 --- a/spec/services/action_service_spec.rb +++ b/spec/services/action_service_spec.rb @@ -61,6 +61,7 @@ describe ActionService do 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) + allow(Rails.configuration.dispatcher).to receive(:dispatch) action_service.assign_agent([agent.id]) @@ -68,6 +69,8 @@ describe ActionService do expect(conversation.assignee).to eq(agent) expect(conversation.assignee_agent_bot).to be_nil expect(conversation.status).to eq('open') + expect(Rails.configuration.dispatcher).to have_received(:dispatch) + .with(Events::Types::CONVERSATION_BOT_HANDOFF, kind_of(Time), hash_including(conversation: conversation)) end end