diff --git a/app/models/conversation.rb b/app/models/conversation.rb index d9c06c4d8..fb5701765 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -283,12 +283,21 @@ class Conversation < ApplicationRecord return handle_campaign_status if campaign.present? # TODO: make this an inbox config instead of assuming bot conversations should start as pending + assign_connected_agent_bot if inbox.agent_bot_inbox&.active? self.status = :pending if inbox.active_bot? end def handle_campaign_status # If campaign has no sender (bot-initiated) and inbox has active bot, let bot handle it - self.status = :pending if campaign.sender_id.nil? && inbox.active_bot? + return unless campaign.sender_id.nil? && inbox.active_bot? + + assign_connected_agent_bot if inbox.agent_bot_inbox&.active? + self.status = :pending + end + + def assign_connected_agent_bot + self.assignee = nil + self.assignee_agent_bot = inbox.agent_bot end def notify_conversation_creation diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 58d64ea94..0e15920eb 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -658,6 +658,11 @@ RSpec.describe Conversation do expect(conversation.status).to eq('pending') end + it 'sets connected agent bot as the conversation owner' do + expect(conversation.assignee_agent_bot).to eq(bot_inbox.agent_bot) + expect(conversation.assignee).to be_nil + end + context 'with campaigns' do let(:user) { create(:user, account: bot_inbox.inbox.account) } @@ -665,12 +670,14 @@ RSpec.describe Conversation do campaign = create(:campaign, inbox: bot_inbox.inbox, account: bot_inbox.inbox.account, sender: user) conversation = create(:conversation, inbox: bot_inbox.inbox, campaign: campaign) expect(conversation.status).to eq('open') + expect(conversation.assignee_agent_bot).to be_nil end it 'returns conversation as pending if campaign has no sender (bot-initiated) and bot is active' do campaign = create(:campaign, inbox: bot_inbox.inbox, account: bot_inbox.inbox.account, sender: nil) conversation = create(:conversation, inbox: bot_inbox.inbox, campaign: campaign) expect(conversation.status).to eq('pending') + expect(conversation.assignee_agent_bot).to eq(bot_inbox.agent_bot) end end @@ -701,6 +708,10 @@ RSpec.describe Conversation do it 'returns conversation status as pending' do expect(conversation.status).to eq('pending') end + + it 'does not set agent bot ownership' do + expect(conversation.assignee_agent_bot).to be_nil + end end describe '#delete conversation' do