From 794a56d4cc149744687927cd46f5b17a40299f28 Mon Sep 17 00:00:00 2001 From: "Aswin Dev P.S" Date: Fri, 17 Sep 2021 22:17:11 +0530 Subject: [PATCH] Feat: Out of office autoresponder (#2992) This change allows the user to enable autoresponder during the out-of-office time. Fixes: #2035 --- .../dashboard/settings/inbox/Settings.vue | 8 ++-- app/models/working_hour.rb | 8 ++-- .../hook_execution_service.rb | 6 ++- spec/models/working_hour_spec.rb | 24 ++++++++++ .../hook_execution_service_spec.rb | 48 ++++++++++++------- 5 files changed, 68 insertions(+), 26 deletions(-) diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue index 4a241102f7cf4..cf8ddd2fd6cbb 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue @@ -392,6 +392,10 @@ export default { key: 'collaborators', name: this.$t('INBOX_MGMT.TABS.COLLABORATORS'), }, + { + key: 'businesshours', + name: this.$t('INBOX_MGMT.TABS.BUSINESS_HOURS'), + }, ]; if (this.isAWebWidgetInbox) { @@ -401,10 +405,6 @@ export default { key: 'preChatForm', name: this.$t('INBOX_MGMT.TABS.PRE_CHAT_FORM'), }, - { - key: 'businesshours', - name: this.$t('INBOX_MGMT.TABS.BUSINESS_HOURS'), - }, { key: 'configuration', name: this.$t('INBOX_MGMT.TABS.CONFIGURATION'), diff --git a/app/models/working_hour.rb b/app/models/working_hour.rb index b37be4eb1fb5f..890a452ba80ae 100644 --- a/app/models/working_hour.rb +++ b/app/models/working_hour.rb @@ -43,10 +43,10 @@ def self.today def open_at?(time) return false if closed_all_day? - time.hour >= open_hour && - time.min >= open_minutes && - time.hour <= close_hour && - time.min <= close_minutes + open_time = Time.zone.now.in_time_zone(inbox.timezone).change({ hour: open_hour, min: open_minutes }) + close_time = Time.zone.now.in_time_zone(inbox.timezone).change({ hour: close_hour, min: close_minutes }) + + time.between?(open_time, close_time) end def open_now? diff --git a/app/services/message_templates/hook_execution_service.rb b/app/services/message_templates/hook_execution_service.rb index 4e63808bd9ff6..d306a399d648b 100644 --- a/app/services/message_templates/hook_execution_service.rb +++ b/app/services/message_templates/hook_execution_service.rb @@ -14,14 +14,16 @@ def perform delegate :contact, to: :conversation def trigger_templates - # TODO: let's see whether this is needed and remove this and related logic if not - # ::MessageTemplates::Template::OutOfOffice.new(conversation: conversation).perform if should_send_out_of_office_message? + ::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 inbox.enable_email_collect && should_send_email_collect? ::MessageTemplates::Template::CsatSurvey.new(conversation: conversation).perform if should_send_csat_survey? end def should_send_out_of_office_message? + # should not send if its a tweet message + return false if conversation.tweet? + inbox.out_of_office? && conversation.messages.today.template.empty? && inbox.out_of_office_message.present? end diff --git a/spec/models/working_hour_spec.rb b/spec/models/working_hour_spec.rb index d7c49b0bf1b93..73cb4efdfa421 100644 --- a/spec/models/working_hour_spec.rb +++ b/spec/models/working_hour_spec.rb @@ -26,4 +26,28 @@ expect(described_class.today.closed_now?).to be true end end + + context 'when on friday 12:30pm' do + before do + Time.zone = 'UTC' + create(:working_hour) + travel_to '10.09.2021 12:30'.to_datetime + end + + it 'is considered to be in business hours' do + expect(described_class.today.open_now?).to be true + end + end + + context 'when on friday 17:30pm' do + before do + Time.zone = 'UTC' + create(:working_hour) + travel_to '10.09.2021 17:30'.to_datetime + end + + it 'is considered out of office' do + expect(described_class.today.closed_now?).to be true + end + end end diff --git a/spec/services/message_templates/hook_execution_service_spec.rb b/spec/services/message_templates/hook_execution_service_spec.rb index 526d1888d66d9..929888657a571 100644 --- a/spec/services/message_templates/hook_execution_service_spec.rb +++ b/spec/services/message_templates/hook_execution_service_spec.rb @@ -154,25 +154,41 @@ end end - # 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 + 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 + + 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) + twitter_inbox.update(working_hours_enabled: true, out_of_office_message: 'We are out of office') - # expect(::MessageTemplates::Template::OutOfOffice).to have_received(:new).with(conversation: message.conversation) - # expect(out_of_office_service).to have_received(:perform) - # end - # end + conversation = create(:conversation, inbox: twitter_inbox, additional_attributes: { type: 'tweet' }) + + 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(false) + + message = create(:message, conversation: conversation) + expect(::MessageTemplates::Template::OutOfOffice).not_to have_received(:new).with(conversation: message.conversation) + expect(out_of_office_service).not_to receive(:perform) + end + end end