From aaadd61e09293325a9586a2d8e12daa59e633583 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Tue, 21 Sep 2021 10:16:14 +0530 Subject: [PATCH] fix: Outbound messages triggering out of office (#3051) --- .../hook_execution_service.rb | 2 ++ .../hook_execution_service_spec.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/services/message_templates/hook_execution_service.rb b/app/services/message_templates/hook_execution_service.rb index d306a399d..bed23b2f8 100644 --- a/app/services/message_templates/hook_execution_service.rb +++ b/app/services/message_templates/hook_execution_service.rb @@ -23,6 +23,8 @@ class MessageTemplates::HookExecutionService def should_send_out_of_office_message? # should not send if its a tweet message return false if conversation.tweet? + # should not send for outbound messages + return false unless message.incoming? inbox.out_of_office? && conversation.messages.today.template.empty? && inbox.out_of_office_message.present? end diff --git a/spec/services/message_templates/hook_execution_service_spec.rb b/spec/services/message_templates/hook_execution_service_spec.rb index 929888657..87264a6ae 100644 --- a/spec/services/message_templates/hook_execution_service_spec.rb +++ b/spec/services/message_templates/hook_execution_service_spec.rb @@ -174,6 +174,25 @@ describe ::MessageTemplates::HookExecutionService do expect(out_of_office_service).to have_received(:perform) end + it 'will not calls ::MessageTemplates::Template::OutOfOffice when outgoing message' 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) + + 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) + + # described class gets called in message after commit + message = create(:message, conversation: conversation, message_type: 'outgoing') + + expect(::MessageTemplates::Template::OutOfOffice).not_to have_received(:new).with(conversation: message.conversation) + expect(out_of_office_service).not_to have_received(:perform) + end + it 'will not call ::MessageTemplates::Template::OutOfOffice if its a tweet conversation' do twitter_channel = create(:channel_twitter_profile) twitter_inbox = create(:inbox, channel: twitter_channel)