fix: emit bot handoff on assignment takeover

This commit is contained in:
Sojan Jose
2026-07-02 23:10:28 -07:00
parent 98592b6e62
commit 4c25024f74
2 changed files with 16 additions and 2 deletions
+13 -2
View File
@@ -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,
+3
View File
@@ -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