chore: Disable message hooks for conversations without incoming message (#7620)

When using client APIs to create conversations and auto-assignment is turned on, welcome messages were getting triggered. This PR disable the behaviour and ensure template hooks are triggered only if there are incoming messages present. 

Fixes: https://linear.app/chatwoot/issue/CW-2187
This commit is contained in:
Sojan Jose
2023-07-26 20:38:49 +03:00
committed by GitHub
parent 6c1ee4d965
commit 6cbe1ed911
4 changed files with 26 additions and 11 deletions

View File

@@ -60,11 +60,7 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
def sent_first_outgoing_message_after_24_hours?
# we can send max 1 message after 24 hour window
conversation.messages.outgoing.where('id > ?', last_incoming_message.id).count == 1
end
def last_incoming_message
conversation.messages.incoming.last
conversation.messages.outgoing.where('id > ?', conversation.last_incoming_message.id).count == 1
end
def handle_facebook_error(exception)

View File

@@ -90,11 +90,7 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService
def sent_first_outgoing_message_after_24_hours?
# we can send max 1 message after 24 hour window
conversation.messages.outgoing.where('id > ?', last_incoming_message.id).count == 1
end
def last_incoming_message
conversation.messages.incoming.last
conversation.messages.outgoing.where('id > ?', conversation.last_incoming_message.id).count == 1
end
def config

View File

@@ -3,6 +3,7 @@ class MessageTemplates::HookExecutionService
def perform
return if conversation.campaign.present?
return if conversation.last_incoming_message.blank?
trigger_templates
end

View File

@@ -1,6 +1,27 @@
require 'rails_helper'
describe MessageTemplates::HookExecutionService do
context 'when there is no incoming message in conversation' do
it 'will not call any hooks' do
contact = create(:contact, email: nil)
conversation = create(:conversation, contact: contact)
# ensure greeting hook is enabled
conversation.inbox.update(greeting_enabled: true, enable_email_collect: true)
email_collect_service = double
allow(MessageTemplates::Template::EmailCollect).to receive(:new).and_return(email_collect_service)
allow(email_collect_service).to receive(:perform).and_return(true)
allow(MessageTemplates::Template::Greeting).to receive(:new)
# described class gets called in message after commit
create(:message, conversation: conversation, message_type: 'activity', content: 'Conversation marked resolved!!')
expect(MessageTemplates::Template::Greeting).not_to have_received(:new)
expect(MessageTemplates::Template::EmailCollect).not_to have_received(:new)
end
end
context 'when Greeting Message' do
it 'doesnot calls ::MessageTemplates::Template::Greeting if greeting_message is empty' do
contact = create(:contact, email: nil)
@@ -22,7 +43,7 @@ describe MessageTemplates::HookExecutionService do
expect(email_collect_service).to have_received(:perform)
end
it 'will not call ::MessageTemplates::Template::CsatSurvey if its a tweet conversation' do
it 'will not call ::MessageTemplates::Template::Greeting if its a tweet conversation' do
twitter_channel = create(:channel_twitter_profile)
twitter_inbox = create(:inbox, channel: twitter_channel)
# ensure greeting hook is enabled and greeting_message is present
@@ -97,6 +118,7 @@ describe MessageTemplates::HookExecutionService do
before do
allow(MessageTemplates::Template::CsatSurvey).to receive(:new).and_return(csat_survey)
allow(csat_survey).to receive(:perform).and_return(true)
create(:message, conversation: conversation, message_type: 'incoming')
end
it 'calls ::MessageTemplates::Template::CsatSurvey when a conversation is resolved in an inbox with survey enabled' do