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

Mark previous date alert notifications as read

Christophe Bliard898d622a62de
diff --git a/app/workers/notifications/create_date_alerts_notifications_job.rb b/app/workers/notifications/create_date_alerts_notifications_job.rb
index 651241d97d9..f5d927a4480 100644
--- a/app/workers/notifications/create_date_alerts_notifications_job.rb
+++ b/app/workers/notifications/create_date_alerts_notifications_job.rb
@@ -45,14 +45,25 @@ module Notifications
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)
+ 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(
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 f6da7d562cb..451ae33d0eb 100644
--- a/spec/workers/notifications/create_date_alerts_notifications_job_spec.rb
+++ b/spec/workers/notifications/create_date_alerts_notifications_job_spec.rb
@@ -274,6 +274,72 @@ describe Notifications::CreateDateAlertsNotificationsJob, type: :job do
end
end
+ context 'with existing date alerts' do
+ it 'marks them as read when new ones are created' do
+ work_package = alertable_work_package(assigned_to: user_paris,
+ start_date: in_1_day,
+ due_date: in_1_day)
+ existing_start_notification = create(:notification,
+ resource: work_package,
+ recipient: user_paris,
+ reason: :date_alert_start_date)
+ existing_due_notification = create(:notification,
+ resource: work_package,
+ recipient: user_paris,
+ reason: :date_alert_due_date)
+
+ 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(existing_start_notification.reload).to have_attributes(read_ian: true)
+ expect(existing_due_notification.reload).to have_attributes(read_ian: true)
+ unread_date_alert_notifications = Notification.where(recipient: user_paris,
+ read_ian: false,
+ resource: work_package)
+ expect(unread_date_alert_notifications.pluck(:reason))
+ .to contain_exactly("date_alert_start_date", "date_alert_due_date")
+ end
+ end
+
+ # rubocop:disable RSpec/ExampleLength
+ it 'does not mark them as read when if no new notifications are created' do
+ work_package_start = alertable_work_package(assigned_to: user_paris,
+ start_date: in_1_day,
+ due_date: nil)
+ work_package_due = alertable_work_package(assigned_to: user_paris,
+ start_date: nil,
+ due_date: in_1_day)
+ existing_wp_start_start_notif = create(:notification,
+ reason: :date_alert_start_date,
+ recipient: user_paris,
+ resource: work_package_start)
+ existing_wp_start_due_notif = create(:notification,
+ reason: :date_alert_due_date,
+ recipient: user_paris,
+ resource: work_package_start)
+ existing_wp_due_start_notif = create(:notification,
+ reason: :date_alert_start_date,
+ recipient: user_paris,
+ resource: work_package_due)
+ existing_wp_due_due_notif = create(:notification,
+ reason: :date_alert_due_date,
+ recipient: user_paris,
+ resource: work_package_due)
+
+ 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(existing_wp_start_start_notif.reload).to have_attributes(read_ian: true)
+ expect(existing_wp_start_due_notif.reload).to have_attributes(read_ian: false)
+ expect(existing_wp_due_start_notif.reload).to have_attributes(read_ian: false)
+ expect(existing_wp_due_due_notif.reload).to have_attributes(read_ian: true)
+ end
+ end
+ # rubocop:enable RSpec/ExampleLength
+ 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