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
Trigger date alerts based on project specific settings
Christophe Bliard8f438bd55c98
diff --git a/app/workers/notifications/create_date_alerts_notifications_job.rb b/app/workers/notifications/create_date_alerts_notifications_job.rbindex c0726f43a58..651241d97d9 100644--- a/app/workers/notifications/create_date_alerts_notifications_job.rb+++ b/app/workers/notifications/create_date_alerts_notifications_job.rb@@ -38,46 +38,29 @@ module NotificationsTime.use_zone(time_zones.first) doUser.with_time_zone(time_zones).find_each do |user|- send_start_date_alert_notifications(user)- send_due_date_alert_notifications(user)+ send_date_alert_notifications(user)endendend- def send_start_date_alert_notifications(user)- start_date_alert = user.notification_settings.first.start_date- return if start_date_alert.nil?-- WorkPackage.with_status_open.involving_user(user)- .where(start_date: Date.current + start_date_alert.days)- .each do |work_package|- create_service = Notifications::CreateService.new(user:)- create_service.call(- recipient_id: user.id,- project_id: work_package.project_id,- resource: work_package,- reason: :date_alert_start_date,- read_ian: false- )- end+ def send_date_alert_notifications(user)+ alertables = AlertableWorkPackages.new(user)+ alertables.alertable_for_start.each do |work_package|+ create_date_alert_notification(user, work_package, :date_alert_start_date)+ end+ alertables.alertable_for_due.each do |work_package|+ create_date_alert_notification(user, work_package, :date_alert_due_date)+ endend- def send_due_date_alert_notifications(user)- due_date_alert = user.notification_settings.first.due_date- return if due_date_alert.nil?-- WorkPackage.with_status_open.involving_user(user)- .where(due_date: Date.current + due_date_alert.days)- .each do |work_package|- create_service = Notifications::CreateService.new(user:)- create_service.call(- recipient_id: user.id,- project_id: work_package.project_id,- resource: work_package,- reason: :date_alert_due_date,- read_ian: false- )- end+ def create_date_alert_notification(user, work_package, reason)+ create_service = Notifications::CreateService.new(user:)+ create_service.call(+ recipient_id: user.id,+ project_id: work_package.project_id,+ resource: work_package,+ reason:+ )enddef time_zones_covering_1am_local_timediff --git a/app/workers/notifications/create_date_alerts_notifications_job/alertable_work_packages.rb b/app/workers/notifications/create_date_alerts_notifications_job/alertable_work_packages.rbnew file mode 100644index 00000000000..f7d9d5fb677--- /dev/null+++ b/app/workers/notifications/create_date_alerts_notifications_job/alertable_work_packages.rb@@ -0,0 +1,124 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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 docs/COPYRIGHT.rdoc for more details.+#++++class Notifications::CreateDateAlertsNotificationsJob::AlertableWorkPackages+ attr_reader :user++ def initialize(user)+ @user = user+ end++ def alertable_for_start+ find_alertables+ .filter_map { |row| row["id"] if row["start_alert"] }+ .then { |ids| WorkPackage.where(id: ids) }+ end++ def alertable_for_due+ find_alertables+ .filter_map { |row| row["id"] if row["due_alert"] }+ .then { |ids| WorkPackage.where(id: ids) }+ end++ private++ def find_alertables+ @find_alertables ||= ActiveRecord::Base.connection.execute(query).to_a+ end++ def query+ alertables = alertable_work_packages(user)+ .select(:id,+ :project_id,+ "work_packages.start_date - #{today} AS start_delta",+ "work_packages.due_date - #{today} AS due_delta",+ "#{today} - work_packages.due_date AS overdue_delta")+ .where("work_packages.start_date - #{today} BETWEEN 0 AND 7 " \+ "OR work_packages.due_date - #{today} BETWEEN 0 and 7 " \+ "OR #{today} - work_packages.due_date > 0")++ <<~SQL.squish+ WITH+ alertable_work_packages (id, project_id, start_delta, due_delta, overdue_delta) AS (+ #{alertables.to_sql}+ ),+ project_ids AS (+ SELECT distinct project_id+ FROM alertable_work_packages+ ),+ project_notification_settings (project_id, start_delta, due_delta, overdue_step) AS (+ SELECT DISTINCT ON (project_ids.project_id)+ project_ids.project_id,+ notification_settings.start_date,+ notification_settings.due_date,+ notification_settings.overdue+ FROM project_ids+ LEFT OUTER JOIN notification_settings ON (+ notification_settings.project_id = project_ids.project_id+ OR notification_settings.project_id IS NULL+ )+ WHERE notification_settings.user_id = #{user.id}+ ORDER BY project_ids.project_id, notification_settings.project_id NULLS LAST+ ),+ notifiable_work_packages (id, start_alert, due_alert) AS (+ SELECT+ alertable_work_packages.id,+ pns.start_delta = alertable_work_packages.start_delta AS start_alert,+ (+ (pns.due_delta = alertable_work_packages.due_delta)+ OR (alertable_work_packages.overdue_delta > 0)+ ) AS due_alert+ FROM alertable_work_packages+ LEFT OUTER JOIN project_notification_settings AS pns USING (project_id)+ WHERE pns.start_delta = alertable_work_packages.start_delta+ OR pns.due_delta = alertable_work_packages.due_delta+ OR (+ (alertable_work_packages.overdue_delta > 0)+ AND (MOD(alertable_work_packages.overdue_delta - 1, pns.overdue_step) = 0)+ )+ )+ SELECT id, start_alert, due_alert+ FROM notifiable_work_packages+ SQL+ end++ def alertable_work_packages(user)+ relation = WorkPackage.with_status_open.involving_user(user)+ # `relation.to_sql` was producing SQL with weird select clauses that could+ # not be used in a CTE, while doing `relation.pluck(:something)` was producing+ # nice formatted sql. Reading `#pluck` source code revealed the existence+ # of `#join_dependency` and how to use it to have a nicely formatted sql+ # query.+ join_dependency = relation.construct_join_dependency([:status], Arel::Nodes::OuterJoin)+ relation.joins(join_dependency)+ end++ def today+ @today ||= Arel::Nodes::build_quoted(Date.current).to_sql+ end+enddiff --git a/spec/workers/notifications/create_date_alerts_notifications_job/alertable_work_packages_spec.rb b/spec/workers/notifications/create_date_alerts_notifications_job/alertable_work_packages_spec.rbnew file mode 100644index 00000000000..0d24858929d--- /dev/null+++ b/spec/workers/notifications/create_date_alerts_notifications_job/alertable_work_packages_spec.rb@@ -0,0 +1,386 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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 'rails_helper'++def relative_days(nb_days)+ case nb_days+ when 1 then 'tomorrow'+ when 0 then 'today'+ when -1 then 'yesterday'+ else nb_days > 0 ? "in #{nb_days} days" : "#{-nb_days} days ago"+ end+end++RSpec.describe Notifications::CreateDateAlertsNotificationsJob::AlertableWorkPackages do+ subject { described_class.new(user) }++ shared_let(:today) { Date.current }+ shared_let(:yesterday) { today - 1.day }+ shared_let(:in_1_day) { today + 1.day }++ shared_let(:role) { create(:existing_role, permissions: [:view_work_packages]) }+ shared_let(:user) { create(:user, firstname: 'main') }+ shared_let(:other_user) { create(:user, firstname: 'other') }+ shared_let(:project) { create(:project, name: 'main', members: { user => role }) }+ shared_let(:other_project) { create(:project, name: 'other', members: { user => role }) }++ shared_let(:status_open) { create(:status, name: "open", is_closed: false) }+ shared_let(:status_closed) { create(:status, name: "closed", is_closed: true) }++ shared_let(:alertable_work_packages) do+ create_list(:work_package, 3, project:, author: user)+ end++ let(:global_notification_settings) { user.notification_settings.first }++ def make_alertable_work_package(attributes = {})+ assignee = attributes.slice(:responsible, :responsible_id).any? ? nil : user+ attributes = attributes.reverse_merge(+ start_date: in_1_day,+ assigned_to: assignee+ )+ wp = alertable_work_packages.shift+ wp.update!(attributes)+ wp+ end++ ### open / closed work package++ it 'returns open work packages' do+ open_work_package = make_alertable_work_package(status: status_open)+ expect(subject.alertable_for_start).to include(open_work_package)+ end++ it 'does not return closed work packages' do+ make_alertable_work_package(status: status_closed)+ expect(subject.alertable_for_start).to be_empty+ end++ ### user involved++ it 'returns work packages the user is assigned to' do+ assigned_work_package = make_alertable_work_package(assigned_to: user)+ expect(subject.alertable_for_start).to include(assigned_work_package)+ end++ it 'returns work packages the user is responsible / accountable for' do+ accountable_work_package = make_alertable_work_package(responsible: user)+ expect(subject.alertable_for_start).to include(accountable_work_package)+ end++ it 'returns work packages the user is watching' do+ watched_work_package = make_alertable_work_package(assigned_to: nil)+ create(:watcher, watchable: watched_work_package, user:)+ expect(subject.alertable_for_start).to include(watched_work_package)+ end++ it 'does not return work packages the user is author of' do+ make_alertable_work_package(author: user, assigned_to: nil)+ expect(subject.alertable_for_start).to be_empty+ end++ it 'does not return work packages other users are involved into' do+ make_alertable_work_package(assigned_to: other_user)+ expect(subject.alertable_for_start).to be_empty+ end++ ### default notification settings++ context 'with default global notification settings' do+ it 'returns work packages starting in 1 day for a user' do+ wp_start_in_1_day = make_alertable_work_package(start_date: in_1_day, due_date: nil)+ expect(subject.alertable_for_start).to include(wp_start_in_1_day)+ end++ it 'returns work packages due in 1 day for a user' do+ wp_due_in_1_day = make_alertable_work_package(start_date: nil, due_date: in_1_day)+ expect(subject.alertable_for_due).to include(wp_due_in_1_day)+ end++ it 'does not return overdue work packages for a user' do+ make_alertable_work_package(start_date: nil, due_date: yesterday)+ expect(subject.alertable_for_due).to be_empty+ end+ end++ ### start_date specs++ shared_examples 'alertable if start date' do |days_from_now:|+ date = Date.current + days_from_now.days++ it "returns work packages with start date being #{relative_days(days_from_now)}" do+ work_package = make_alertable_work_package(start_date: date, due_date: nil)+ expect(subject.alertable_for_start).to include(work_package)+ end+ end++ shared_examples 'not alertable if start date' do |days_from_now:|+ date = Date.current + days_from_now.days++ it "does not return work packages with start date being #{relative_days(days_from_now)}" do+ make_alertable_work_package(start_date: date, due_date: nil)+ expect(subject.alertable_for_start).to be_empty+ end+ end++ context 'with notification settings start_date not set' do+ before do+ global_notification_settings.update(start_date: nil, due_date: nil, overdue: nil)+ end++ include_examples 'not alertable if start date', days_from_now: 0+ include_examples 'not alertable if start date', days_from_now: 1+ include_examples 'not alertable if start date', days_from_now: 3+ include_examples 'not alertable if start date', days_from_now: 42+ end++ context 'with notification settings start_date set to same day' do+ before do+ global_notification_settings.update(start_date: 0, due_date: nil, overdue: nil)+ end++ include_examples 'alertable if start date', days_from_now: 0+ include_examples 'not alertable if start date', days_from_now: 1+ include_examples 'not alertable if start date', days_from_now: 3+ include_examples 'not alertable if start date', days_from_now: 7+ end++ context 'with notification settings start_date set to 1 day' do+ before do+ global_notification_settings.update(start_date: 1, due_date: nil, overdue: nil)+ end++ include_examples 'not alertable if start date', days_from_now: 0+ include_examples 'alertable if start date', days_from_now: 1+ include_examples 'not alertable if start date', days_from_now: 3+ include_examples 'not alertable if start date', days_from_now: 7+ end++ context 'with notification settings start_date set to 3 days' do+ before do+ global_notification_settings.update(start_date: 3, due_date: nil, overdue: nil)+ end++ include_examples 'not alertable if start date', days_from_now: 0+ include_examples 'not alertable if start date', days_from_now: 1+ include_examples 'alertable if start date', days_from_now: 3+ include_examples 'not alertable if start date', days_from_now: 7+ end++ context 'with notification settings start_date set to 7 days' do+ before do+ global_notification_settings.update(start_date: 7, due_date: nil, overdue: nil)+ end++ include_examples 'not alertable if start date', days_from_now: 0+ include_examples 'not alertable if start date', days_from_now: 1+ include_examples 'not alertable if start date', days_from_now: 3+ include_examples 'alertable if start date', days_from_now: 7+ end++ ### due_date specs++ shared_examples 'alertable if due date' do |days_from_now:|+ date = Date.current + days_from_now.days++ it "returns work packages with due date being #{relative_days(days_from_now)}" do+ work_package = make_alertable_work_package(start_date: nil, due_date: date)+ expect(subject.alertable_for_due).to include(work_package)+ end+ end++ shared_examples 'not alertable if due date' do |days_from_now:|+ date = Date.current + days_from_now.days++ it "does not return work packages with due date being #{relative_days(days_from_now)}" do+ make_alertable_work_package(start_date: nil, due_date: date)+ expect(subject.alertable_for_due).to be_empty+ end+ end++ context 'with notification settings due_date not set' do+ before do+ global_notification_settings.update(start_date: nil, due_date: nil, overdue: nil)+ end++ include_examples 'not alertable if due date', days_from_now: 0+ include_examples 'not alertable if due date', days_from_now: 1+ include_examples 'not alertable if due date', days_from_now: 3+ include_examples 'not alertable if due date', days_from_now: 42+ end++ context 'with notification settings due_date set to same day' do+ before do+ global_notification_settings.update(start_date: nil, due_date: 0, overdue: nil)+ end++ include_examples 'alertable if due date', days_from_now: 0+ include_examples 'not alertable if due date', days_from_now: 1+ include_examples 'not alertable if due date', days_from_now: 3+ include_examples 'not alertable if due date', days_from_now: 7+ end++ context 'with notification settings due_date set to 1 day' do+ before do+ global_notification_settings.update(start_date: nil, due_date: 1, overdue: nil)+ end++ include_examples 'not alertable if due date', days_from_now: 0+ include_examples 'alertable if due date', days_from_now: 1+ include_examples 'not alertable if due date', days_from_now: 3+ include_examples 'not alertable if due date', days_from_now: 7+ end++ context 'with notification settings due_date set to 3 days' do+ before do+ global_notification_settings.update(start_date: nil, due_date: 3, overdue: nil)+ end++ include_examples 'not alertable if due date', days_from_now: 0+ include_examples 'not alertable if due date', days_from_now: 1+ include_examples 'alertable if due date', days_from_now: 3+ include_examples 'not alertable if due date', days_from_now: 7+ end++ context 'with notification settings due_date set to 7 days' do+ before do+ global_notification_settings.update(start_date: nil, due_date: 7, overdue: nil)+ end++ include_examples 'not alertable if due date', days_from_now: 0+ include_examples 'not alertable if due date', days_from_now: 1+ include_examples 'not alertable if due date', days_from_now: 3+ include_examples 'alertable if due date', days_from_now: 7+ end++ ### overdue specs++ shared_examples 'alertable if due' do |days_ago:|+ date = Date.current - days_ago.days++ it "returns work packages due #{relative_days(-days_ago)}" do+ work_package = make_alertable_work_package(start_date: nil, due_date: date)+ expect(subject.alertable_for_due).to include(work_package)+ end+ end++ shared_examples 'not alertable if due' do |days_ago:|+ date = Date.current - days_ago.days++ it "does not return work packages due #{relative_days(-days_ago)}" do+ make_alertable_work_package(start_date: nil, due_date: date)+ expect(subject.alertable_for_due).to be_empty+ end+ end++ context 'with notification settings overdue not set' do+ before do+ global_notification_settings.update(start_date: nil, due_date: nil, overdue: nil)+ end++ include_examples 'not alertable if due', days_ago: 0+ include_examples 'not alertable if due', days_ago: 1+ include_examples 'not alertable if due', days_ago: 2+ include_examples 'not alertable if due', days_ago: 3+ include_examples 'not alertable if due', days_ago: 42+ end++ context 'with notification settings overdue set to every day' do+ before do+ global_notification_settings.update(start_date: nil, due_date: nil, overdue: 1)+ end++ include_examples 'not alertable if due', days_ago: 0+ include_examples 'alertable if due', days_ago: 1+ include_examples 'alertable if due', days_ago: 2+ include_examples 'alertable if due', days_ago: 10+ end++ context 'with notification settings overdue set to every 3 days' do+ before do+ global_notification_settings.update(start_date: nil, due_date: nil, overdue: 3)+ end++ include_examples 'not alertable if due', days_ago: 0+ include_examples 'alertable if due', days_ago: 1+ include_examples 'not alertable if due', days_ago: 2+ include_examples 'not alertable if due', days_ago: 3+ include_examples 'alertable if due', days_ago: 4+ include_examples 'alertable if due', days_ago: 7+ end++ context 'with notification settings overdue set to every 7 days' do+ before do+ global_notification_settings.update(start_date: nil, due_date: nil, overdue: 7)+ end++ include_examples 'not alertable if due', days_ago: 0+ include_examples 'alertable if due', days_ago: 1+ include_examples 'not alertable if due', days_ago: 2+ include_examples 'not alertable if due', days_ago: 3+ include_examples 'not alertable if due', days_ago: 4+ include_examples 'not alertable if due', days_ago: 7+ include_examples 'alertable if due', days_ago: 8+ include_examples 'alertable if due', days_ago: 15+ end++ ### project-specific notification settings specs++ context 'with project notification settings overriding global notification settings' do+ shared_let(:project_notification_settings) { user.notification_settings.create(project:) }++ it 'uses the project settings' do+ global_notification_settings.update(start_date: nil, due_date: nil, overdue: nil)+ project_notification_settings.update(start_date: 1, due_date: 1, overdue: 1)++ wp_start_and_due_in_1_day =+ make_alertable_work_package(start_date: in_1_day, due_date: in_1_day, project:)+ wp_overdue =+ make_alertable_work_package(start_date: nil, due_date: yesterday, project:)+ wp_start_and_due_in_1_day_other_project =+ make_alertable_work_package(start_date: in_1_day, due_date: in_1_day, project: other_project)++ expect(subject.alertable_for_start).to include(wp_start_and_due_in_1_day)+ expect(subject.alertable_for_due).to include(wp_start_and_due_in_1_day, wp_overdue)++ expect(subject.alertable_for_start).not_to include(wp_start_and_due_in_1_day_other_project)+ expect(subject.alertable_for_due).not_to include(wp_start_and_due_in_1_day_other_project)+ end++ it 'does not use global settings if project settings are nil' do+ global_notification_settings.update(start_date: 1, due_date: 1)+ project_notification_settings.update(start_date: nil, due_date: nil)++ make_alertable_work_package(start_date: in_1_day, due_date: in_1_day, project:)++ expect(subject.alertable_for_start).to be_empty+ expect(subject.alertable_for_due).to be_empty+ end+ end+enddiff --git a/spec/workers/notifications/create_date_alerts_notifications_job_spec.rb b/spec/workers/notifications/create_date_alerts_notifications_job_spec.rbindex 567037d5155..f6da7d562cb 100644--- a/spec/workers/notifications/create_date_alerts_notifications_job_spec.rb+++ b/spec/workers/notifications/create_date_alerts_notifications_job_spec.rb@@ -31,22 +31,15 @@ require 'spec_helper'describe Notifications::CreateDateAlertsNotificationsJob, type: :job doinclude ActiveSupport::Testing::TimeHelpers- create_shared_association_defaults_for_work_package_factory-- subject { scheduled_job.perform }+ shared_let(:project) { create(:project, name: 'main') }shared_let(:status_open) { create(:status, name: "open", is_closed: false) }shared_let(:status_closed) { create(:status, name: "closed", is_closed: true) }- let(:scheduled_job) do- described_class.ensure_scheduled!-- described_class.delayed_job- end-shared_let(:today) { Date.current }shared_let(:in_1_day) { today + 1.day }shared_let(:in_3_days) { today + 3.days }+ shared_let(:in_7_days) { today + 7.days }# Paris and Berlin are both UTC+01:00 (CET) or UTC+02:00 (CEST)shared_let(:user_paris) do@@ -72,8 +65,14 @@ describe Notifications::CreateDateAlertsNotificationsJob, type: :job dopreferences: { time_zone: 'Asia/Kathmandu' })end+shared_let(:alertable_work_packages) do- create_list(:work_package, 2)+ create_list(:work_package, 2, project:, author: user_paris)+ end++ let(:scheduled_job) do+ described_class.ensure_scheduled!+ described_class.delayed_jobendbefore do@@ -241,6 +240,40 @@ describe Notifications::CreateDateAlertsNotificationsJob, type: :job doendend+ context 'when project notification settings are defined for a user' do+ it 'creates date alert notifications using these settings for work packages of the project' do+ # global notification settings+ user_paris.notification_settings.first.update(+ start_date: 1,+ due_date: nil+ )+ # project notifications settings+ user_paris.notification_settings.create(+ project:,+ start_date: nil,+ due_date: 7+ )+ silent_work_package = alertable_work_package(assigned_to: user_paris,+ project:,+ start_date: in_1_day,+ due_date: in_1_day)+ noisy_work_package = alertable_work_package(assigned_to: user_paris,+ project:,+ start_date: in_7_days,+ due_date: in_7_days)++ set_scheduled_time(timezone_paris.now.change(hour: 1, min: 0))+ travel_to(timezone_paris.now.change(hour: 1, min: 4)) do+ scheduled_job.invoke_job++ expect(user_paris).not_to have_a_start_date_alert_notification_for(silent_work_package)+ expect(user_paris).not_to have_a_due_date_alert_notification_for(silent_work_package)+ expect(user_paris).not_to have_a_start_date_alert_notification_for(noisy_work_package)+ expect(user_paris).to have_a_due_date_alert_notification_for(noisy_work_package)+ end+ end+ end+context 'when scheduled and executed at 01:00 am Paris local time' doit 'creates a start date alert notification for a user in the same time zone' dowork_package = alertable_work_package