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 only for open work packages
Christophe Bliarda3a5e581784f
diff --git a/app/models/user.rb b/app/models/user.rbindex e6eef901c26..633b2b5f90d 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -83,6 +83,7 @@ class User < Principal:newest,:notified_globally,:watcher_recipients,+ :with_time_zone,:having_reminder_mail_to_senddef self.create_blocked_scope(scope, blocked)diff --git a/app/models/users/scopes/with_time_zone.rb b/app/models/users/scopes/with_time_zone.rbnew file mode 100644index 00000000000..f94f65e61b6--- /dev/null+++ b/app/models/users/scopes/with_time_zone.rb@@ -0,0 +1,54 @@+#-- 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.+#++++# Find a user account by matching case-insensitive.+module Users::Scopes+ module WithTimeZone+ extend ActiveSupport::Concern++ class_methods do+ def with_time_zone(time_zones)+ return User.none if time_zones.empty?++ where_clause = <<~SQL.squish+ COALESCE(+ NULLIF(user_preferences.settings->>'time_zone', ''),+ #{user_default_time_zone.to_sql}+ ) IN (?)+ SQL+ User+ .left_joins(:preference)+ .where(where_clause, time_zones)+ end++ def user_default_time_zone+ Arel::Nodes::build_quoted(Setting.user_default_timezone.presence || 'Etc/UTC')+ end+ end+ end+enddiff --git a/app/workers/notifications/create_date_alerts_notifications_job.rb b/app/workers/notifications/create_date_alerts_notifications_job.rbindex 9d88e00cb52..6236540c34a 100644--- a/app/workers/notifications/create_date_alerts_notifications_job.rb+++ b/app/workers/notifications/create_date_alerts_notifications_job.rb@@ -27,39 +27,42 @@#++module Notifications+ # Creates date alerts for users whose local time is 1:00 am.class CreateDateAlertsNotificationsJob < Cron::CronJob# runs every quarter of an hour, so 00:00, 00:15,..., 15:30, 15:45, 16:00, ...self.cron_expression = '*/15 * * * *'def perform- users_with_1am_local_time.each do |user|- work_package_with_involved(user)- .where(start_date: Date.current + 1.day)- .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- end- end-- def users_with_1am_local_timetime_zones = time_zones_covering_1am_local_time+ return if time_zones.empty?- return User.none if time_zones.empty?+ Time.use_zone(time_zones.first) do+ User.with_time_zone(time_zones).find_each do |user|+ send_start_date_alert_notifications(user)+ end+ end+ end- User- .joins(:preference)- .where("user_preferences.settings->>'time_zone' IN (?)", time_zones)+ def send_start_date_alert_notifications(user)+ work_package_with_involved(user)+ .where(start_date: Date.current + 1.day)+ .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+ )+ endenddef work_package_with_involved(user)- WorkPackage.where(assigned_to: user)+ WorkPackage+ .joins(:status)+ .where(statuses: { is_closed: false })+ .where(assigned_to: user)enddef time_zones_covering_1am_local_timediff --git a/spec/factories/user_factory.rb b/spec/factories/user_factory.rbindex bf01510b40d..dba9d755323 100644--- a/spec/factories/user_factory.rb+++ b/spec/factories/user_factory.rb@@ -49,7 +49,7 @@ FactoryBot.define doendcallback(:after_build) do |user, evaluator|- evaluator.preferences.each do |key, val|+ evaluator.preferences&.each do |key, val|user.pref[key] = valendenddiff --git a/spec/models/users/scopes/with_time_zone_spec.rb b/spec/models/users/scopes/with_time_zone_spec.rbnew file mode 100644index 00000000000..12fd8f1264b--- /dev/null+++ b/spec/models/users/scopes/with_time_zone_spec.rb@@ -0,0 +1,126 @@+#-- 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 'spec_helper'++describe Users::Scopes::WithTimeZone do+ shared_let(:user_besancon) do+ create(+ :user,+ firstname: 'Besançon',+ preferences: { time_zone: 'Europe/Paris' }+ )+ end+ shared_let(:user_kathmandu) do+ create(+ :user,+ firstname: 'Kathmandu',+ preferences: { time_zone: 'Asia/Kathmandu' }+ )+ end+ shared_let(:user_new_york) do+ create(+ :user,+ firstname: 'New York',+ preferences: { time_zone: 'America/New_York' }+ )+ end+ shared_let(:user_paris) do+ create(+ :user,+ firstname: 'Paris',+ preferences: { time_zone: 'Europe/Paris' }+ )+ end+ shared_let(:user_without_preferences) do+ create(+ :user,+ firstname: 'no preference',+ preferences: nil+ )+ end+ shared_let(:user_without_time_zone) do+ create(+ :user,+ firstname: 'no preference',+ preferences: {}+ )+ end+ shared_let(:user_with_empty_time_zone) do+ create(+ :user,+ firstname: 'no preference',+ preferences: { time_zone: '' }+ )+ end++ describe '.with_time_zone' do+ it 'returns user having set a time zone in their preference matching the specified time zone(s)' do+ expect(User.with_time_zone('Europe/Paris'))+ .to contain_exactly(user_paris, user_besancon)++ expect(User.with_time_zone([]))+ .to eq([])++ expect(User.with_time_zone(['America/New_York', 'Asia/Kathmandu']))+ .to contain_exactly(user_new_york, user_kathmandu)+ end++ context 'when users have no preferences' do+ it 'uses the default time zone returned by Setting.user_default_timezone',+ with_settings: { user_default_timezone: "Europe/Berlin" } do+ expect(User.with_time_zone("Europe/Berlin"))+ .to include(user_without_preferences)+ end+ end++ context 'when users have preferences without time zone set' do+ it 'uses the default time zone returned by Setting.user_default_timezone',+ with_settings: { user_default_timezone: "Europe/Berlin" } do+ expect(User.with_time_zone("Europe/Berlin"))+ .to include(user_without_time_zone)+ end+ end++ context 'when users have preferences with time zone set to empty string' do+ it 'uses the default time zone returned by Setting.user_default_timezone',+ with_settings: { user_default_timezone: "Europe/Berlin" } do+ expect(User.with_time_zone("Europe/Berlin"))+ .to include(user_with_empty_time_zone)+ end+ end++ context 'when users have no time zone and default user time zone is not set' do+ it 'assumes Etc/UTC as default time zone',+ with_settings: { user_default_timezone: nil } do+ expect(User.with_time_zone("Etc/UTC"))+ .to contain_exactly(user_without_preferences, user_without_time_zone, user_with_empty_time_zone)+ end+ 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 f6a4e2bc6de..716c102ce1a 100644--- a/spec/workers/notifications/create_date_alerts_notifications_job_spec.rb+++ b/spec/workers/notifications/create_date_alerts_notifications_job_spec.rb@@ -33,31 +33,38 @@ describe Notifications::CreateDateAlertsNotificationsJob, type: :job docreate_shared_association_defaults_for_work_package_factory- subject { scheduled_job.invoke_job }+ subject { scheduled_job.perform }++ 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) dodescribed_class.ensure_scheduled!- Delayed::Job.first+ described_class.delayed_jobend- let(:today) { Date.current }- let(:in_1_day) { today + 1.day }+ shared_let(:today) { Date.current }+ shared_let(:in_1_day) { today + 1.day }- let(:user_paris) do+ shared_let(:user_paris) docreate(:user,firstname: 'Europe/Paris',preferences: { time_zone: 'Europe/Paris' })end- let(:user_kathmandu) do+ shared_let(:user_kathmandu) docreate(:user,firstname: 'Asia/Kathmandu',preferences: { time_zone: 'Asia/Kathmandu' })end+ shared_let(:alertable_work_packages) do+ create_list(:work_package, 2,+ assigned_to: user_paris)+ endbefore do# We need to access the job as stored in the database to get at the run_at time persisted there@@ -95,46 +102,72 @@ describe Notifications::CreateDateAlertsNotificationsJob, type: :job doendend- def set_run_at(run_at)+ def set_scheduled_time(run_at)scheduled_job.update_column(:run_at, run_at)end+ def alertable_work_package(attributes = {})+ wp = alertable_work_packages.shift+ wp.update!(attributes.reverse_merge(start_date: in_1_day))+ wp+ end+describe '#perform' dolet(:timezone_paris) { ActiveSupport::TimeZone[user_paris.preference.time_zone] }+ let(:timezone_kathmandu) { ActiveSupport::TimeZone[user_kathmandu.preference.time_zone] }- context 'when scheduled and executed at 01:00 am Paris local time' do- it 'creates a start date alert notification for a user in the same time zone' do- set_run_at(timezone_paris.now.change(hour: 1, min: 0))- travel_to(timezone_paris.now.change(hour: 1, min: 0))- work_package = create(:work_package,- start_date: in_1_day,- assigned_to: user_paris)+ it 'creates date alert notifications only for open work packages' do+ open_work_package = alertable_work_package(status: status_open)+ closed_work_package = alertable_work_package(status: status_closed)++ set_scheduled_time(timezone_paris.now.change(hour: 1, min: 0))+ travel_to(timezone_paris.now.change(hour: 1, min: 0))++ scheduled_job.invoke_job++ expect(user_paris).to have_a_start_date_alert_notification_for(open_work_package)+ expect(user_paris).not_to have_a_start_date_alert_notification_for(closed_work_package)+ end+ it 'creates date alert notifications only for users whose local time is 1:00 am when the job is executed' do+ work_package_for_paris_user = alertable_work_package(assigned_to: user_paris)+ work_package_for_kathmandu_user = alertable_work_package(assigned_to: user_kathmandu)++ set_scheduled_time(timezone_paris.now.change(hour: 1, min: 0))+ travel_to(timezone_paris.now.change(hour: 1, min: 4)) doscheduled_job.invoke_job- expect(user_paris).to have_a_start_date_alert_notification_for(work_package)+ expect(user_paris).to have_a_start_date_alert_notification_for(work_package_for_paris_user)+ expect(user_kathmandu).not_to have_a_start_date_alert_notification_for(work_package_for_kathmandu_user)+ end++ # change scheduled time and current time to cover kathmandu timezone+ set_scheduled_time(timezone_kathmandu.now.change(hour: 1, min: 0))+ travel_to(timezone_kathmandu.now.change(hour: 1, min: 4)) do+ scheduled_job.reload.invoke_job++ expect(user_kathmandu).to have_a_start_date_alert_notification_for(work_package_for_kathmandu_user)end+ end- it 'does not create a start date alert notification for a user in another time zone' do- set_run_at(timezone_paris.now.change(hour: 1, min: 0))+ context 'when scheduled and executed at 01:00 am Paris local time' do+ it 'creates a start date alert notification for a user in the same time zone' do+ work_package = alertable_work_package++ set_scheduled_time(timezone_paris.now.change(hour: 1, min: 0))travel_to(timezone_paris.now.change(hour: 1, min: 0))- work_package = create(:work_package,- start_date: in_1_day,- assigned_to: user_kathmandu)scheduled_job.invoke_job- expect(user_kathmandu).not_to have_a_start_date_alert_notification_for(work_package)+ expect(user_paris).to have_a_start_date_alert_notification_for(work_package)endend- context 'when running at 01:14 am Paris local time' do+ context 'when scheduled and executed at 01:14 am Paris local time' doit 'creates a start date alert notification for a user in the same time zone' do- set_run_at(timezone_paris.now.change(hour: 1, min: 14))+ work_package = alertable_work_package+ set_scheduled_time(timezone_paris.now.change(hour: 1, min: 14))travel_to(timezone_paris.now.change(hour: 1, min: 14))- work_package = create(:work_package,- start_date: in_1_day,- assigned_to: user_paris)scheduled_job.invoke_job@@ -142,13 +175,12 @@ describe Notifications::CreateDateAlertsNotificationsJob, type: :job doendend- context 'when running at 01:15 am Paris local time' do+ context 'when scheduled and executed at 01:15 am Paris local time' doit 'does not create a start date alert notification for a user in the same time zone' do- set_run_at(timezone_paris.now.change(hour: 1, min: 15))+ work_package = alertable_work_package++ set_scheduled_time(timezone_paris.now.change(hour: 1, min: 15))travel_to(timezone_paris.now.change(hour: 1, min: 15))- work_package = create(:work_package,- start_date: in_1_day,- assigned_to: user_paris)scheduled_job.invoke_job