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)