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
Define set reminders update service
Kabiru Mwenja724f899033fe
diff --git a/app/contracts/reminders/update_contract.rb b/app/contracts/reminders/update_contract.rbnew file mode 100644index 00000000000..e09a94cdc81--- /dev/null+++ b/app/contracts/reminders/update_contract.rb@@ -0,0 +1,41 @@+#-- 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 UpdateContract < BaseContract+ validate :unchangeable_attributes++ private++ def unchangeable_attributes+ if model.remindable_changed? || model.creator_id_changed?+ errors.add(:base, :unchangeable_attributes)+ end+ end+ end+enddiff --git a/app/models/reminder.rb b/app/models/reminder.rbindex d960aa724cc..0c8bb474b61 100644--- a/app/models/reminder.rb+++ b/app/models/reminder.rb@@ -37,6 +37,10 @@ class Reminder < ApplicationRecordnotifications.exists?(read_ian: [false, nil])end+ def unread_notifications+ notifications.where(read_ian: [false, nil])+ end+def scheduled?job_id.present?enddiff --git a/app/services/reminders/update_service.rb b/app/services/reminders/update_service.rbnew file mode 100644index 00000000000..dce0e6b0688--- /dev/null+++ b/app/services/reminders/update_service.rb@@ -0,0 +1,63 @@+#-- 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 UpdateService < ::BaseServices::Update+ def after_perform(service_call)+ reminder = service_call.result++ if remind_at_changed?(reminder)+ destroy_scheduled_reminder_job(reminder.job_id) if reminder.scheduled?+ mark_unread_notifications_as_read_for(reminder) if reminder.unread_notifications?++ job = Reminders::ScheduleReminderJob.schedule(reminder)+ reminder.update_columns(job_id: job.job_id)+ end++ service_call+ end++ private++ def remind_at_changed?(reminder)+ # For some reason reminder.remind_at_changed? returns false+ # so we assume a change if remind_at is present in the params (would have passed contract validation)+ params.key?(:remind_at) && reminder.remind_at == params[:remind_at]+ end++ def destroy_scheduled_reminder_job(job_id)+ return unless job = GoodJob::Job.find_by(id: job_id)++ job.destroy unless job.finished?+ end++ def mark_unread_notifications_as_read_for(reminder)+ reminder.unread_notifications.update_all(read_ian: true)+ end+ end+enddiff --git a/spec/contracts/reminders/update_contract_spec.rb b/spec/contracts/reminders/update_contract_spec.rbnew file mode 100644index 00000000000..1feb81a88fd--- /dev/null+++ b/spec/contracts/reminders/update_contract_spec.rb@@ -0,0 +1,68 @@+# 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 "contracts/shared/model_contract_shared_context"++RSpec.describe Reminders::UpdateContract do+ include_context "ModelContract shared context"++ let(:contract) { described_class.new(reminder, user) }+ let(:user) { build_stubbed(:admin) }+ let(:creator) { user }+ let(:reminder) { build_stubbed(:reminder, creator:) }++ before do+ User.current = user+ allow(User).to receive(:exists?).with(user.id).and_return(true)+ end++ describe "validate unchangeable attributes" do+ context "when remindable changed" do+ before do+ reminder.remindable = build_stubbed(:work_package)+ end++ it_behaves_like "contract is invalid", base: :unchangeable_attributes+ end++ context "when creator_id changed" do+ before do+ new_creator = build_stubbed(:user)+ reminder.creator = new_creator+ allow(User).to receive(:exists?).with(new_creator.id).and_return(true)+ end++ it_behaves_like "contract is invalid", base: :unchangeable_attributes+ end+ end++ include_examples "contract reuses the model errors"+enddiff --git a/spec/models/reminder_spec.rb b/spec/models/reminder_spec.rbindex 8b730464012..8e77a7bcc14 100644--- a/spec/models/reminder_spec.rb+++ b/spec/models/reminder_spec.rb@@ -41,18 +41,21 @@ RSpec.describe Reminder dosubject { create(:reminder, :with_unread_notifications) }it { is_expected.to be_an_unread_notification }+ it { expect(subject.unread_notifications).to be_present }endcontext "with no unread notifications" dosubject { create(:reminder, :with_read_notifications) }it { is_expected.not_to be_an_unread_notification }+ it { expect(subject.unread_notifications).to be_empty }endcontext "with no notifications" do- subject { build(:reminder, notifications: []) }+ subject { build_stubbed(:reminder) }it { is_expected.not_to be_an_unread_notification }+ it { expect(subject.unread_notifications).to be_empty }endenddiff --git a/spec/services/reminders/update_service_spec.rb b/spec/services/reminders/update_service_spec.rbnew file mode 100644index 00000000000..b3cc66b9785--- /dev/null+++ b/spec/services/reminders/update_service_spec.rb@@ -0,0 +1,102 @@+# 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_update_service"++RSpec.describe Reminders::UpdateService do+ it_behaves_like "BaseServices update service" do+ let(:factory) { :reminder }+ end++ describe "remind_at changed" do+ subject { described_class.new(user:, model: model_instance).call(call_attributes) }++ let(:model_instance) { create(:reminder, :scheduled, creator: user, job_id: 1) }+ let(:user) { create(:admin) }+ let(:call_attributes) { { remind_at: 2.days.from_now } }++ before do+ allow(Reminders::ScheduleReminderJob).to receive(:schedule)+ .with(model_instance)+ .and_return(instance_double(Reminders::ScheduleReminderJob, job_id: 2))+ end++ context "with an existing unfinished scheduled job" do+ let(:job) { instance_double(GoodJob::Job, finished?: false, destroy: true) }++ before do+ allow(GoodJob::Job).to receive(:find_by).with(id: "1").and_return(job)+ end++ it "reschedules the reminder" do+ expect { subject }.to change(model_instance, :job_id).from("1").to("2")++ 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 "schedule new job" do+ expect(Reminders::ScheduleReminderJob).to have_received(:schedule).with(model_instance)+ end+ end+ end++ context "with an existing finished scheduled job" do+ let(:job) { instance_double(GoodJob::Job, finished?: true, destroy: true) }++ before do+ allow(GoodJob::Job).to receive(:find_by).with(id: "1").and_return(job)+ end++ it "schedules a new job" do+ expect { subject }.to change(model_instance, :job_id).from("1").to("2")++ 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 "schedule new job" do+ expect(Reminders::ScheduleReminderJob).to have_received(:schedule).with(model_instance)+ end+ end+ end++ context "with remind_at attribute in non-utc timezone" do+ let(:call_attributes) { { remind_at: 2.days.from_now.in_time_zone("Africa/Nairobi") } }++ it "schedules the reminder" do+ expect { subject }.to change(model_instance, :job_id).from("1").to("2")+ end+ end+ end+end