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

Trigger date alerts based on durations in notification settings

Christophe Bliard0204a762093c
diff --git a/app/workers/notifications/create_date_alerts_notifications_job.rb b/app/workers/notifications/create_date_alerts_notifications_job.rb
index 141959af036..c0726f43a58 100644
--- a/app/workers/notifications/create_date_alerts_notifications_job.rb
+++ b/app/workers/notifications/create_date_alerts_notifications_job.rb
@@ -39,13 +39,17 @@ module Notifications
Time.use_zone(time_zones.first) do
User.with_time_zone(time_zones).find_each do |user|
send_start_date_alert_notifications(user)
+ send_due_date_alert_notifications(user)
end
end
end
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 + 1.day)
+ .where(start_date: Date.current + start_date_alert.days)
.each do |work_package|
create_service = Notifications::CreateService.new(user:)
create_service.call(
@@ -58,6 +62,24 @@ module Notifications
end
end
+ 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
+ end
+
def time_zones_covering_1am_local_time
UserPreferences::UpdateContract
.assignable_time_zones
diff --git a/spec/workers/notifications/create_date_alerts_notifications_job_spec.rb b/spec/workers/notifications/create_date_alerts_notifications_job_spec.rb
index 9f8aea70195..567037d5155 100644
--- a/spec/workers/notifications/create_date_alerts_notifications_job_spec.rb
+++ b/spec/workers/notifications/create_date_alerts_notifications_job_spec.rb
@@ -46,18 +46,29 @@ describe Notifications::CreateDateAlertsNotificationsJob, type: :job do
shared_let(:today) { Date.current }
shared_let(:in_1_day) { today + 1.day }
+ shared_let(:in_3_days) { today + 3.days }
+ # Paris and Berlin are both UTC+01:00 (CET) or UTC+02:00 (CEST)
shared_let(:user_paris) do
create(
:user,
- firstname: 'Europe/Paris',
+ firstname: 'Paris',
preferences: { time_zone: 'Europe/Paris' }
)
end
+ shared_let(:user_berlin) do
+ create(
+ :user,
+ firstname: 'Berlin',
+ preferences: { time_zone: 'Europe/Berlin' }
+ )
+ end
+
+ # Kathmandu is UTC+05:45 (no DST)
shared_let(:user_kathmandu) do
create(
:user,
- firstname: 'Asia/Kathmandu',
+ firstname: 'Kathmandu',
preferences: { time_zone: 'Asia/Kathmandu' }
)
end
@@ -106,6 +117,40 @@ describe Notifications::CreateDateAlertsNotificationsJob, type: :job do
end
end
+ define :have_a_due_date_alert_notification_for do |work_package|
+ match do |user|
+ Notification
+ .reason_date_alert_due_date
+ .recipient(user)
+ .exists?(resource: work_package)
+ end
+
+ failure_message do |user|
+ "expected user #{inspect_keys(user)} " \
+ "to have a due date alert notification for work package " \
+ "#{inspect_keys(work_package)}"
+ end
+
+ failure_message_when_negated do |user|
+ "expected user #{inspect_keys(user)} " \
+ "to NOT have a due date alert notification for work package " \
+ "#{inspect_keys(work_package)}"
+ end
+
+ def inspect_keys(object)
+ keys =
+ case object
+ when User then %i[id firstname]
+ when WorkPackage then %i[id start_date due_date assigned_to_id responsible_id]
+ end
+ formatted_pairs = object
+ .slice(*keys)
+ .map { |k, v| "#{k}: #{v.is_a?(Date) ? v.to_s : v.inspect}" }
+ .join(', ')
+ "#<#{formatted_pairs}>"
+ end
+ end
+
def set_scheduled_time(run_at)
scheduled_job.update_column(:run_at, run_at)
end
@@ -172,6 +217,30 @@ describe Notifications::CreateDateAlertsNotificationsJob, type: :job do
end
end
+ it 'creates start and finish date alert notifications based on user notification settings' do
+ user_paris.notification_settings.first.update(
+ start_date: 1,
+ due_date: nil
+ )
+ user_berlin.notification_settings.first.update(
+ start_date: nil,
+ due_date: 3
+ )
+ work_package = alertable_work_package(assigned_to: user_paris,
+ responsible: user_berlin,
+ start_date: in_1_day,
+ due_date: in_3_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).to have_a_start_date_alert_notification_for(work_package)
+ expect(user_paris).not_to have_a_due_date_alert_notification_for(work_package)
+ expect(user_berlin).not_to have_a_start_date_alert_notification_for(work_package)
+ expect(user_berlin).to have_a_due_date_alert_notification_for(work_package)
+ end
+ end
+
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