Compare commits

...
4 changed files with 77 additions and 1 deletions
+16 -1
View File
@@ -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
@@ -136,6 +136,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) {
@@ -287,6 +287,7 @@ describe('composeConversationHelper', () => {
contactId: 2,
message: { content: 'Hello' },
assigneeId: 3,
status: 'open',
});
});
@@ -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