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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Revert "fix[Op#59433]: simplify reminder model to directly linked to notification"
Kabiru Mwenjab6367992bc9e
diff --git a/app/models/notification.rb b/app/models/notification.rbindex 99407a2e29d..e21493cc10d 100644--- a/app/models/notification.rb+++ b/app/models/notification.rb@@ -51,7 +51,8 @@ class Notification < ApplicationRecordbelongs_to :journalbelongs_to :resource, polymorphic: true- has_one :reminder, dependent: :nullify+ has_one :reminder_notification, dependent: :destroy+ has_one :reminder, through: :reminder_notificationinclude Scopes::Scopedscopes :unsent_reminders_before,diff --git a/app/models/reminder.rb b/app/models/reminder.rbindex a2271dfa5c4..d960aa724cc 100644--- a/app/models/reminder.rb+++ b/app/models/reminder.rb@@ -29,8 +29,15 @@class Reminder < ApplicationRecordbelongs_to :remindable, polymorphic: truebelongs_to :creator, class_name: "User"- belongs_to :notification, optional: true- def notified? = notification_id.present?- def scheduled? = job_id.present?+ has_many :reminder_notifications, dependent: :destroy+ has_many :notifications, through: :reminder_notifications++ def unread_notifications?+ notifications.exists?(read_ian: [false, nil])+ end++ def scheduled?+ job_id.present?+ endenddiff --git a/app/models/reminder_notification.rb b/app/models/reminder_notification.rbnew file mode 100644index 00000000000..cbdd70905a9--- /dev/null+++ b/app/models/reminder_notification.rb@@ -0,0 +1,4 @@+class ReminderNotification < ApplicationRecord+ belongs_to :reminder+ belongs_to :notification+enddiff --git a/app/workers/reminders/schedule_reminder_job.rb b/app/workers/reminders/schedule_reminder_job.rbindex 51ec59593cd..522dad565bc 100644--- a/app/workers/reminders/schedule_reminder_job.rb+++ b/app/workers/reminders/schedule_reminder_job.rb@@ -35,12 +35,12 @@ module Remindersenddef perform(reminder)- return if reminder.notified?+ return if reminder.unread_notifications?create_notification_service = create_notification_from_reminder(reminder)create_notification_service.on_success do |service_result|- reminder.update_columns(notification_id: service_result.result.id)+ ReminderNotification.create!(reminder:, notification: service_result.result)endcreate_notification_service.on_failure do |service_result|diff --git a/db/migrate/20241119131205_create_reminders.rb b/db/migrate/20241119131205_create_reminders.rbindex 47d7677799e..454e830fefd 100644--- a/db/migrate/20241119131205_create_reminders.rb+++ b/db/migrate/20241119131205_create_reminders.rb@@ -3,17 +3,11 @@ class CreateReminders < ActiveRecord::Migration[7.1]create_table :reminders do |t|t.references :remindable, polymorphic: true, null: falset.references :creator, null: false, foreign_key: { to_table: :users }- t.references :notification, foreign_key: truet.datetime :remind_at, null: falset.string :job_id- t.string :note+ t.text :notet.timestampsend-- add_index :reminders, :notification_id,- unique: true,- where: "notification_id IS NOT NULL",- name: "index_reminders_on_notification_id_unique"endenddiff --git a/db/migrate/20241121113638_create_reminder_notifications.rb b/db/migrate/20241121113638_create_reminder_notifications.rbnew file mode 100644index 00000000000..eaa711b431e--- /dev/null+++ b/db/migrate/20241121113638_create_reminder_notifications.rb@@ -0,0 +1,14 @@+class CreateReminderNotifications < ActiveRecord::Migration[7.1]+ def change+ create_table :reminder_notifications do |t|+ t.references :reminder, foreign_key: true+ t.references :notification, foreign_key: true++ t.timestamps+ end++ add_index :reminder_notifications, :notification_id,+ unique: true,+ name: "index_reminder_notifications_unique"+ end+enddiff --git a/spec/factories/reminders_factory.rb b/spec/factories/reminders_factory.rbindex 29a45bfee8f..982038d75e7 100644--- a/spec/factories/reminders_factory.rb+++ b/spec/factories/reminders_factory.rb@@ -37,9 +37,21 @@ FactoryBot.define dojob_id { SecureRandom.uuid }end- trait :notified do- job_id { SecureRandom.uuid }- notification factory: :notification+ trait :with_unread_notifications do+ after(:create) do |reminder|+ create(:reminder_notification, reminder: reminder, notification: create(:notification, read_ian: false))+ end+ end++ trait :with_read_notifications do+ after(:create) do |reminder|+ create(:reminder_notification, reminder: reminder, notification: create(:notification, read_ian: true))+ endendend++ factory :reminder_notification do+ reminder+ notification+ endenddiff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rbindex b850207b6e3..c21c920e261 100644--- a/spec/models/notification_spec.rb+++ b/spec/models/notification_spec.rb@@ -34,7 +34,8 @@ RSpec.describe Notification doit { is_expected.to belong_to(:actor).class_name("User") }it { is_expected.to belong_to(:recipient).class_name("User") }- it { is_expected.to have_one(:reminder).dependent(:nullify) }+ it { is_expected.to have_one(:reminder_notification).dependent(:destroy) }+ it { is_expected.to have_one(:reminder).through(:reminder_notification) }enddescribe "Enums" dodiff --git a/spec/models/reminder_notification_spec.rb b/spec/models/reminder_notification_spec.rbnew file mode 100644index 00000000000..95c8193813c--- /dev/null+++ b/spec/models/reminder_notification_spec.rb@@ -0,0 +1,36 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require "spec_helper"++RSpec.describe ReminderNotification do+ describe "Associations" do+ it { is_expected.to belong_to(:reminder) }+ it { is_expected.to belong_to(:notification) }+ end+enddiff --git a/spec/models/reminder_spec.rb b/spec/models/reminder_spec.rbindex fdb8a5f85f2..8b730464012 100644--- a/spec/models/reminder_spec.rb+++ b/spec/models/reminder_spec.rb@@ -32,24 +32,27 @@ RSpec.describe Reminder dodescribe "Associations" doit { is_expected.to belong_to(:remindable) }it { is_expected.to belong_to(:creator).class_name("User") }- it { is_expected.to belong_to(:notification).optional }+ it { is_expected.to have_many(:reminder_notifications).dependent(:destroy) }+ it { is_expected.to have_many(:notifications).through(:reminder_notifications) }end- describe "DB Indexes" do- it { is_expected.to have_db_index(:notification_id).unique(true) }- end+ describe "#unread_notifications?" do+ context "with an unread notification" do+ subject { create(:reminder, :with_unread_notifications) }++ it { is_expected.to be_an_unread_notification }+ end- describe "#notified?" do- it "returns true if notification_id is present" do- reminder = build_stubbed(:reminder, :notified)+ context "with no unread notifications" do+ subject { create(:reminder, :with_read_notifications) }- expect(reminder).to be_notified+ it { is_expected.not_to be_an_unread_notification }end- it "returns false if notification_id is not present" do- reminder = build(:reminder, notification_id: nil)+ context "with no notifications" do+ subject { build(:reminder, notifications: []) }- expect(reminder).not_to be_notified+ it { is_expected.not_to be_an_unread_notification }endenddiff --git a/spec/services/reminders/create_service_spec.rb b/spec/services/reminders/create_service_spec.rbindex 65dde3e508b..b220ee90aa7 100644--- a/spec/services/reminders/create_service_spec.rb+++ b/spec/services/reminders/create_service_spec.rb@@ -36,7 +36,7 @@ RSpec.describe Reminders::CreateService dolet(:factory) { :reminder }before do- allow(model_instance).to receive(:update_columns)+ allow(model_instance).to receive(:update_columns).and_return(true)allow(Reminders::ScheduleReminderJob).to receive(:schedule).with(model_instance).and_return(instance_double(Reminders::ScheduleReminderJob, job_id: 1))diff --git a/spec/workers/reminders/schedule_reminder_job_spec.rb b/spec/workers/reminders/schedule_reminder_job_spec.rbindex 95db3509756..2928e20cb68 100644--- a/spec/workers/reminders/schedule_reminder_job_spec.rb+++ b/spec/workers/reminders/schedule_reminder_job_spec.rb@@ -49,32 +49,28 @@ RSpec.describe Reminders::ScheduleReminderJob doit "creates a notification from the reminder" donotification_svc = nil- expect { notification_svc = subject }.to change(Notification, :count).by(1)-- notification = notification_svc.result+ expect { notification_svc = subject }.to change(Notification, :count).by(1) & change(ReminderNotification, :count).by(1)aggregate_failures "notification attributes" do+ notification = notification_svc.result+expect(notification.recipient_id).to eq(reminder.creator_id)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- expect(reminder.notification).to eq(notification)+ aggregate_failures "marks reminder as having unread notifications" do+ expect(reminder.reload).to be_an_unread_notificationendendcontext "when the reminder is already notified" do- let(:reminder) { build_stubbed(:reminder, :notified) }-before do- allow(Notifications::CreateService).to receive(:new).and_call_original+ create(:reminder_notification, reminder: reminder, notification: create(:notification, read_ian: false))endit "does not create a notification from the reminder" doexpect { subject }.not_to change(Notification, :count)- expect(Notifications::CreateService).not_to have_received(:new)endendend