From e6cf8c39b7a3d4482c2a96dcc51995e910800255 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Tue, 20 Feb 2024 18:55:39 +0530 Subject: [PATCH] feat: Update `last_snoozed_at` after the un-snooze notification (#8943) --- .../v1/accounts/notifications_controller.rb | 3 ++- .../reopen_snoozed_notifications_job.rb | 23 +++++++++++++++---- app/models/notification.rb | 1 + .../notifications/index.json.jbuilder | 1 + .../accounts/notifications_controller_spec.rb | 1 + .../reopen_snoozed_notifications_job_spec.rb | 3 +++ 6 files changed, 27 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/accounts/notifications_controller.rb b/app/controllers/api/v1/accounts/notifications_controller.rb index 3ac0568e3..52035ce64 100644 --- a/app/controllers/api/v1/accounts/notifications_controller.rb +++ b/app/controllers/api/v1/accounts/notifications_controller.rb @@ -54,7 +54,8 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro end def snooze - @notification.update(snoozed_until: parse_date_time(params[:snoozed_until].to_s)) if params[:snoozed_until] + updated_meta = (@notification.meta || {}).merge('last_snoozed_at' => nil) + @notification.update(snoozed_until: parse_date_time(params[:snoozed_until].to_s), meta: updated_meta) if params[:snoozed_until] render json: @notification end diff --git a/app/jobs/notification/reopen_snoozed_notifications_job.rb b/app/jobs/notification/reopen_snoozed_notifications_job.rb index 05235b62b..91ed5ee61 100644 --- a/app/jobs/notification/reopen_snoozed_notifications_job.rb +++ b/app/jobs/notification/reopen_snoozed_notifications_job.rb @@ -2,9 +2,24 @@ 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, read_at: nil) - # rubocop:enable Rails/SkipsModelValidations + Notification.where(snoozed_until: 3.days.ago..Time.current).find_in_batches(batch_size: 100) do |notifications_batch| + notifications_batch.each do |notification| + update_notification(notification) + end + end + end + + private + + def update_notification(notification) + updated_meta = (notification.meta || {}).merge('last_snoozed_at' => notification.snoozed_until) + + notification.update!( + snoozed_until: nil, + updated_at: Time.current, + last_activity_at: Time.current, + meta: updated_meta, + read_at: nil + ) end end diff --git a/app/models/notification.rb b/app/models/notification.rb index cd3d57627..113c6cf3e 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -64,6 +64,7 @@ class Notification < ApplicationRecord created_at: created_at.to_i, last_activity_at: last_activity_at.to_i, snoozed_until: snoozed_until, + meta: meta, account_id: account_id } diff --git a/app/views/api/v1/accounts/notifications/index.json.jbuilder b/app/views/api/v1/accounts/notifications/index.json.jbuilder index ae86372c9..4a408dfca 100644 --- a/app/views/api/v1/accounts/notifications/index.json.jbuilder +++ b/app/views/api/v1/accounts/notifications/index.json.jbuilder @@ -21,6 +21,7 @@ json.data do json.created_at notification.created_at.to_i json.last_activity_at notification.last_activity_at.to_i json.snoozed_until notification.snoozed_until + json.meta notification.meta end end end diff --git a/spec/controllers/api/v1/accounts/notifications_controller_spec.rb b/spec/controllers/api/v1/accounts/notifications_controller_spec.rb index 4605e0fda..9b16e48fb 100644 --- a/spec/controllers/api/v1/accounts/notifications_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/notifications_controller_spec.rb @@ -179,6 +179,7 @@ RSpec.describe 'Notifications API', type: :request do expect(response).to have_http_status(:success) expect(notification.reload.snoozed_until).not_to eq('') + expect(notification.reload.meta['last_snoozed_at']).to be_nil end end end diff --git a/spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb b/spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb index 024c3d0d6..94697dcd6 100644 --- a/spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb +++ b/spec/jobs/notification/reopen_snoozed_notifications_job_spec.rb @@ -14,10 +14,13 @@ RSpec.describe Notification::ReopenSnoozedNotificationsJob do it 'reopens snoozed notifications whose snooze until has passed' do described_class.perform_now + snoozed_until = snoozed_till_5_minutes_ago.reload.snoozed_until + 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 expect(snoozed_indefinitely.reload.read_at).to be_nil + expect(snoozed_until).to eq(snoozed_till_5_minutes_ago.reload.meta['snoozed_until']) end end end