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
[#43678] Support date alerts in email digest
Dmitrii8b6519de774c
diff --git a/app/helpers/mail_digest_helper.rb b/app/helpers/mail_digest_helper.rbindex 4152f043045..71709435d93 100644--- a/app/helpers/mail_digest_helper.rb+++ b/app/helpers/mail_digest_helper.rb@@ -42,6 +42,55 @@ module MailDigestHelpersummaryend+ def date_alerts_text(notification)+ work_package = notification.resource+ date_value = date_value(notification, work_package)++ alert_text = if date_value+ date_is_past = date_value.before?(Time.zone.today)+ is_overdue = date_is_past && (notification.reason == "date_alert_due_date" || work_package.milestone?)+ days_diff = (date_value - Time.zone.today).to_i.abs++ build_alert_text(days_diff, is_overdue, date_is_past)+ else+ I18n.t('js.notifications.date_alerts.property_is_deleted')+ end++ "#{property_text(notification, is_overdue, days_diff)} #{alert_text}"+ end++ def date_value(notification, work_package)+ notification.reason == "date_alert_start_date" ? work_package.start_date : work_package.due_date+ end++ def property_text(notification, is_overdue, days_diff)+ if is_overdue && days_diff > 0+ I18n.t('js.notifications.date_alerts.overdue')+ else+ property_text_helper(notification)+ end+ end++ def build_alert_text(days_diff, is_overdue, date_is_past)+ days_text = I18n.t('js.units.day', count: days_diff)++ return 'is today' if days_diff == 0+ return "since #{days_text}" if is_overdue+ return "was #{days_text} ago" if date_is_past++ "is in #{days_text}"+ end++ def property_text_helper(notification)+ return I18n.t('js.notifications.date_alerts.milestone_date') if notification.resource.milestone?++ if notification.reason == "date_alert_start_date"+ I18n.t('js.work_packages.properties.startDate')+ else+ I18n.t('js.work_packages.properties.dueDate')+ end+ end+def digest_notification_timestamp_text(notification, html: true)journal = notification.journaluser = html ? link_to_user(journal.user, only_path: false) : journal.user.namediff --git a/app/models/notification.rb b/app/models/notification.rbindex 3ec7e172c46..d0ddb3f1109 100644--- a/app/models/notification.rb+++ b/app/models/notification.rb@@ -29,4 +29,8 @@ class Notification < ApplicationRecord:mail_alert_unsent,:recipient,:visible++ def date_alert?+ reason.in?(["date_alert_start_date", "date_alert_due_date"]) || resource&.milestone?+ endenddiff --git a/app/views/digest_mailer/work_packages.html.erb b/app/views/digest_mailer/work_packages.html.erbindex c20d8d4b22f..4318594c6b2 100644--- a/app/views/digest_mailer/work_packages.html.erb+++ b/app/views/digest_mailer/work_packages.html.erb@@ -17,7 +17,7 @@salutation: I18n.t(:'mail.salutation', user: @user.firstname)} %>- <% @aggregated_notifications.first(DigestMailer::MAX_SHOWN_WORK_PACKAGES).each do | work_package, notifications_by_work_package| %>+ <% @aggregated_notifications.first(DigestMailer::MAX_SHOWN_WORK_PACKAGES).each do |work_package, notifications_by_work_package| %><%= render layout: 'mailer/notification_row',locals: {work_package: work_package,@@ -29,7 +29,11 @@<table <%= placeholder_table_styles %>><tr><td style="color: #878787; line-height: 24px; font-size: 14px; white-space: normal;">- <%= digest_notification_timestamp_text(notifications_by_work_package.first) %>+ <% if notifications_by_work_package.first.date_alert? %>+ <%= date_alerts_text(notifications_by_work_package.first) %>+ <% else %>+ <%= digest_notification_timestamp_text(notifications_by_work_package.first) %>+ <% end %><%= digest_additional_author_text(notifications_by_work_package) %></td></tr>diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 9c0501773bb..611ede0ccda 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -2109,6 +2109,8 @@ en:mentioned: 'Mentioned'subscribed: 'all'prefix: 'Received because of the notification setting: %{reason}'+ date_alert_start_date: 'Date alert'+ date_alert_due_date: 'Date alert'see_all: 'See all'updated_at: 'Updated at %{timestamp} by %{user}'diff --git a/spec/mailers/digest_mailer_spec.rb b/spec/mailers/digest_mailer_spec.rbindex f05b0097a9e..7d9fb151a89 100644--- a/spec/mailers/digest_mailer_spec.rb+++ b/spec/mailers/digest_mailer_spec.rb@@ -28,7 +28,7 @@require 'spec_helper'-describe DigestMailer, type: :mailer do+describe DigestMailer doinclude OpenProject::ObjectLinkinginclude ActionView::Helpers::UrlHelperinclude OpenProject::StaticRouting::UrlHelpers@@ -130,5 +130,181 @@ describe DigestMailer, type: :mailer do.to eql({})endend++ describe "#date_alerts_text" do+ let!(:color) { create(:color) }+ let!(:project1) { create(:project) }+ let!(:recipient) { create(:user) }+ let!(:work_package_journal) do+ create(:work_package_journal,+ journable_id: work_package.id,+ version: 2,+ data: build(:journal_work_package_journal,+ description: 'text'))+ end+ let(:notifications) { [notification] }++ context 'when notification_wp_start_past' do+ let(:work_package) do+ create(:work_package, subject: 'WP start past', project: project1, start_date: 1.day.ago, type: Type.first)+ end+ let(:notification) do+ create(:notification,+ reason: :date_alert_start_date,+ recipient:,+ resource: work_package,+ project: project1,+ journal: work_package_journal)+ end++ it 'matches generated text' do+ expect(mail_body).to have_text('Start date was 1 day ago')+ end+ end++ context 'when notification_wp_start_future' do+ let(:work_package) do+ create(:work_package, subject: 'WP start future', project: project1, start_date: 2.days.from_now, type: Type.first)+ end+ let(:notification) do+ create(:notification,+ reason: :date_alert_start_date,+ recipient:,+ resource: work_package,+ project: project1,+ journal: work_package_journal)+ end++ it 'matches generated text' do+ expect(mail_body).to have_text('Start date is in 2 days')+ end+ end++ context 'when notification_wp_due_past' do+ let(:work_package) do+ create(:work_package, subject: 'WP due past', project: project1, due_date: 3.days.ago, type: Type.first)+ end+ let(:notification) do+ create(:notification,+ reason: :date_alert_due_date,+ recipient:,+ resource: work_package,+ project: project1,+ journal: work_package_journal)+ end++ it 'matches generated text' do+ expect(mail_body).to have_text('Overdue since 3 days')+ end+ end++ context 'when notification_wp_due_future' do+ let(:work_package) do+ create(:work_package, subject: 'WP due future', project: project1, due_date: 3.days.from_now, type: Type.first)+ end+ let(:notification) do+ create(:notification,+ reason: :date_alert_due_date,+ recipient:,+ resource: work_package,+ project: project1,+ journal: work_package_journal)+ end++ it 'matches generated text' do+ expect(mail_body).to have_text('Finish date is in 3 days')+ end+ end++ context 'when notification_milestone_past' do+ let(:milestone_type) { create(:type_milestone) }+ let(:work_package) do+ create(:work_package, subject: 'Milestone WP past', project: project1, type: milestone_type, due_date: 2.days.ago)+ end+ let(:notification) do+ create(:notification,+ reason: :date_alert_due_date,+ recipient:,+ resource: work_package,+ project: project1,+ journal: work_package_journal)+ end++ it 'matches generated text' do+ expect(mail_body).to have_text('Overdue since 2 days')+ end+ end++ context 'when notification_milestone_future' do+ let(:milestone_type) { create(:type_milestone) }+ let(:work_package) do+ create(:work_package, subject: 'Milestone WP future', project: project1, type: milestone_type, due_date: 1.day.from_now)+ end+ let(:notification) do+ create(:notification,+ reason: :date_alert_due_date,+ recipient:,+ resource: work_package,+ project: project1,+ journal: work_package_journal)+ end++ it 'matches generated text' do+ expect(mail_body).to have_text('Milestone date is in 1 day')+ end+ end++ context 'when notification_wp_unset_date' do+ let(:work_package) { create(:work_package, subject: 'Unset date', project: project1, due_date: nil, type: Type.first) }+ let(:notification) do+ create(:notification,+ reason: :date_alert_due_date,+ recipient:,+ resource: work_package,+ project: project1,+ journal: work_package_journal)+ end++ it 'matches generated text' do+ expect(mail_body).to have_text('Finish date is deleted')+ end+ end++ context 'when notification_wp_due_today' do+ let(:work_package) do+ create(:work_package, subject: 'Due today', project: project1, due_date: Time.zone.today, type: Type.first)+ end+ let(:notification) do+ create(:notification,+ reason: :date_alert_due_date,+ recipient:,+ resource: work_package,+ project: project1,+ journal: work_package_journal)+ end++ it 'matches generated text' do+ expect(mail_body).to have_text('Finish date is today')+ end+ end++ context 'when notification_wp_double_date_alert' do+ let(:work_package) do+ create(:work_package, subject: 'Alert + Mention', project: project1, due_date: 1.day.from_now, type: Type.first)+ end+ let(:notification) do+ create(:notification,+ reason: :date_alert_due_date,+ recipient:,+ resource: work_package,+ project: project1,+ journal: work_package_journal)+ end++ it 'matches generated text' do+ expect(mail_body).to have_text('Finish date is in 1 day')+ end+ end+ endendend