From 03bc4bf224ac309a45b6c12df74ed9d7bed85286 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Mon, 1 Mar 2021 10:58:49 +0530 Subject: [PATCH] fix: Greeting messages not sent when pre-chat form is enabled (#1842) --- .../hook_execution_service.rb | 7 +-- .../hook_execution_service_spec.rb | 50 ++++++++++++------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/app/services/message_templates/hook_execution_service.rb b/app/services/message_templates/hook_execution_service.rb index 05429127e..6d4c9ddd7 100644 --- a/app/services/message_templates/hook_execution_service.rb +++ b/app/services/message_templates/hook_execution_service.rb @@ -4,9 +4,9 @@ class MessageTemplates::HookExecutionService def perform return if inbox.agent_bot_inbox&.active? - ::MessageTemplates::Template::OutOfOffice.new(conversation: conversation).perform if should_send_out_of_office_message? # TODO: let's see whether this is needed and remove this and related logic if not - #::MessageTemplates::Template::Greeting.new(conversation: conversation).perform if should_send_greeting? + # ::MessageTemplates::Template::OutOfOffice.new(conversation: conversation).perform if should_send_out_of_office_message? + ::MessageTemplates::Template::Greeting.new(conversation: conversation).perform if should_send_greeting? ::MessageTemplates::Template::EmailCollect.new(conversation: conversation).perform if should_send_email_collect? end @@ -31,8 +31,9 @@ class MessageTemplates::HookExecutionService conversation.messages.where(content_type: 'input_email').present? end + # TODO: we should be able to reduce this logic once we have a toggle for email collect messages def should_send_email_collect? - !contact_has_email? && inbox.web_widget? && !email_collect_was_sent? + !contact_has_email? && inbox.web_widget? && !inbox.channel.pre_chat_form_enabled? && !email_collect_was_sent? end def contact_has_email? diff --git a/spec/services/message_templates/hook_execution_service_spec.rb b/spec/services/message_templates/hook_execution_service_spec.rb index 0abc15790..a35ccd25d 100644 --- a/spec/services/message_templates/hook_execution_service_spec.rb +++ b/spec/services/message_templates/hook_execution_service_spec.rb @@ -19,13 +19,26 @@ describe ::MessageTemplates::HookExecutionService do # described class gets called in message after commit message = create(:message, conversation: conversation) - # TODO: remove this if this hook is removed - # expect(::MessageTemplates::Template::Greeting).to have_received(:new).with(conversation: message.conversation) - # expect(greeting_service).to have_received(:perform) + expect(::MessageTemplates::Template::Greeting).to have_received(:new).with(conversation: message.conversation) + expect(greeting_service).to have_received(:perform) expect(::MessageTemplates::Template::EmailCollect).to have_received(:new).with(conversation: message.conversation) expect(email_collect_service).to have_received(:perform) end + it 'doesnot calls ::MessageTemplates::Template::EmailCollect when prechat form is enabled' do + contact = create(:contact, email: nil) + conversation = create(:conversation, contact: contact) + + # ensure prechat form is enabled + conversation.inbox.channel.update(pre_chat_form_enabled: true) + allow(::MessageTemplates::Template::EmailCollect).to receive(:new).and_return(true) + + # described class gets called in message after commit + message = create(:message, conversation: conversation) + + expect(::MessageTemplates::Template::EmailCollect).not_to have_received(:new).with(conversation: message.conversation) + end + it 'doesnot calls ::MessageTemplates::Template::Greeting if greeting_message is empty' do contact = create(:contact, email: nil) conversation = create(:conversation, contact: contact) @@ -47,24 +60,25 @@ describe ::MessageTemplates::HookExecutionService do end end - context 'when it is after working hours' do - it 'calls ::MessageTemplates::Template::OutOfOffice' do - contact = create :contact - conversation = create :conversation, contact: contact + # TODO: remove this if this hook is removed + # context 'when it is after working hours' do + # it 'calls ::MessageTemplates::Template::OutOfOffice' do + # contact = create :contact + # conversation = create :conversation, contact: contact - conversation.inbox.update(working_hours_enabled: true, out_of_office_message: 'We are out of office') - conversation.inbox.working_hours.today.update!(closed_all_day: true) + # conversation.inbox.update(working_hours_enabled: true, out_of_office_message: 'We are out of office') + # conversation.inbox.working_hours.today.update!(closed_all_day: true) - out_of_office_service = double + # out_of_office_service = double - allow(::MessageTemplates::Template::OutOfOffice).to receive(:new).and_return(out_of_office_service) - allow(out_of_office_service).to receive(:perform).and_return(true) + # allow(::MessageTemplates::Template::OutOfOffice).to receive(:new).and_return(out_of_office_service) + # allow(out_of_office_service).to receive(:perform).and_return(true) - # described class gets called in message after commit - message = create(:message, conversation: conversation) + # # described class gets called in message after commit + # message = create(:message, conversation: conversation) - expect(::MessageTemplates::Template::OutOfOffice).to have_received(:new).with(conversation: message.conversation) - expect(out_of_office_service).to have_received(:perform) - end - end + # expect(::MessageTemplates::Template::OutOfOffice).to have_received(:new).with(conversation: message.conversation) + # expect(out_of_office_service).to have_received(:perform) + # end + # end end