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

use daily_reminder settings to find reminder recipients

ulferts9114daf0d745
diff --git a/app/models/users/scopes/having_reminder_mail_to_send_now.rb b/app/models/users/scopes/having_reminder_mail_to_send_now.rb
index 0e5bc7e483b..f3866b9103f 100644
--- a/app/models/users/scopes/having_reminder_mail_to_send_now.rb
+++ b/app/models/users/scopes/having_reminder_mail_to_send_now.rb
@@ -37,8 +37,8 @@ module Users::Scopes
# * That user has an unread notification
# * The user hasn't been informed about the unread notification before
# * The user has configured reminder mails to be sent now.
- # This assumes that users only have full hours specified for the time they desire
- # to receive a reminder mail.
+ # This assumes that users only have full hours specified for the times they desire
+ # to receive a reminder mail at.
def having_reminder_mail_to_send_now
# Left outer join as not all user instances have preferences associated
# but we still want to select them.
@@ -56,10 +56,25 @@ module Users::Scopes
end
def local_time_join
+ # Joins the times local to the user preferences and then checks whether:
+ # * reminders are enabled
+ # * any of the configured reminder time is the local time
+ # If no time zone is present, utc is assumed.
+ # If no reminder settings are present, sending a reminder at 08:00 local time is assumed.
<<~SQL.squish
JOIN (#{local_time_table}) AS local_times
ON COALESCE(user_preferences.settings->>'time_zone', 'UTC') = local_times.zone
- AND local_times.time = '#{Setting.notification_email_digest_time}:00+00:00'
+ AND (
+ (
+ user_preferences.settings->'daily_reminders'->'times' IS NULL
+ AND local_times.time = '08:00:00+00:00'
+ )
+ OR
+ (
+ (user_preferences.settings->'daily_reminders'->'enabled')::boolean
+ AND user_preferences.settings->'daily_reminders'->'times' ? local_times.time
+ )
+ )
SQL
end
diff --git a/spec/features/notifications/reminder_mail_spec.rb b/spec/features/notifications/reminder_mail_spec.rb
index 0c136cbb5c3..037e556e0a2 100644
--- a/spec/features/notifications/reminder_mail_spec.rb
+++ b/spec/features/notifications/reminder_mail_spec.rb
@@ -71,34 +71,40 @@ describe "Reminder email", type: :feature, js: true do
let(:work_package) { FactoryBot.create(:work_package, project: project) }
let(:watched_work_package) { FactoryBot.create(:work_package, project: project, watcher_users: [current_user]) }
let(:involved_work_package) { FactoryBot.create(:work_package, project: project, assigned_to: current_user) }
+ let(:current_utc_time) { ActiveSupport::TimeZone['Hawaii'].parse("08:43").utc }
current_user do
- FactoryBot.create :user,
- notification_settings: [
- FactoryBot.build(:mail_digest_notification_setting,
- involved: true,
- watched: true,
- mentioned: true,
- work_package_commented: true,
- work_package_created: true,
- work_package_processed: true,
- work_package_prioritized: true,
- work_package_scheduled: true,
- all: false)
- ]
+ FactoryBot.create(
+ :user,
+ preferences: {
+ time_zone: "Hawaii",
+ daily_reminders: {
+ enabled: true,
+ times: [hitting_reminder_slot_for("Hawaii", current_utc_time)]
+ }
+ },
+ notification_settings: [
+ FactoryBot.build(:mail_digest_notification_setting,
+ involved: true,
+ watched: true,
+ mentioned: true,
+ work_package_commented: true,
+ work_package_created: true,
+ work_package_processed: true,
+ work_package_prioritized: true,
+ work_package_scheduled: true,
+ all: false)
+ ]
+ )
end
before do
- allow(Setting).to receive(:notification_email_digest_time)
- .and_return(hitting_reminder_slot_for(current_user))
+ allow(Time).to receive(:current).and_return(current_utc_time)
watched_work_package
work_package
involved_work_package
- allow(CustomStyle.current)
- .to receive(:logo).and_return(nil)
-
ActiveJob::Base.queue_adapter.enqueued_jobs.clear
end
diff --git a/spec/models/users/scopes/having_reminder_mail_to_send_now_spec.rb b/spec/models/users/scopes/having_reminder_mail_to_send_now_spec.rb
index c61100ca9eb..cfd8c28b6d0 100644
--- a/spec/models/users/scopes/having_reminder_mail_to_send_now_spec.rb
+++ b/spec/models/users/scopes/having_reminder_mail_to_send_now_spec.rb
@@ -35,29 +35,36 @@ describe User, '.with_reminder_mail_to_send_now', type: :job do
described_class.having_reminder_mail_to_send_now
end
- # As it is hard to mock Postgres's "now()" method, in the specs here we need to adopt the slot time
+ # As it is hard to mock PostgreSQL's "now()" method, in the specs here we need to adopt the slot time
# relative to the local time of the user that we want to hit.
let(:current_utc_time) { Time.current.getutc }
- let(:slot_time) { hitting_reminder_slot_for(hitting_user, current_utc_time) } # ie. "08:00", "08:30"
- let(:hitting_user) { paris_user }
- let(:paris_user) { FactoryBot.create(:user, preferences: { time_zone: "Paris" }) } # time_zone in winter is +01:00
- let(:moscow_user) { FactoryBot.create(:user, preferences: { time_zone: "Moscow" }) } # time_zone all year is +03:00
- let(:greenland_user) { FactoryBot.create(:user, preferences: { time_zone: "Greenland" }) } # time_zone in winter is -03:00
- let(:no_zone_user) { FactoryBot.create(:user) } # time_zone is nil
- let(:notifications) { FactoryBot.create(:notification, recipient: hitting_user) }
+ let(:paris_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Paris',
+ preferences: {
+ time_zone: "Paris",
+ daily_reminders: paris_user_daily_reminders
+ }
+ )
+ end
+ let(:paris_user_daily_reminders) do
+ {
+ enabled: true,
+ times: [hitting_reminder_slot_for("Paris", current_utc_time)]
+ }
+ end
+ let(:notifications) { FactoryBot.create(:notification, recipient: paris_user, created_at: 5.minutes.ago) }
+ let(:users) { [paris_user] }
before do
- allow(Setting).to receive(:notification_email_digest_time).and_return(slot_time)
allow(Time).to receive(:current).and_return(current_utc_time)
notifications
+ users
end
context 'for a user whose local time is matching the configured time' do
- let(:notifications) do
- FactoryBot.create(:notification, recipient: paris_user, created_at: 5.minutes.ago)
- end
-
it 'contains the user' do
expect(scope)
.to match_array([paris_user])
@@ -65,10 +72,23 @@ describe User, '.with_reminder_mail_to_send_now', type: :job do
end
context 'for a user whose local time is matching the configured time (in a non CET time zone)' do
- let(:slot_time) { hitting_reminder_slot_for(moscow_user, current_utc_time) }
+ let(:moscow_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Moscow',
+ preferences: {
+ time_zone: "Moscow",
+ daily_reminders: {
+ enabled: true,
+ times: [hitting_reminder_slot_for("Moscow", current_utc_time)]
+ }
+ }
+ )
+ end
let(:notifications) do
FactoryBot.create(:notification, recipient: moscow_user, created_at: 5.minutes.ago)
end
+ let(:users) { [moscow_user] }
it 'contains the user' do
expect(scope)
@@ -76,10 +96,27 @@ describe User, '.with_reminder_mail_to_send_now', type: :job do
end
end
+ context 'for a user whose local time is matching one of the configured times' do
+ let(:paris_user_daily_reminders) do
+ {
+ enabled: true,
+ times: [
+ hitting_reminder_slot_for("Paris", current_utc_time - 3.hours),
+ hitting_reminder_slot_for("Paris", current_utc_time),
+ hitting_reminder_slot_for("Paris", current_utc_time + 3.hours)
+ ]
+ }
+ end
+
+ it 'contains the user' do
+ expect(scope)
+ .to match_array([paris_user])
+ end
+ end
+
context 'for a user whose local time is matching the configured time but without a notification' do
let(:notifications) do
- # There is a notification for a different user
- FactoryBot.create(:notification, recipient: moscow_user, created_at: 5.minutes.ago)
+ nil
end
it 'is empty' do
@@ -88,6 +125,56 @@ describe User, '.with_reminder_mail_to_send_now', type: :job do
end
end
+ context 'for a user whose local time is matching the configured time but with the reminder being deactivated' do
+ let(:paris_user_daily_reminders) do
+ {
+ enabled: false,
+ times: [hitting_reminder_slot_for("Paris", current_utc_time)]
+ }
+ end
+
+ it 'is empty' do
+ expect(scope)
+ .to be_empty
+ end
+ end
+
+ context 'for a user whose local time is matching the configured time but without a daily_reminders setting at 8:00' do
+ let(:paris_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Paris',
+ preferences: {
+ time_zone: "Paris"
+ }
+ )
+ end
+ let(:current_utc_time) { ActiveSupport::TimeZone['Paris'].parse("08:00").utc }
+
+ it 'contains the user' do
+ expect(scope)
+ .to match_array([paris_user])
+ end
+ end
+
+ context 'for a user whose local time is matching the configured time but without a daily_reminders setting at 10:00' do
+ let(:paris_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Paris',
+ preferences: {
+ time_zone: "Paris"
+ }
+ )
+ end
+ let(:current_utc_time) { ActiveSupport::TimeZone['Paris'].parse("10:00").utc }
+
+ it 'is empty' do
+ expect(scope)
+ .to be_empty
+ end
+ end
+
context 'for a user whose local time is matching the configured time but with an already read notification (IAN)' do
let(:notifications) do
FactoryBot.create(:notification, recipient: paris_user, created_at: 5.minutes.ago, read_ian: true)
@@ -126,36 +213,82 @@ describe User, '.with_reminder_mail_to_send_now', type: :job do
end
context 'for a user whose local time is before the configured time' do
- let(:notifications) do
- FactoryBot.create(:notification, recipient: moscow_user, created_at: 5.minutes.ago)
+ let(:paris_user_daily_reminders) do
+ {
+ enabled: true,
+ times: [hitting_reminder_slot_for("Paris", current_utc_time + 1.hour)]
+ }
end
- it 'contains the user' do
+ it 'is empty' do
expect(scope)
.to be_empty
end
end
context 'for a user whose local time is after the configured time' do
- let(:notifications) do
- FactoryBot.create(:notification, recipient: greenland_user, created_at: 5.minutes.ago)
+ let(:paris_user_daily_reminders) do
+ {
+ enabled: true,
+ times: [hitting_reminder_slot_for("Paris", current_utc_time - 1.hour)]
+ }
end
- it 'contains the user' do
+ it 'is empty' do
expect(scope)
.to be_empty
end
end
context 'for a user without a time zone' do
- let(:slot_time) { hitting_reminder_slot_for(no_zone_user, current_utc_time) }
- let(:notifications) do
- FactoryBot.create(:notification, recipient: no_zone_user, created_at: 5.minutes.ago)
+ let(:paris_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Paris',
+ preferences: {
+ daily_reminders: {
+ enabled: true,
+ times: [hitting_reminder_slot_for("UTC", current_utc_time)]
+ }
+ }
+ )
end
it 'is including the user as UTC is assumed' do
expect(scope)
- .to match_array([no_zone_user])
+ .to match_array([paris_user])
+ end
+ end
+
+ context 'for a user without a time zone and daily_reminders at 08:00' do
+ let(:paris_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Paris',
+ preferences: {}
+ )
+ end
+ let(:current_utc_time) { ActiveSupport::TimeZone['UTC'].parse("08:00").utc }
+
+ it 'is including the user as UTC at 08:00 is assumed' do
+ expect(scope)
+ .to match_array([paris_user])
+ end
+ end
+
+ context 'for a user without a time zone and daily_reminders at 10:00' do
+ let(:paris_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Paris',
+ preferences: {}
+ )
+ end
+ let(:current_utc_time) { ActiveSupport::TimeZone['UTC'].parse("10:00").utc }
+
+ it 'is empty as UTC at 08:00 is assumed' do
+ expect(scope)
+ .to be_empty
end
end
end
diff --git a/spec/support/schedule_reminder_mails.rb b/spec/support/schedule_reminder_mails.rb
index ed4c0ee05fd..e30e8ab4d77 100644
--- a/spec/support/schedule_reminder_mails.rb
+++ b/spec/support/schedule_reminder_mails.rb
@@ -28,6 +28,6 @@
##
# Calculates a slot in the user's local time that hits for scheduling reminder mail jobs
-def hitting_reminder_slot_for(user, current_utc_time = Time.current.getutc)
- current_utc_time.in_time_zone(user.time_zone).strftime('%H:00')
+def hitting_reminder_slot_for(time_zone, current_utc_time = Time.current.getutc)
+ current_utc_time.in_time_zone(ActiveSupport::TimeZone[time_zone]).strftime('%H:00:00+00:00')
end