feat: allow bots to handle campaigns when sender_id is nil (#12805)
This commit is contained in:
@@ -228,14 +228,17 @@ class Conversation < ApplicationRecord
|
|||||||
def determine_conversation_status
|
def determine_conversation_status
|
||||||
self.status = :resolved and return if contact.blocked?
|
self.status = :resolved and return if contact.blocked?
|
||||||
|
|
||||||
# Message template hooks aren't executed for conversations from campaigns
|
return handle_campaign_status if campaign.present?
|
||||||
# So making these conversations open for agent visibility
|
|
||||||
return if campaign.present?
|
|
||||||
|
|
||||||
# TODO: make this an inbox config instead of assuming bot conversations should start as pending
|
# TODO: make this an inbox config instead of assuming bot conversations should start as pending
|
||||||
self.status = :pending if inbox.active_bot?
|
self.status = :pending if inbox.active_bot?
|
||||||
end
|
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?
|
||||||
|
end
|
||||||
|
|
||||||
def notify_conversation_creation
|
def notify_conversation_creation
|
||||||
dispatcher_dispatch(CONVERSATION_CREATED)
|
dispatcher_dispatch(CONVERSATION_CREATED)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -576,9 +576,38 @@ RSpec.describe Conversation do
|
|||||||
expect(conversation.status).to eq('pending')
|
expect(conversation.status).to eq('pending')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns conversation as open if campaign is present' do
|
context 'with campaigns' do
|
||||||
conversation = create(:conversation, inbox: bot_inbox.inbox, campaign: create(:campaign))
|
let(:user) { create(:user, account: bot_inbox.inbox.account) }
|
||||||
expect(conversation.status).to eq('open')
|
|
||||||
|
it 'returns conversation as open if campaign has a sender' 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')
|
||||||
|
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')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with campaigns in inbox without bot' do
|
||||||
|
let(:account) { create(:account) }
|
||||||
|
let(:inbox) { create(:inbox, account: account) }
|
||||||
|
let(:user) { create(:user, account: account) }
|
||||||
|
|
||||||
|
it 'returns conversation as open if campaign has no sender but no bot is active' do
|
||||||
|
campaign = create(:campaign, inbox: inbox, account: account, sender: nil)
|
||||||
|
conversation = create(:conversation, inbox: inbox, campaign: campaign)
|
||||||
|
expect(conversation.status).to eq('open')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns conversation as open if campaign has a sender' do
|
||||||
|
campaign = create(:campaign, inbox: inbox, account: account, sender: user)
|
||||||
|
conversation = create(:conversation, inbox: inbox, campaign: campaign)
|
||||||
|
expect(conversation.status).to eq('open')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user