Skip to content

Commit

Permalink
Keep track of reminder notifications
Browse files Browse the repository at this point in the history
Arises from: 73aa20c

Once a reminder is created we need to keep track of:
 (1) The scheduled job: this is done via Reminder#job_id which corresponds to a GoodJob::Job#id
 (2) Any Notification(s) resulting from the reminder

This is important in order to easily handle any modifications to the reminder such as snoozing, editing
& deleting reminders
  • Loading branch information
akabiru committed Nov 21, 2024
1 parent 67f524a commit 753f8a0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Notification < ApplicationRecord
belongs_to :journal
belongs_to :resource, polymorphic: true

has_many :reminder_notifications, dependent: :destroy

include Scopes::Scoped
scopes :unsent_reminders_before,
:mail_reminder_unsent,
Expand Down
4 changes: 4 additions & 0 deletions app/models/reminder_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ReminderNotification < ApplicationRecord
belongs_to :reminder
belongs_to :notification
end
1 change: 1 addition & 0 deletions app/workers/reminders/schedule_reminder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def perform(reminder)
return if reminder.scheduled?

create_notification_from_reminder(reminder)
.on_success { |service_result| ReminderNotification.create!(reminder:, notification: service_result.result) }
end

private
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20241121113638_create_reminder_notifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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
end
end
2 changes: 2 additions & 0 deletions spec/models/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
it { is_expected.to belong_to(:resource) }
it { is_expected.to belong_to(:actor).class_name("User") }
it { is_expected.to belong_to(:recipient).class_name("User") }

it { is_expected.to have_many(:notification_settings).dependent(:destroy) }
end

describe "Enums" do
Expand Down
36 changes: 36 additions & 0 deletions spec/models/reminder_notification_spec.rb
Original file line number Diff line number Diff line change
@@ -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
end
2 changes: 1 addition & 1 deletion spec/workers/reminders/schedule_reminder_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

it "creates a notification from the reminder" do
notification_svc = nil
expect { notification_svc = subject }.to change(Notification, :count).by(1)
expect { notification_svc = subject }.to change(Notification, :count).by(1) & change(ReminderNotification, :count).by(1)

aggregate_failures "notification attributes" do
notification = notification_svc.result
Expand Down

0 comments on commit 753f8a0

Please sign in to comment.