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
Add date alerts creation job in feature spec
Christophe Bliard18b354429e6d
diff --git a/app/workers/notifications/create_date_alerts_notifications_job.rb b/app/workers/notifications/create_date_alerts_notifications_job.rbindex 188a06d24fa..15505fcd2e1 100644--- a/app/workers/notifications/create_date_alerts_notifications_job.rb+++ b/app/workers/notifications/create_date_alerts_notifications_job.rb@@ -35,61 +35,8 @@ module Notificationsdef performreturn unless EnterpriseToken.allows_to?(:date_alerts)- time_zones = time_zones_covering_1am_local_time- return if time_zones.empty?-- Time.use_zone(time_zones.first) do- User.with_time_zone(time_zones).find_each do |user|- send_date_alert_notifications(user)- end- end- end-- def send_date_alert_notifications(user)- alertables = AlertableWorkPackages.new(user)- create_date_alert_notifications(user, alertables.alertable_for_start, :date_alert_start_date)- create_date_alert_notifications(user, alertables.alertable_for_due, :date_alert_due_date)- end-- def create_date_alert_notifications(user, work_packages, reason)- mark_previous_notifications_as_read(user, work_packages, reason)- work_packages.find_each do |work_package|- create_date_alert_notification(user, work_package, reason)- end- end-- def mark_previous_notifications_as_read(user, work_packages, reason)- Notification- .where(recipient: user,- reason:,- resource: work_packages)- .update_all(read_ian: true, updated_at: Time.current)- 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:- )- end-- def time_zones_covering_1am_local_time- UserPreferences::UpdateContract- .assignable_time_zones- .select { |time_zone| executing_at_1am_for_timezone?(time_zone) }- .map { |time_zone| time_zone.tzinfo.canonical_zone.name }- end-- def executing_at_1am_for_timezone?(time_zone)- times_from_scheduled_to_execution.any? { |time| is_1am?(time, time_zone) }- end-- def is_1am?(time, time_zone)- local_time = time.in_time_zone(time_zone)- local_time.strftime('%H:%M') == '01:00'+ service = Service.new(times_from_scheduled_to_execution)+ service.callend# Returns times from scheduled execution time to current time in 15 minutes@@ -100,20 +47,17 @@ module Notifications# between scheduled time and current time need to be considered to match# with 1:00am in a time zone.def times_from_scheduled_to_execution- @times_from_scheduled_to_execution ||= begin- time = scheduled_time- times = []- begin- times << time- time += 15.minutes- end while time < Time.current- times- end+ time = scheduled_time+ times = []+ begin+ times << time+ time += 15.minutes+ end while time < Time.current+ timesenddef scheduled_time- @scheduled_time ||=- self.class.delayed_job.run_at.then { |t| t.change(min: t.min / 15 * 15) }+ self.class.delayed_job.run_at.then { |t| t.change(min: t.min / 15 * 15) }endendenddiff --git a/app/workers/notifications/create_date_alerts_notifications_job/service.rb b/app/workers/notifications/create_date_alerts_notifications_job/service.rbnew file mode 100644index 00000000000..acb37190253--- /dev/null+++ b/app/workers/notifications/create_date_alerts_notifications_job/service.rb@@ -0,0 +1,105 @@+#-- 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.+#++++# Creates date alerts notifications for users whose local time is 1am for the+# given run_times.+class Notifications::CreateDateAlertsNotificationsJob::Service+ attr_reader :run_times++ # @param run_times [Array<DateTime>] the times for which the service is run.+ # Must be multiple of 15 minutes (xx:00, xx:15, xx:30, or xx:45).+ def initialize(run_times)+ @run_times = run_times+ end++ def call+ return unless EnterpriseToken.allows_to?(:date_alerts)++ time_zones = time_zones_covering_1am_local_time+ return if time_zones.empty?++ # warning: there may be a subtle bug here: if many run_times are given, time+ # zones will have different time shifting. This should be ok: as the period+ # covered is small this should not have any impact. If the period is more+ # than 23h, then the day will change.+ Time.use_zone(time_zones.first) do+ User.with_time_zone(time_zones).find_each do |user|+ send_date_alert_notifications(user)+ end+ end+ end++ private++ def send_date_alert_notifications(user)+ alertables = Notifications::CreateDateAlertsNotificationsJob::AlertableWorkPackages.new(user)+ create_date_alert_notifications(user, alertables.alertable_for_start, :date_alert_start_date)+ create_date_alert_notifications(user, alertables.alertable_for_due, :date_alert_due_date)+ end++ def create_date_alert_notifications(user, work_packages, reason)+ mark_previous_notifications_as_read(user, work_packages, reason)+ work_packages.find_each do |work_package|+ create_date_alert_notification(user, work_package, reason)+ end+ end++ def mark_previous_notifications_as_read(user, work_packages, reason)+ Notification+ .where(recipient: user,+ reason:,+ resource: work_packages)+ .update_all(read_ian: true, updated_at: Time.current)+ 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:+ )+ end++ def time_zones_covering_1am_local_time+ UserPreferences::UpdateContract+ .assignable_time_zones+ .select { |time_zone| executing_at_1am_for_timezone?(time_zone) }+ .map { |time_zone| time_zone.tzinfo.canonical_zone.name }+ end++ def executing_at_1am_for_timezone?(time_zone)+ run_times.any? { |time| is_1am?(time, time_zone) }+ end++ def is_1am?(time, time_zone)+ local_time = time.in_time_zone(time_zone)+ local_time.strftime('%H:%M') == '01:00'+ end+enddiff --git a/spec/features/notifications/notification_center/notification_center_date_alerts_spec.rb b/spec/features/notifications/notification_center/notification_center_date_alerts_spec.rbindex 690664c1756..e2c9122d5f1 100644--- a/spec/features/notifications/notification_center/notification_center_date_alerts_spec.rb+++ b/spec/features/notifications/notification_center/notification_center_date_alerts_spec.rb@@ -1,42 +1,76 @@require 'spec_helper'-describe "Notification center date alerts", js: true, with_settings: { journal_aggregation_time_minutes: 0 } do- create_shared_association_defaults_for_work_package_factory-- shared_let(:project) { project_with_types }+# rubocop:disable RSpec/ScatteredLet+describe "Notification center date alerts", js: true,+ with_ee: %i[date_alerts],+ with_settings: { journal_aggregation_time_minutes: 0 } do+ include ActiveSupport::Testing::TimeHelpers++ shared_let(:time_zone) { ActiveSupport::TimeZone['Europe/Berlin'] }+ shared_let(:user) do+ create(:user, preferences: { time_zone: time_zone.name }).tap do |user|+ user.notification_settings.first.update(+ start_date: 7,+ due_date: 3,+ overdue: 1+ )+ end+ end+ shared_let(:project) { create(:project_with_types) }shared_let(:role) { create(:role, permissions: %i[view_work_packages edit_work_packages work_package_assigned]) }- shared_let(:membership) { create(:member, principal: user, project: project_with_types, roles: [role]) }+ shared_let(:membership) { create(:member, principal: user, project:, roles: [role]) }shared_let(:milestone_type) { create(:type_milestone) }+ def create_alertable(**attributes)+ attributes = attributes.reverse_merge(assigned_to: user, project:)+ create(:work_package, **attributes)+ end++ # notification will be created by the job because `overdue: 1` in user notifications settingsshared_let(:milestone_wp_past) do- create(:work_package, subject: 'Milestone WP past', project:, type: milestone_type, due_date: 2.days.ago)+ create_alertable(subject: 'Milestone WP past', type: milestone_type, due_date: time_zone.today - 2.days)end+shared_let(:milestone_wp_future) do- create(:work_package, subject: 'Milestone WP future', project:, type: milestone_type, due_date: 1.day.from_now)+ create_alertable(subject: 'Milestone WP future', type: milestone_type, due_date: time_zone.today + 1.day)end- shared_let(:wp_start_past) { create(:work_package, subject: 'WP start past', project:, start_date: 1.day.ago) }- shared_let(:wp_start_future) { create(:work_package, subject: 'WP start future', project:, start_date: 2.days.from_now) }+ shared_let(:wp_start_past) do+ create_alertable(subject: 'WP start past', start_date: time_zone.today - 1.day)+ end+ # notification will be created by job because `start_date: 7` in user notifications settings+ shared_let(:wp_start_future) do+ create_alertable(subject: 'WP start future', start_date: time_zone.today + 7.days)+ end- shared_let(:wp_due_past) { create(:work_package, subject: 'WP due past', project:, due_date: 3.days.ago) }- shared_let(:wp_due_future) { create(:work_package, subject: 'WP due future', project:, due_date: 3.days.from_now) }+ # notification will be created by job because `overdue: 1` in user notifications settings+ shared_let(:wp_due_past) do+ create_alertable(subject: 'WP due past', due_date: time_zone.today - 3.days)+ end+ # notification will be created by job because `due_date: 3` in user notifications settings+ shared_let(:wp_due_future) do+ create_alertable(subject: 'WP due future', due_date: time_zone.today + 3.days)+ end- shared_let(:wp_double_notification) { create(:work_package, subject: 'Alert + Mention', project:, due_date: 1.day.from_now) }+ shared_let(:wp_double_notification) do+ create_alertable(subject: 'Alert + Mention', due_date: time_zone.today + 1.day)+ end- shared_let(:wp_unset_date) { create(:work_package, subject: 'Unset date', project:, due_date: nil) }+ shared_let(:wp_unset_date) do+ create_alertable(subject: 'Unset date', due_date: nil)+ end- shared_let(:wp_due_today) { create(:work_package, subject: 'Due today', project:, due_date: Time.zone.today) }+ shared_let(:wp_due_today) do+ create_alertable(subject: 'Due today', due_date: time_zone.today)+ endshared_let(:wp_double_alert) do- create(:work_package, subject: 'Double alert', project:, start_date: 1.day.ago, due_date: 1.day.from_now)+ create_alertable(subject: 'Double alert', start_date: time_zone.today - 1.day, due_date: time_zone.today + 1.day)end- shared_let(:notification_milestone_past) do- create(:notification,- reason: :date_alert_due_date,- recipient: user,- resource: milestone_wp_past,- project:)+ # notification created by CreateDateAlertsNotificationsJob+ let(:notification_milestone_past) do+ Notification.find_by(reason: 'date_alert_due_date', resource: milestone_wp_past)endshared_let(:notification_milestone_future) do@@ -55,28 +89,19 @@ describe "Notification center date alerts", js: true, with_settings: { journal_aproject:)end- shared_let(:notification_wp_start_future) do- create(:notification,- reason: :date_alert_start_date,- recipient: user,- resource: wp_start_future,- project:)+ # notification created by CreateDateAlertsNotificationsJob+ let(:notification_wp_start_future) do+ Notification.find_by(reason: 'date_alert_start_date', resource: wp_start_future)end- shared_let(:notification_wp_due_past) do- create(:notification,- reason: :date_alert_due_date,- recipient: user,- resource: wp_due_past,- project:)+ # notification created by CreateDateAlertsNotificationsJob+ let(:notification_wp_due_past) do+ Notification.find_by(reason: 'date_alert_due_date', resource: wp_due_past)end- shared_let(:notification_wp_due_future) do- create(:notification,- reason: :date_alert_due_date,- recipient: user,- resource: wp_due_future,- project:)+ # notification created by CreateDateAlertsNotificationsJob+ let(:notification_wp_due_future) do+ Notification.find_by(reason: 'date_alert_due_date', resource: wp_due_future)endshared_let(:notification_wp_double_date_alert) do@@ -130,14 +155,30 @@ describe "Notification center date alerts", js: true, with_settings: { journal_alet(:center) { ::Pages::Notifications::Center.new }let(:side_menu) { ::Components::Notifications::Sidemenu.new }+ # Converts "hh:mm" into { hour: h, min: m }+ def time_hash(time)+ %i[hour min].zip(time.split(':', 2).map(&:to_i)).to_h+ end++ def timezone_time(time, timezone)+ timezone.now.change(time_hash(time))+ end++ def run_create_date_alerts_notifications_job+ create_date_alerts_service = Notifications::CreateDateAlertsNotificationsJob::Service.new([timezone_time('1:00', time_zone)])+ travel_to(timezone_time('1:04', time_zone))+ create_date_alerts_service.call+ end+before do+ run_create_date_alerts_notifications_joblogin_as uservisit notifications_center_pathendit 'shows the date alerts according to specification' docenter.expect_item(notification_wp_start_past, 'Start date was 1 day ago')- center.expect_item(notification_wp_start_future, 'Start date is in 2 days')+ center.expect_item(notification_wp_start_future, 'Start date is in 7 days')center.expect_item(notification_wp_due_past, 'Overdue since 3 days')center.expect_item(notification_wp_due_future, 'Finish date is in 3 days')@@ -175,3 +216,4 @@ describe "Notification center date alerts", js: true, with_settings: { journal_acenter.expect_no_item(notification_wp_double_mention)endend+# rubocop:enable RSpec/ScatteredLet