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

Add workday to having_reminder_mail_to_send

Oliver Güntherdf4eb3686a57
diff --git a/app/models/users/scopes/having_reminder_mail_to_send.rb b/app/models/users/scopes/having_reminder_mail_to_send.rb
index 9e63a7ed586..83f04719130 100644
--- a/app/models/users/scopes/having_reminder_mail_to_send.rb
+++ b/app/models/users/scopes/having_reminder_mail_to_send.rb
@@ -70,11 +70,19 @@ module Users::Scopes
# * 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.
+ times_sql = arel_table
+ .grouping(Arel::Nodes::ValuesList.new(local_times))
+ .as('t(time, zone, workday)')
<<~SQL.squish
- JOIN (
- SELECT * FROM #{arel_table.grouping(Arel::Nodes::ValuesList.new(local_times)).as('t(time, zone)').to_sql}
- ) AS local_times
+ JOIN (SELECT * FROM #{times_sql.to_sql}) AS local_times
ON COALESCE(user_preferences.settings->>'time_zone', 'UTC') = local_times.zone
+ AND (
+ user_preferences.settings->'workdays' @> to_jsonb(local_times.workday)
+ OR (
+ user_preferences.settings->'workdays' IS NULL
+ AND local_times.workday BETWEEN 1 AND 5
+ )
+ )
AND (
(
user_preferences.settings->'daily_reminders'->'times' IS NULL
@@ -99,16 +107,20 @@ module Users::Scopes
ActiveSupport::TimeZone
.all
.map do |z|
- times.map do |time|
- local_time = time.in_time_zone(z)
+ times.map do |time|
+ local_time = time.in_time_zone(z)
- # Since only full hours can be configured, we can disregard any local time that is not
- # a full hour.
- next if local_time.min != 0
+ # Get the iso weekday of the current time to check
+ # which users have it enabled as a workday
+ workday = Time.now.in_time_zone(z).to_date.cwday
- [local_time.strftime('%H:00:00+00:00'), z.name.gsub("'", "''")]
- end
+ # Since only full hours can be configured, we can disregard any local time that is not
+ # a full hour.
+ next if local_time.min != 0
+
+ [local_time.strftime('%H:00:00+00:00'), z.name.gsub("'", "''"), workday]
end
+ end
.flatten(1)
.compact
end
diff --git a/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb b/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb
index de146280e55..71402a204e1 100644
--- a/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb
+++ b/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb
@@ -42,6 +42,8 @@ describe User, '.having_reminder_mail_to_send', type: :model do
end
end
+ # Let the date be one where workdays are enabled by default
+ # to avoid specifying them explictly
let(:current_time) { "2021-09-30T08:10:59Z".to_datetime }
let(:scope_time) { "2021-09-30T08:00:00Z".to_datetime }
@@ -51,10 +53,12 @@ describe User, '.having_reminder_mail_to_send', type: :model do
firstname: 'Paris',
preferences: {
time_zone: "Paris",
+ workdays: paris_user_workdays,
daily_reminders: paris_user_daily_reminders
}
)
end
+ let(:paris_user_workdays) { [1, 2, 3, 4, 5] }
let(:paris_user_daily_reminders) do
{
enabled: true,
@@ -76,6 +80,16 @@ describe User, '.having_reminder_mail_to_send', type: :model do
end
end
+ context 'for a user whose local time is matching but the workday is disabled' do
+ # Configured date is a thursday = 4
+ let(:paris_user_workdays) { [1, 2] }
+
+ it 'is empty' do
+ expect(scope)
+ .to be_empty
+ end
+ end
+
context 'for a user whose local time is not matching the configured time' do
let(:current_time) { "2021-09-30T08:20:59Z".to_datetime }
let(:scope_time) { "2021-09-30T08:15:00Z".to_datetime }
@@ -86,6 +100,84 @@ describe User, '.having_reminder_mail_to_send', type: :model do
end
end
+ context 'for a user whose local time is on the previous workday' do
+ # 8:00 thursday UTC = 22:00 wednesday Hawaii
+ let(:hawaii_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Hawaii',
+ preferences: {
+ time_zone: "Hawaii",
+ workdays: hawaii_user_workdays,
+ daily_reminders: {
+ enabled: true,
+ times: [hitting_reminder_slot_for("Hawaii", current_time)]
+ }
+ }
+ )
+ end
+ let(:notifications) do
+ FactoryBot.create(:notification, recipient: hawaii_user, created_at: 5.minutes.ago)
+ end
+ let(:users) { [hawaii_user] }
+ let(:hawaii_user_workdays) { paris_user_workdays }
+
+ it 'contains the user' do
+ expect(scope)
+ .to match_array([hawaii_user])
+ end
+
+ context 'when the user disables Wednesday as a workday' do
+ let(:hawaii_user_workdays) { [1, 2, 4, 5, 6, 7] }
+
+ it 'is empty' do
+ expect(scope)
+ .to be_empty
+ end
+ end
+ end
+
+
+ context 'for a user whose local time is on the next workday' do
+ # 12:00 thursday UTC = 03:00 friday @ Samoa
+ let(:current_time) { "2021-09-30T12:05:59Z".to_datetime }
+ let(:scope_time) { "2021-09-30T12:00:00Z".to_datetime }
+
+ let(:samoa_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Samoa',
+ preferences: {
+ time_zone: "Samoa",
+ workdays: samoa_user_workdays,
+ daily_reminders: {
+ enabled: true,
+ times: [hitting_reminder_slot_for("Samoa", current_time)]
+ }
+ }
+ )
+ end
+ let(:notifications) do
+ FactoryBot.create(:notification, recipient: samoa_user, created_at: 5.minutes.ago)
+ end
+ let(:users) { [samoa_user] }
+ let(:samoa_user_workdays) { paris_user_workdays }
+
+ it 'contains the user' do
+ expect(scope)
+ .to match_array([samoa_user])
+ end
+
+ context 'when the user disables Wednesday as a workday' do
+ let(:samoa_user_workdays) { [1, 2, 3, 4, 6, 7] }
+
+ it 'is empty' do
+ expect(scope)
+ .to be_empty
+ end
+ end
+ end
+
context 'for a user whose local time is matching the configured time (in a non CET time zone)' do
let(:moscow_user) do
FactoryBot.create(