From 10b9da14ef3aab5616cf4858bda16251d925a7d3 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Thu, 3 Jul 2025 19:51:58 +0530 Subject: [PATCH] fix: Conversation opening in pending if there's an active bot on the inbox --- app/builders/conversation_builder.rb | 17 +++++- .../helpers/composeConversationHelper.js | 1 + spec/builders/conversation_builder_spec.rb | 59 +++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/app/builders/conversation_builder.rb b/app/builders/conversation_builder.rb index 6a995b188..1591bfd53 100644 --- a/app/builders/conversation_builder.rb +++ b/app/builders/conversation_builder.rb @@ -14,7 +14,22 @@ class ConversationBuilder end def create_new_conversation - ::Conversation.create!(conversation_params) + conversation = ::Conversation.create!(conversation_params) + + # Override status if explicitly provided in params + # This is required because the Conversation model's determine_conversation_status callback + # automatically sets status to :pending when there's an active bot on the inbox. + # However, when users manually create conversations (e.g., via the frontend form), + # they should be able to explicitly set the status to :open to bypass bot handling. + # We use update_column to avoid triggering callbacks and activity logs since this + # is just correcting the status to match the user's explicit intent. + if params[:status].present? + # rubocop:disable Rails/SkipsModelValidations + conversation.update_column(:status, params[:status]) + # rubocop:enable Rails/SkipsModelValidations + end + + conversation end def conversation_params diff --git a/app/javascript/dashboard/components-next/NewConversation/helpers/composeConversationHelper.js b/app/javascript/dashboard/components-next/NewConversation/helpers/composeConversationHelper.js index 57e819245..de2917de9 100644 --- a/app/javascript/dashboard/components-next/NewConversation/helpers/composeConversationHelper.js +++ b/app/javascript/dashboard/components-next/NewConversation/helpers/composeConversationHelper.js @@ -134,6 +134,7 @@ export const prepareNewMessagePayload = ({ contactId: Number(selectedContact.id), message: { content: message }, assigneeId: currentUser.id, + status: 'open', // Explicitly set status to open for manually created conversations }; if (attachedFiles?.length) { diff --git a/spec/builders/conversation_builder_spec.rb b/spec/builders/conversation_builder_spec.rb index c0c5f248b..da446aa95 100644 --- a/spec/builders/conversation_builder_spec.rb +++ b/spec/builders/conversation_builder_spec.rb @@ -80,5 +80,64 @@ describe ConversationBuilder do expect(conversation.id).to eq(existing_conversation.id) end end + + context 'when status is provided in params' do + it 'sets the status to the provided value' do + conversation = described_class.new( + contact_inbox: contact_api_inbox, + params: { status: 'open' } + ).perform + + expect(conversation.status).to eq('open') + end + + it 'overrides bot status when status is explicitly provided' do + # Create a bot for the inbox + create(:agent_bot, account: account) + create(:agent_bot_inbox, inbox: api_inbox, account: account) + + conversation = described_class.new( + contact_inbox: contact_api_inbox, + params: { status: 'open' } + ).perform + + expect(conversation.status).to eq('open') + end + + it 'sets status to resolved when explicitly provided' do + conversation = described_class.new( + contact_inbox: contact_api_inbox, + params: { status: 'resolved' } + ).perform + + expect(conversation.status).to eq('resolved') + end + end + + context 'when no status is provided and bot is active' do + it 'sets status to pending for bot conversations' do + # Create a bot for the inbox + create(:agent_bot, account: account) + create(:agent_bot_inbox, inbox: api_inbox, account: account) + + conversation = described_class.new( + contact_inbox: contact_api_inbox, + params: {} + ).perform + + expect(conversation.status).to eq('pending') + end + end + + context 'when no status is provided and no bot is active' do + it 'uses default status from model' do + conversation = described_class.new( + contact_inbox: contact_api_inbox, + params: {} + ).perform + + expect(conversation.status).to eq('open') + end + end end end