From ae694da6c11feac4df1602c9c190a307d5f795f3 Mon Sep 17 00:00:00 2001 From: Pranav Date: Wed, 12 Mar 2025 19:22:44 -0700 Subject: [PATCH] chore: Disable sending outgoing messages if the conversation is active (#11073) At 5 PM, when business hours officially end, an automatic out-of-office message is sent to customers. However, this creates a poor experience if an agent is actively chatting with the customer. This update ensures that the out-of-office message is only sent if no agent message has been sent in the last 5 minutes. If the customer reaches out again after 5 minutes, the out-of-office message will be triggered. --- .../hook_execution_service.rb | 3 +++ .../hook_execution_service_spec.rb | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/services/message_templates/hook_execution_service.rb b/app/services/message_templates/hook_execution_service.rb index 401f90b27..bc161d885 100644 --- a/app/services/message_templates/hook_execution_service.rb +++ b/app/services/message_templates/hook_execution_service.rb @@ -25,6 +25,9 @@ class MessageTemplates::HookExecutionService return false if conversation.tweet? # should not send for outbound messages return false unless message.incoming? + # prevents sending out-of-office message if an agent has sent a message in last 5 minutes + # ensures better UX by not interrupting active conversations at the end of business hours + return false if conversation.messages.outgoing.exists?(['created_at > ?', 5.minutes.ago]) 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 949d71ac6..9d9d82922 100644 --- a/spec/services/message_templates/hook_execution_service_spec.rb +++ b/spec/services/message_templates/hook_execution_service_spec.rb @@ -194,6 +194,25 @@ describe MessageTemplates::HookExecutionService do expect(out_of_office_service).to have_received(:perform) end + it 'does not call ::MessageTemplates::Template::OutOfOffice when there are recent outgoing messages' 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) + + create(:message, conversation: conversation, message_type: :outgoing, created_at: 2.minutes.ago) + + 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) + + create(:message, conversation: conversation) + + expect(MessageTemplates::Template::OutOfOffice).not_to have_received(:new) + expect(out_of_office_service).not_to have_received(:perform) + end + it 'will not calls ::MessageTemplates::Template::OutOfOffice when outgoing message' do contact = create(:contact) conversation = create(:conversation, contact: contact)