feat: add last snoozed at
This commit is contained in:
@@ -2,9 +2,17 @@ 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
|
||||
Notification.where(snoozed_until: 3.days.ago..Time.current).find_each do |notification|
|
||||
# Ensure the meta field is a hash and merge the last_snoozed_at key
|
||||
updated_meta = (notification.meta || {}).merge('last_snoozed_at' => notification.snoozed_until)
|
||||
|
||||
# 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
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# last_activity_at :datetime
|
||||
# meta :jsonb
|
||||
# notification_type :integer not null
|
||||
# primary_actor_type :string not null
|
||||
# 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
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# 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
|
||||
enable_extension "pg_stat_statements"
|
||||
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 "snoozed_until"
|
||||
t.datetime "last_activity_at", default: -> { "CURRENT_TIMESTAMP" }
|
||||
t.jsonb "meta", default: {}
|
||||
t.index ["account_id"], name: "index_notifications_on_account_id"
|
||||
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"
|
||||
|
||||
@@ -13,10 +13,15 @@ RSpec.describe Notification::ReopenSnoozedNotificationsJob do
|
||||
context 'when called' do
|
||||
it 'reopens snoozed notifications whose snooze until has passed' do
|
||||
described_class.perform_now
|
||||
time_after_job = Time.current
|
||||
|
||||
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_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
|
||||
|
||||
Reference in New Issue
Block a user