From 68062216e484751ae004e2616c179afacecfd41f Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Fri, 5 Jan 2024 15:13:22 +0530 Subject: [PATCH] feat: Add a job to reopen snoozed notifications (#8545) --- .../reopen_snoozed_notifications_job.rb | 10 +++++++++ app/jobs/trigger_scheduled_items_job.rb | 3 +++ .../reopen_snoozed_notifications_job_spec.rb | 22 +++++++++++++++++++ spec/jobs/trigger_scheduled_items_job_spec.rb | 5 +++++ 4 files changed, 40 insertions(+) create mode 100644 app/jobs/notification/reopen_snoozed_notifications_job.rb create mode 100644 spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb diff --git a/app/jobs/notification/reopen_snoozed_notifications_job.rb b/app/jobs/notification/reopen_snoozed_notifications_job.rb new file mode 100644 index 000000000..89b245187 --- /dev/null +++ b/app/jobs/notification/reopen_snoozed_notifications_job.rb @@ -0,0 +1,10 @@ +class Notification::ReopenSnoozedNotificationsJob < ApplicationJob + queue_as :low + + def perform + # rubocop:disable Rails/SkipsModelValidations + Notification.where(snoozed_until: 3.days.ago..Time.current) + .update_all(snoozed_until: nil, updated_at: Time.current, last_activity_at: Time.current) + # rubocop:enable Rails/SkipsModelValidations + end +end diff --git a/app/jobs/trigger_scheduled_items_job.rb b/app/jobs/trigger_scheduled_items_job.rb index f51d9df9a..9fc7a6491 100644 --- a/app/jobs/trigger_scheduled_items_job.rb +++ b/app/jobs/trigger_scheduled_items_job.rb @@ -11,6 +11,9 @@ class TriggerScheduledItemsJob < ApplicationJob # Job to reopen snoozed conversations Conversations::ReopenSnoozedConversationsJob.perform_later + # Job to reopen snoozed notifications + Notification::ReopenSnoozedNotificationsJob.perform_later + # Job to auto-resolve conversations Account::ConversationsResolutionSchedulerJob.perform_later diff --git a/spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb b/spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb new file mode 100644 index 000000000..2b210ef2b --- /dev/null +++ b/spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe Notification::ReopenSnoozedNotificationsJob do + let!(:snoozed_till_5_minutes_ago) { create(:notification, snoozed_until: 5.minutes.ago) } + let!(:snoozed_till_tomorrow) { create(:notification, snoozed_until: 1.day.from_now) } + let!(:snoozed_indefinitely) { create(:notification) } + + it 'enqueues the job' do + expect { described_class.perform_later }.to have_enqueued_job(described_class) + .on_queue('low') + end + + context 'when called' do + it 'reopens snoozed notifications whose snooze until has passed' do + described_class.perform_now + + expect(snoozed_till_5_minutes_ago.reload.snoozed_until).to be_nil + expect(snoozed_till_tomorrow.reload.snoozed_until.to_date).to eq 1.day.from_now.to_date + expect(snoozed_indefinitely.reload.snoozed_until).to be_nil + end + end +end diff --git a/spec/jobs/trigger_scheduled_items_job_spec.rb b/spec/jobs/trigger_scheduled_items_job_spec.rb index 270bd9076..9fd846c2d 100644 --- a/spec/jobs/trigger_scheduled_items_job_spec.rb +++ b/spec/jobs/trigger_scheduled_items_job_spec.rb @@ -26,6 +26,11 @@ RSpec.describe TriggerScheduledItemsJob do described_class.perform_now end + it 'triggers Notification::ReopenSnoozedNotificationsJob' do + expect(Notification::ReopenSnoozedNotificationsJob).to receive(:perform_later).once + described_class.perform_now + end + it 'triggers Account::ConversationsResolutionSchedulerJob' do expect(Account::ConversationsResolutionSchedulerJob).to receive(:perform_later).once described_class.perform_now