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

Add reminder completion via delete service

Kabiru Mwenja36e23d9d5512
diff --git a/app/models/reminder.rb b/app/models/reminder.rb
index 6c1eb71ef41..1228420fc19 100644
--- a/app/models/reminder.rb
+++ b/app/models/reminder.rb
@@ -41,7 +41,11 @@ class Reminder < ApplicationRecord
notifications.where(read_ian: [false, nil])
end
+ def completed?
+ completed_at.present?
+ end
+
def scheduled?
- job_id.present?
+ job_id.present? && !completed?
end
end
diff --git a/app/services/reminders/delete_service.rb b/app/services/reminders/delete_service.rb
new file mode 100644
index 00000000000..8250ffe12f8
--- /dev/null
+++ b/app/services/reminders/delete_service.rb
@@ -0,0 +1,39 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+module Reminders
+ class DeleteService < ::BaseServices::Delete
+ include Reminders::ServiceHelpers
+
+ def destroy(reminder)
+ destroy_scheduled_reminder_job(reminder)
+ mark_unread_notifications_as_read_for(reminder)
+ reminder.update(completed_at: Time.zone.now)
+ end
+ end
+end
diff --git a/db/migrate/20241119131205_create_reminders.rb b/db/migrate/20241119131205_create_reminders.rb
index 454e830fefd..cb2a7325589 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 :completed_at
t.string :job_id
t.text :note
diff --git a/spec/factories/reminders_factory.rb b/spec/factories/reminders_factory.rb
index c1b1bd0d50d..0c195743a1e 100644
--- a/spec/factories/reminders_factory.rb
+++ b/spec/factories/reminders_factory.rb
@@ -37,6 +37,11 @@ FactoryBot.define do
job_id { SecureRandom.uuid }
end
+ trait :completed do
+ job_id { SecureRandom.uuid }
+ completed_at { Time.zone.now }
+ end
+
trait :with_unread_notifications do
after(:create) do |reminder|
create(:reminder_notification, reminder:, notification: create(:notification, read_ian: false))
diff --git a/spec/models/reminder_spec.rb b/spec/models/reminder_spec.rb
index 8e77a7bcc14..3c3a9c675a7 100644
--- a/spec/models/reminder_spec.rb
+++ b/spec/models/reminder_spec.rb
@@ -71,5 +71,25 @@ RSpec.describe Reminder do
expect(reminder).not_to be_scheduled
end
+
+ it "returns false if completed_at is present" do
+ reminder = build(:reminder, :scheduled, :completed)
+
+ expect(reminder).not_to be_scheduled
+ end
+ end
+
+ describe "#completed?" do
+ it "returns true if completed_at is present" do
+ reminder = build(:reminder, :completed)
+
+ expect(reminder).to be_completed
+ end
+
+ it "returns false if completed_at is not present" do
+ reminder = build(:reminder, completed_at: nil)
+
+ expect(reminder).not_to be_completed
+ end
end
end
diff --git a/spec/services/reminders/delete_service_spec.rb b/spec/services/reminders/delete_service_spec.rb
new file mode 100644
index 00000000000..40607fecb3c
--- /dev/null
+++ b/spec/services/reminders/delete_service_spec.rb
@@ -0,0 +1,98 @@
+# frozen_string_literal: true
+
+#-- 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"
+require "services/base_services/behaves_like_delete_service"
+
+RSpec.describe Reminders::DeleteService do
+ it_behaves_like "BaseServices delete service" do
+ let(:factory) { :reminder }
+
+ before do
+ allow(model_instance).to receive(:update).and_return(model_destroy_result)
+ end
+ end
+
+ describe "Remove reminder" do
+ subject { described_class.new(user:, model: model_instance).call }
+
+ let(:model_instance) { create(:reminder, :scheduled, :with_unread_notifications, creator: user) }
+ let(:user) { create(:admin) }
+
+ context "with an existing unfinished scheduled job" do
+ let(:job) { instance_double(GoodJob::Job, finished?: false, destroy: true) }
+
+ before do
+ model_instance.update(job_id: 1, completed_at: nil)
+ allow(GoodJob::Job).to receive(:find_by).and_return(job)
+ end
+
+ it "completes the reminder" do
+ expect { subject }.to change(model_instance, :completed_at).from(nil)
+
+ aggregate_failures "destroy existing job" do
+ expect(GoodJob::Job).to have_received(:find_by).with(id: "1")
+ expect(job).to have_received(:destroy)
+ end
+
+ aggregate_failures "marks unread notifications as read" do
+ expect(model_instance.notifications.count).to eq(1)
+ expect(model_instance.unread_notifications.count).to eq(0)
+ end
+
+ aggregate_failures "marks the reminder as complete" do
+ expect(model_instance).to be_completed
+ end
+ end
+ end
+
+ context "with an existing finished scheduled job" do
+ let(:job) { instance_double(GoodJob::Job, finished?: true, destroy: true) }
+
+ before do
+ model_instance.update(job_id: 1, completed_at: nil)
+ allow(GoodJob::Job).to receive(:find_by).and_return(job)
+ end
+
+ it "completes the reminder" do
+ expect { subject }.to change(model_instance, :completed_at).from(nil)
+
+ aggregate_failures "does NOT destroy existing job" do
+ expect(GoodJob::Job).to have_received(:find_by).with(id: "1")
+ expect(job).not_to have_received(:destroy)
+ end
+
+ aggregate_failures "marks the reminder as complete" do
+ expect(model_instance).to be_completed
+ end
+ end
+ end
+ end
+end