Compare commits

...
Author SHA1 Message Date
Muhsin Keloth d32b75ffd3 feat: add last snoozed at 2024-02-14 15:47:16 +05:30
5 changed files with 25 additions and 5 deletions
@@ -2,9 +2,17 @@ class Notification::ReopenSnoozedNotificationsJob < ApplicationJob
queue_as :low queue_as :low
def perform def perform
# rubocop:disable Rails/SkipsModelValidations Notification.where(snoozed_until: 3.days.ago..Time.current).find_each do |notification|
Notification.where(snoozed_until: 3.days.ago..Time.current) # Ensure the meta field is a hash and merge the last_snoozed_at key
.update_all(snoozed_until: nil, updated_at: Time.current, last_activity_at: Time.current) updated_meta = (notification.meta || {}).merge('last_snoozed_at' => notification.snoozed_until)
# rubocop:enable Rails/SkipsModelValidations
# Update the notification with the new values
notification.update(
snoozed_until: nil,
updated_at: Time.current,
last_activity_at: Time.current,
meta: updated_meta
)
end
end end
end end
+1
View File
@@ -4,6 +4,7 @@
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# last_activity_at :datetime # last_activity_at :datetime
# meta :jsonb
# notification_type :integer not null # notification_type :integer not null
# primary_actor_type :string not null # primary_actor_type :string not null
# read_at :datetime # read_at :datetime
@@ -0,0 +1,5 @@
class AddMetaToNotifications < ActiveRecord::Migration[7.0]
def change
add_column :notifications, :meta, :jsonb, default: {}
end
end
+2 -1
View File
@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2024_02_07_103014) do ActiveRecord::Schema[7.0].define(version: 2024_02_14_092129) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements" enable_extension "pg_stat_statements"
enable_extension "pg_trgm" enable_extension "pg_trgm"
@@ -750,6 +750,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_02_07_103014) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.datetime "snoozed_until" t.datetime "snoozed_until"
t.datetime "last_activity_at", default: -> { "CURRENT_TIMESTAMP" } t.datetime "last_activity_at", default: -> { "CURRENT_TIMESTAMP" }
t.jsonb "meta", default: {}
t.index ["account_id"], name: "index_notifications_on_account_id" t.index ["account_id"], name: "index_notifications_on_account_id"
t.index ["last_activity_at"], name: "index_notifications_on_last_activity_at" t.index ["last_activity_at"], name: "index_notifications_on_last_activity_at"
t.index ["primary_actor_type", "primary_actor_id"], name: "uniq_primary_actor_per_account_notifications" t.index ["primary_actor_type", "primary_actor_id"], name: "uniq_primary_actor_per_account_notifications"
@@ -13,10 +13,15 @@ RSpec.describe Notification::ReopenSnoozedNotificationsJob do
context 'when called' do context 'when called' do
it 'reopens snoozed notifications whose snooze until has passed' do it 'reopens snoozed notifications whose snooze until has passed' do
described_class.perform_now described_class.perform_now
time_after_job = Time.current
expect(snoozed_till_5_minutes_ago.reload.snoozed_until).to be_nil 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_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.snoozed_until).to be_nil
expect(snoozed_till_5_minutes_ago.reload.last_activity_at).to be_within(1.second).of(time_after_job)
last_snoozed_at = Time.zone.parse(snoozed_till_5_minutes_ago.reload.meta['last_snoozed_at'])
expect(last_snoozed_at).to be <= time_after_job
end end
end end
end end