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
Pause reminders functionality
Oliver Günther68d6b446359e
diff --git a/app/contracts/user_preferences/base_contract.rb b/app/contracts/user_preferences/base_contract.rbindex d0d9ae4d820..7df69b4276d 100644--- a/app/contracts/user_preferences/base_contract.rb+++ b/app/contracts/user_preferences/base_contract.rb@@ -51,6 +51,9 @@ module UserPreferencesvalidate :no_duplicate_workdays,if: -> { model.workdays.present? }+ validate :valid_pause_days,+ if: -> { model.pause_reminders.present? && model.pause_reminders[:enabled] }+class << self### Returns time zones supported by OpenProject. Those include only the subset of all the@@ -118,5 +121,19 @@ module UserPreferenceserrors.add :workdays, :no_duplicatesendend++ def valid_pause_days+ first = model.pause_reminders[:first_day]+ last = model.pause_reminders[:last_day]++ unless first && last+ errors.add :pause_reminders, :presence+ return+ end++ unless last.to_date >= first.to_date+ errors.add :pause_reminders, :invalid_range+ end+ endendenddiff --git a/app/models/user_preference.rb b/app/models/user_preference.rbindex c46442a6f30..335501d1ddf 100644--- a/app/models/user_preference.rb+++ b/app/models/user_preference.rb@@ -126,6 +126,10 @@ class UserPreference < ApplicationRecordsuper.presence || { mentioned: false }.with_indifferent_accessend+ def pause_reminders+ super.presence || { enabled: false }.with_indifferent_access+ end+def workdayssuper.presence || [1, 2, 3, 4, 5]enddiff --git a/app/models/users/scopes/having_reminder_mail_to_send.rb b/app/models/users/scopes/having_reminder_mail_to_send.rbindex b57bbc4ec74..a724216574a 100644--- a/app/models/users/scopes/having_reminder_mail_to_send.rb+++ b/app/models/users/scopes/having_reminder_mail_to_send.rb@@ -74,7 +74,7 @@ module Users::Scopes# If no workdays are specified, 1 - 5 is assumed which is represents Monday to Friday.times_sql = arel_table.grouping(Arel::Nodes::ValuesList.new(local_times))- .as('t(time, zone, workday)')+ .as('t(today_utc, hours, zone, workday)')default_timezone = Setting.user_default_timezone.present? ? "'#{Setting.user_default_timezone}'" : 'NULL'@@ -91,12 +91,27 @@ module Users::ScopesAND ((user_preferences.settings->'daily_reminders'->'times' IS NULL- AND local_times.time = '08:00:00+00:00'+ AND local_times.hours = '08:00:00+00:00')OR((user_preferences.settings->'daily_reminders'->'enabled')::boolean- AND user_preferences.settings->'daily_reminders'->'times' ? local_times.time+ AND user_preferences.settings->'daily_reminders'->'times' ? local_times.hours+ )+ )+ AND (+ (+ user_preferences.settings->'pause_reminders' IS NULL+ OR (user_preferences.settings->'pause_reminders'->'enabled')::boolean = false+ )+ OR+ (+ (user_preferences.settings->'pause_reminders'->'enabled')::boolean+ AND (+ local_times.today_utc::date+ NOT BETWEEN (user_preferences.settings->'pause_reminders'->>'first_day')::date+ AND (user_preferences.settings->'pause_reminders'->>'last_day')::date+ )))SQL@@ -112,20 +127,29 @@ module Users::ScopesUserPreferences::UpdateContract.assignable_time_zones.map do |z|- times.map do |time|- local_time = time.in_time_zone(z)-- # Get the iso weekday of the current time to check- # which users have it enabled as a workday- workday = local_time.to_date.cwday-- # 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.tzinfo.canonical_zone.name, workday]- end+ times.map do |time|+ local_time = time.in_time_zone(z)++ # Get the iso weekday of the current time to check+ # which users have it enabled as a workday+ workday = local_time.to_date.cwday++ # Get the corresponding "UTC" date by conversion+ # to compare them with pause_reminder dates which are derived from UTC+ utc_date = local_time.utc.to_date++ # 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++ [+ utc_date,+ local_time.strftime('%H:00:00+00:00'),+ z.tzinfo.canonical_zone.name,+ workday+ ]end+ end.flatten(1).compactenddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex ab72615b58a..695ad233efa 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -762,6 +762,8 @@ en:day_limit: "is too high as a maximum of 24 hours can be logged per date."user_preference:attributes:+ pause_reminders:+ invalid_range: "can only be a valid date range."daily_reminders:full_hour: "can only be configured to be delivered at a full hour."wiki_page:diff --git a/config/locales/js-en.yml b/config/locales/js-en.ymlindex 61ebb4332fd..1d64f9b3ea4 100644--- a/config/locales/js-en.yml+++ b/config/locales/js-en.yml@@ -675,6 +675,8 @@ en:membership_added: 'Membership added'membership_updated: 'Membership updated'title: 'Email reminders'+ pause:+ label: 'Temporarily pause daily email reminders'text_are_you_sure: "Are you sure?"@@ -976,6 +978,7 @@ en:label_content: "Click here to skip over the menu and go to the content"placeholders:default: "-"+ date: "Select date"formattable: "%{name}: Click to edit..."query:column_names: "Columns"diff --git a/config/schemas/user_preferences.schema.json b/config/schemas/user_preferences.schema.jsonindex b6ed2d8f503..ee1931c0e6f 100644--- a/config/schemas/user_preferences.schema.json+++ b/config/schemas/user_preferences.schema.json@@ -35,6 +35,9 @@},"immediate_reminders": {"$ref": "#/definitions/ImmediateReminders"+ },+ "pause_reminders": {+ "$ref": "#/definitions/PauseReminders"}},"title": "UserPreferences"@@ -72,6 +75,27 @@"mentioned"],"title": "ImmediateReminders"+ },+ "PauseReminders": {+ "type": "object",+ "additionalProperties": false,+ "properties": {+ "enabled": {+ "type": "boolean"+ },+ "first_day": {+ "type": "string",+ "format": "date"+ },+ "last_day": {+ "type": "string",+ "format": "date"+ }+ },+ "required": [+ "enabled"+ ],+ "title": "PauseReminders"}}}diff --git a/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.html b/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.htmlindex f8a04d3d726..f624fcde898 100644--- a/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.html+++ b/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.html@@ -23,6 +23,9 @@<h5 [textContent]="text.workdays.title"></h5><op-workdays-settings></op-workdays-settings></section>+ <section class="form--section">+ <op-pause-reminders></op-pause-reminders>+ </section><section class="form--section"><h5 [textContent]="text.alerts.title"></h5><p [textContent]="text.alerts.explanation"></p>diff --git a/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.ts b/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.tsindex 08944710a6d..7d7cbfb7994 100644--- a/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.ts+++ b/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.ts@@ -17,6 +17,7 @@ import {import {DailyRemindersSettings,ImmediateRemindersSettings,+ UserPreferencesModel,} from 'core-app/features/user-preferences/state/user-preferences.model';import {emailAlerts,@@ -28,6 +29,7 @@ import {withLatestFrom,} from 'rxjs/operators';import { filterObservable } from 'core-app/shared/helpers/rxjs/filterWith';+import { NotificationSetting } from 'core-app/features/user-preferences/state/notification-setting.model';export const myReminderPageComponentSelector = 'op-reminders-page';@@ -55,6 +57,10 @@ export class ReminderSettingsPageComponent extends UntilDestroyedMixin implementenabled: this.fb.control(false),times: this.fb.array([]),}),+ pauseReminders: this.fb.group({+ enabled: this.fb.control(false),+ time: this.fb.control(''),+ }),workdays: this.fb.array([this.fb.control(false),this.fb.control(true),@@ -127,31 +133,37 @@ export class ReminderSettingsPageComponent extends UntilDestroyedMixin implementfilterObservable(this.storeService.query.selectLoading(), (val) => !val),).subscribe(([settings, globalSetting]) => {- this.form.get('immediateReminders.mentioned')?.setValue(settings.immediateReminders.mentioned);+ this.buildForm(settings, globalSetting);+ });+ }- this.form.get('dailyReminders.enabled')?.setValue(settings.dailyReminders.enabled);+ private buildForm(settings:UserPreferencesModel, globalSetting:NotificationSetting) {+ this.form.get('immediateReminders.mentioned')?.setValue(settings.immediateReminders.mentioned);- const dailyReminderTimes = this.form.get('dailyReminders.times') as FormArray;- dailyReminderTimes.clear({ emitEvent: false });- settings.dailyReminders.times.forEach((time) => {- dailyReminderTimes.push(this.fb.control(time), { emitEvent: false });- });+ this.form.get('dailyReminders.enabled')?.setValue(settings.dailyReminders.enabled);- dailyReminderTimes.enable({ emitEvent: true });+ this.form.get('pauseReminders')?.patchValue(settings.pauseReminders);- const workdays = this.form.get('workdays') as FormArray;- for (let i = 0; i <= 6; i++) {- const control = workdays.at(i);- control.setValue(settings.workdays.includes(i + 1));- }+ const dailyReminderTimes = this.form.get('dailyReminders.times') as FormArray;+ dailyReminderTimes.clear({ emitEvent: false });+ settings.dailyReminders.times.forEach((time) => {+ dailyReminderTimes.push(this.fb.control(time), { emitEvent: false });+ });- emailAlerts.forEach((alert) => {- this.form.get(`emailAlerts.${alert}`)?.setValue(globalSetting[alert]);- });+ dailyReminderTimes.enable({ emitEvent: true });- this.formInitialized = true;- this.cdRef.detectChanges();- });+ const workdays = this.form.get('workdays') as FormArray;+ for (let i = 0; i <= 6; i++) {+ const control = workdays.at(i);+ control.setValue(settings.workdays.includes(i + 1));+ }++ emailAlerts.forEach((alert) => {+ this.form.get(`emailAlerts.${alert}`)?.setValue(globalSetting[alert]);+ });++ this.formInitialized = true;+ this.cdRef.detectChanges();}public saveChanges():void {diff --git a/frontend/src/app/features/user-preferences/reminder-settings/pause-reminders/pause-reminders.component.html b/frontend/src/app/features/user-preferences/reminder-settings/pause-reminders/pause-reminders.component.htmlnew file mode 100644index 00000000000..18bdfd25a0b--- /dev/null+++ b/frontend/src/app/features/user-preferences/reminder-settings/pause-reminders/pause-reminders.component.html@@ -0,0 +1,17 @@+<div+ class="op-pause-reminders"+ [formGroup]="form"+>+ <input+ type="checkbox"+ formControlName="enabled"+ id="op-pause-reminders-enabled"+ class="op-pause-reminders--enabled"+ />+ <label+ class="op-reminder-settings-daily-time--label"+ [textContent]="text.label"+ for="op-pause-reminders-enabled"+ >+ </label>+</div>\ No newline at end of filediff --git a/frontend/src/app/features/user-preferences/reminder-settings/pause-reminders/pause-reminders.component.sass b/frontend/src/app/features/user-preferences/reminder-settings/pause-reminders/pause-reminders.component.sassnew file mode 100644index 00000000000..e69de29bb2ddiff --git a/frontend/src/app/features/user-preferences/reminder-settings/pause-reminders/pause-reminders.component.ts b/frontend/src/app/features/user-preferences/reminder-settings/pause-reminders/pause-reminders.component.tsnew file mode 100644index 00000000000..69cae33906e--- /dev/null+++ b/frontend/src/app/features/user-preferences/reminder-settings/pause-reminders/pause-reminders.component.ts@@ -0,0 +1,35 @@+import {+ ChangeDetectionStrategy,+ Component,+ OnInit,+} from '@angular/core';+import {+ FormGroup,+ FormGroupDirective,+} from '@angular/forms';+import { I18nService } from 'core-app/core/i18n/i18n.service';++@Component({+ selector: 'op-pause-reminders',+ templateUrl: './pause-reminders.component.html',+ styleUrls: ['./pause-reminders.component.sass'],+ changeDetection: ChangeDetectionStrategy.OnPush,+})+export class PauseRemindersComponent implements OnInit {+ form:FormGroup;++ text = {+ label: this.I18n.t('js.reminders.settings.pause.label'),+ date_placeholder: this.I18n.t('js.placeholders.date'),+ };++ constructor(+ private I18n:I18nService,+ private rootFormGroup:FormGroupDirective,+ ) {+ }++ ngOnInit():void {+ this.form = this.rootFormGroup.control.get('pauseReminders') as FormGroup;+ }+}diff --git a/frontend/src/app/features/user-preferences/state/user-preferences.model.ts b/frontend/src/app/features/user-preferences/state/user-preferences.model.tsindex 1cde1277341..6c6a759298c 100644--- a/frontend/src/app/features/user-preferences/state/user-preferences.model.ts+++ b/frontend/src/app/features/user-preferences/state/user-preferences.model.ts@@ -8,6 +8,11 @@ export interface ImmediateRemindersSettings {mentioned:boolean;}+export interface PauseRemindersSettings {+ enabled:boolean;+ time:string;+}+export interface UserPreferencesModel {autoHidePopups:boolean;commentSortDescending:boolean;@@ -18,4 +23,5 @@ export interface UserPreferencesModel {notifications:NotificationSetting[];dailyReminders:DailyRemindersSettings;immediateReminders:ImmediateRemindersSettings;+ pauseReminders:PauseRemindersSettings;}diff --git a/frontend/src/app/features/user-preferences/state/user-preferences.store.ts b/frontend/src/app/features/user-preferences/state/user-preferences.store.tsindex ee462b514a5..3df9196de3c 100644--- a/frontend/src/app/features/user-preferences/state/user-preferences.store.ts+++ b/frontend/src/app/features/user-preferences/state/user-preferences.store.ts@@ -49,6 +49,10 @@ function createInitialState():UserPreferencesModel {immediateReminders: {mentioned: false,},+ pauseReminders: {+ enabled: false,+ time: '',+ },};}diff --git a/frontend/src/app/features/user-preferences/user-preferences.module.ts b/frontend/src/app/features/user-preferences/user-preferences.module.tsindex e9002d6a6fb..9eafd8127d7 100644--- a/frontend/src/app/features/user-preferences/user-preferences.module.ts+++ b/frontend/src/app/features/user-preferences/user-preferences.module.ts@@ -18,6 +18,7 @@ import { ReminderSettingsDailyTimeComponent } from 'core-app/features/user-prefeimport { ImmediateReminderSettingsComponent } from 'core-app/features/user-preferences/reminder-settings/immediate-reminders/immediate-reminder-settings.component';import { EmailAlertsSettingsComponent } from 'core-app/features/user-preferences/reminder-settings/email-alerts/email-alerts-settings.component';import { WorkdaysSettingsComponent } from './reminder-settings/workdays/workdays-settings.component';+import { PauseRemindersComponent } from './reminder-settings/pause-reminders/pause-reminders.component';@NgModule({providers: [@@ -33,6 +34,7 @@ import { WorkdaysSettingsComponent } from './reminder-settings/workdays/workdaysImmediateReminderSettingsComponent,EmailAlertsSettingsComponent,WorkdaysSettingsComponent,+ PauseRemindersComponent,],imports: [CommonModule,diff --git a/spec/contracts/user_preferences/update_contract_spec.rb b/spec/contracts/user_preferences/update_contract_spec.rbindex 00aabdcdd36..d70b80de635 100644--- a/spec/contracts/user_preferences/update_contract_spec.rb+++ b/spec/contracts/user_preferences/update_contract_spec.rb@@ -295,4 +295,84 @@ describe UserPreferences::UpdateContract do.to match_array([ActiveSupport::TimeZone['London'], ActiveSupport::TimeZone['Zurich']])endend++ describe 'puase_reminders' do+ context 'with enabled false' do+ let(:settings) do+ {+ pause_reminders: { enabled: false }+ }+ end++ it_behaves_like 'contract is valid'+ end++ context 'with enabled true but and valid range' do+ let(:settings) do+ {+ pause_reminders: {+ enabled: true,+ first_day: '2021-10-05',+ last_day: '2021-10-10'+ }+ }+ end++ it_behaves_like 'contract is valid'+ end++ context 'with empty object' do+ let(:settings) do+ {+ pause_reminders: {}+ }+ end++ it_behaves_like 'contract is invalid', pause_reminders: :blank_nested+ end++ context 'with enabled true but no days' do+ let(:settings) do+ {+ pause_reminders: { enabled: true }+ }+ end++ it_behaves_like 'contract is invalid', pause_reminders: :presence+ end++ context 'with enabled true but invalid dates' do+ let(:settings) do+ {+ pause_reminders: {+ enabled: true,+ first_day: '2021-10-05T08:21:35Z',+ last_day: '2021-10-05T08:21:35Z'+ }+ }+ end++ it_behaves_like 'contract is invalid', pause_reminders: %i[format_nested format_nested]+ end++ context 'with enabled true but only first day' do+ let(:settings) do+ {+ pause_reminders: { enabled: true, first_day: '2021-10-05' }+ }+ end++ it_behaves_like 'contract is invalid', pause_reminders: :presence+ end++ context 'with enabled true but only last day' do+ let(:settings) do+ {+ pause_reminders: { enabled: true, last_day: '2021-10-05' }+ }+ end++ it_behaves_like 'contract is invalid', pause_reminders: :presence+ end+ endenddiff --git a/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb b/spec/models/users/scopes/having_reminder_mail_to_send_spec.rbindex 1eec7bb8366..e97445c1998 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@@ -54,11 +54,17 @@ describe User, '.having_reminder_mail_to_send', type: :model dopreferences: {time_zone: "Europe/Paris",workdays: paris_user_workdays,+ pause_reminders: paris_user_pause_reminders,daily_reminders: paris_user_daily_reminders})endlet(:paris_user_workdays) { [1, 2, 3, 4, 5] }+ let(:paris_user_pause_reminders) do+ {+ enabled: false+ }+ endlet(:paris_user_daily_reminders) do{enabled: true,@@ -90,6 +96,51 @@ describe User, '.having_reminder_mail_to_send', type: :model doendend+ context 'for a user whose local time is matching but the reminders are paused' do+ let(:paris_user_pause_reminders) do+ {+ enabled: true,+ first_day: '2021-09-20',+ last_day: '2021-10-05'+ }+ end++ it 'is empty' do+ expect(scope)+ .to be_empty+ end+ end++ context 'for a user whose local time is matching but the reminders are paused until today' do+ let(:paris_user_pause_reminders) do+ {+ enabled: true,+ first_day: '2021-09-10',+ last_day: '2021-09-30'+ }+ end++ it 'is empty' do+ expect(scope)+ .to be_empty+ end+ end++ context 'for a user whose local time is matching and the pause reminders is expired' do+ let(:paris_user_pause_reminders) do+ {+ enabled: true,+ first_day: '2021-09-10',+ last_day: '2021-09-29'+ }+ end++ it 'contains the user' do+ expect(scope)+ .to match_array([paris_user])+ end+ end+context 'for a user whose local time is not matching the configured time' dolet(:current_time) { "2021-09-30T08:20:59Z".to_datetime }let(:scope_time) { "2021-09-30T08:15:00Z".to_datetime }