Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Separate schedule status from notification status on reminders

Kabiru Mwenjafdf09b9aa820
diff --git a/app/models/reminder.rb b/app/models/reminder.rb
index b2fca7cc9ae..bc4d9802b5a 100644
--- a/app/models/reminder.rb
+++ b/app/models/reminder.rb
@@ -32,7 +32,6 @@ class Reminder < ApplicationRecord
has_many :reminder_notifications, dependent: :destroy
- def scheduled?
- job_id.present?
- end
+ def notified? = notified_at.present?
+ def scheduled? = job_id.present?
end
diff --git a/app/workers/reminders/schedule_reminder_job.rb b/app/workers/reminders/schedule_reminder_job.rb
index e87d47ffb9b..260940b51a8 100644
--- a/app/workers/reminders/schedule_reminder_job.rb
+++ b/app/workers/reminders/schedule_reminder_job.rb
@@ -35,10 +35,20 @@ module Reminders
end
def perform(reminder)
- return if reminder.scheduled?
+ return if reminder.notified?
- create_notification_from_reminder(reminder)
- .on_success { |service_result| ReminderNotification.create!(reminder:, notification: service_result.result) }
+ create_notification_service = create_notification_from_reminder(reminder)
+
+ create_notification_service.on_success do |service_result|
+ ReminderNotification.create!(reminder:, notification: service_result.result)
+ reminder.update_column(:notified_at, Time.current)
+ end
+
+ create_notification_service.on_failure do |service_result|
+ Rails.logger.error do
+ "Failed to create notification for reminder #{reminder.id}: #{service_result.message}"
+ end
+ end
end
private
diff --git a/db/migrate/20241119131205_create_reminders.rb b/db/migrate/20241119131205_create_reminders.rb
index 454e830fefd..bcc84c73ae9 100644
--- a/db/migrate/20241119131205_create_reminders.rb
+++ b/db/migrate/20241119131205_create_reminders.rb
@@ -4,6 +4,7 @@ class CreateReminders < ActiveRecord::Migration[7.1]
t.references :remindable, polymorphic: true, null: false
t.references :creator, null: false, foreign_key: { to_table: :users }
t.datetime :remind_at, null: false
+ t.datetime :notified_at
t.string :job_id
t.text :note
diff --git a/spec/factories/reminders_factory.rb b/spec/factories/reminders_factory.rb
index 3903d27be4e..e6335941afa 100644
--- a/spec/factories/reminders_factory.rb
+++ b/spec/factories/reminders_factory.rb
@@ -36,5 +36,9 @@ FactoryBot.define do
trait :scheduled do
job_id { SecureRandom.uuid }
end
+
+ trait :notified do
+ notified_at { remind_at + 1.second }
+ end
end
end
diff --git a/spec/models/reminder_spec.rb b/spec/models/reminder_spec.rb
index bb1398e1125..f8a6bbb6cb7 100644
--- a/spec/models/reminder_spec.rb
+++ b/spec/models/reminder_spec.rb
@@ -35,6 +35,20 @@ RSpec.describe Reminder do
it { is_expected.to have_many(:reminder_notifications).dependent(:destroy) }
end
+ describe "#notified?" do
+ context "when notified_at is present" do
+ subject { build(:reminder, :notified) }
+
+ it { is_expected.to be_notified }
+ end
+
+ context "when notified_at is not present" do
+ subject { build(:reminder) }
+
+ it { is_expected.not_to be_notified }
+ end
+ end
+
describe "#scheduled?" do
context "when job_id is present" do
subject { build(:reminder, :scheduled) }
diff --git a/spec/services/reminders/create_service_spec.rb b/spec/services/reminders/create_service_spec.rb
index 288b0450d23..78ae257ed71 100644
--- a/spec/services/reminders/create_service_spec.rb
+++ b/spec/services/reminders/create_service_spec.rb
@@ -37,15 +37,15 @@ RSpec.describe Reminders::CreateService, type: :model do
before do
allow(model_instance).to receive(:update_column).and_return(true)
- end
-
- it "schedules a reminder job" do
allow(Reminders::ScheduleReminderJob).to receive(:schedule)
.with(model_instance)
.and_return(instance_double(Reminders::ScheduleReminderJob, job_id: 1))
+ end
+ it "schedules a reminder job" do
subject
+ expect(Reminders::ScheduleReminderJob).to have_received(:schedule).with(model_instance)
expect(model_instance).to have_received(:update_column).with(:job_id, 1)
end
end
diff --git a/spec/workers/reminders/schedule_reminder_job_spec.rb b/spec/workers/reminders/schedule_reminder_job_spec.rb
index a24ced8c510..4466834a88e 100644
--- a/spec/workers/reminders/schedule_reminder_job_spec.rb
+++ b/spec/workers/reminders/schedule_reminder_job_spec.rb
@@ -59,11 +59,15 @@ RSpec.describe Reminders::ScheduleReminderJob do
expect(notification.resource).to eq(reminder.remindable)
expect(notification.reason).to eq("reminder")
end
+
+ aggregate_failures "marks the reminder as notified" do
+ expect(reminder.reload).to be_notified
+ end
end
- context "when the reminder is already scheduled" do
+ context "when the reminder is already notified" do
before do
- reminder.update_column(:job_id, SecureRandom.uuid)
+ reminder.update_column(:notified_at, Time.current)
end
it "does not create a notification from the reminder" do