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
[#43673] move validation to contract
Dmitriiac1d98ea03af
diff --git a/app/contracts/user_preferences/params_contract.rb b/app/contracts/user_preferences/params_contract.rbindex 4cfa0657344..1dfaf438b3c 100644--- a/app/contracts/user_preferences/params_contract.rb+++ b/app/contracts/user_preferences/params_contract.rb@@ -28,11 +28,16 @@module UserPreferencesclass ParamsContract < ::ParamsContract+ AVAILABLE_DATE = [nil, 12, 24, 78, 168].freeze+validate :only_one_global_setting,if: -> { notifications.present? }-validate :global_email_alerts,if: -> { notifications.present? }+ validate :global_setting_start_date_due_date_overdue,+ if: -> { notifications.present? }+ validate :project_notifications_start_date_due_date_overdue,+ if: -> { notifications.present? }protected@@ -48,6 +53,34 @@ module UserPreferencesendend+ def global_setting_start_date_due_date_overdue+ return unless stores_all_date_fields?(global_notifications)++ if any_of_date_fields_fail_validation?(global_notifications)+ errors.add :notification_settings, :wrong_date+ end+ end++ def project_notifications_start_date_due_date_overdue+ return unless stores_all_date_fields?(project_notifications)++ if any_of_date_fields_fail_validation?(project_notifications)+ errors.add :notification_settings, :wrong_date+ end+ end++ def stores_all_date_fields?(notifications_type)+ notifications_type.all? { |setting| setting.key?(:start_date) && setting.key?(:due_date) && setting.key?(:overdue) }+ end++ def any_of_date_fields_fail_validation?(notifications_type)+ notifications_type.any? do |setting|+ setting.slice(:start_date, :due_date, :overdue).detect do |_, date|+ AVAILABLE_DATE.exclude?(date)+ end+ end+ end+### Check if the given notification hash has email-only settings setdef email_alerts_set?(notification_setting)diff --git a/app/models/notification_setting.rb b/app/models/notification_setting.rbindex f2943cf7c62..991a68823d5 100644--- a/app/models/notification_setting.rb+++ b/app/models/notification_setting.rb@@ -56,9 +56,4 @@ class NotificationSetting < ApplicationRecordinclude Scopes::Scopedscopes :applicable-- validates :start_date, :due_date, :overdue, numericality: { only_integer: true,- allow_nil: true,- greater_than_or_equal_to: 0,- less_than_or_equal_to: 168 }enddiff --git a/app/services/user_preferences/update_service.rb b/app/services/user_preferences/update_service.rbindex 2402b263a0c..3729f547346 100644--- a/app/services/user_preferences/update_service.rb+++ b/app/services/user_preferences/update_service.rb@@ -50,10 +50,7 @@ module UserPreferencesreturn service_call if notifications.nil?inserted = persist_notifications-- if inserted.present?- remove_other_notifications(inserted)- end+ remove_other_notifications(inserted)service_callend@@ -113,7 +110,7 @@ module UserPreferencesmembership_addedmembership_updated]},- validate: true+ validate: false).idsendenddiff --git a/spec/contracts/user_preferences/params_contract_spec.rb b/spec/contracts/user_preferences/params_contract_spec.rbindex 7c9f3de460a..ddccbed4b1a 100644--- a/spec/contracts/user_preferences/params_contract_spec.rb+++ b/spec/contracts/user_preferences/params_contract_spec.rb@@ -86,6 +86,26 @@ describe UserPreferences::ParamsContract doit_behaves_like 'contract is valid'end+ context 'when project setting with start_date, due_date and overdue set' do+ let(:notification_settings) do+ [+ { start_date: 24, due_date: 24, overdue: 24 }+ ]+ end++ it_behaves_like 'contract is valid'+ end++ context 'when project setting with start_date, due_date and overdue is invalid' do+ let(:notification_settings) do+ [+ { start_date: 22, due_date: 24, overdue: 24 }+ ]+ end++ it_behaves_like 'contract is invalid', notification_settings: :wrong_date+ end+context 'when notification_settings empty' dolet(:params) do{diff --git a/spec/factories/user_factory.rb b/spec/factories/user_factory.rbindex baeaa5e9368..29d279f3fd1 100644--- a/spec/factories/user_factory.rb+++ b/spec/factories/user_factory.rb@@ -58,9 +58,10 @@ FactoryBot.define douser.pref.save if factory.preferences.present?if user.notification_settings.empty?- all_true = NotificationSetting.all_settings.index_with(true)+ date_settings = [NotificationSetting::START_DATE, NotificationSetting::DUE_DATE, NotificationSetting::OVERDUE]+ all_true = NotificationSetting.all_settings.reject { |setting| date_settings.include?(setting) }.index_with(true)user.notification_settings = [- create(:notification_setting, user:, **all_true)+ create(:notification_setting, user:, **all_true.merge(date_settings.index_with(24)))]enddiff --git a/spec/lib/api/v3/user_preferences/notification_setting_representer_rendering_spec.rb b/spec/lib/api/v3/user_preferences/notification_setting_representer_rendering_spec.rbindex e56750a1bc8..64a8cd037bc 100644--- a/spec/lib/api/v3/user_preferences/notification_setting_representer_rendering_spec.rb+++ b/spec/lib/api/v3/user_preferences/notification_setting_representer_rendering_spec.rb@@ -34,7 +34,7 @@ describe ::API::V3::UserPreferences::NotificationSettingRepresenter, 'rendering'subject(:generated) { representer.to_json }let(:project) { build_stubbed :project }- let(:notification_setting) { build_stubbed(:notification_setting, project:) }+ let(:notification_setting) { build_stubbed(:notification_setting, overdue: 24, project:) }let(:representer) dodescribed_class.create notification_setting,diff --git a/spec/lib/api/v3/user_preferences/user_preference_representer_rendering_spec.rb b/spec/lib/api/v3/user_preferences/user_preference_representer_rendering_spec.rbindex 4b3638bbe0b..b845cbdddf5 100644--- a/spec/lib/api/v3/user_preferences/user_preference_representer_rendering_spec.rb+++ b/spec/lib/api/v3/user_preferences/user_preference_representer_rendering_spec.rb@@ -41,7 +41,7 @@ describe ::API::V3::UserPreferences::UserPreferenceRepresenter,}})end- let(:notification_setting) { build(:notification_setting) }+ let(:notification_setting) { build(:notification_setting, overdue: 24) }let(:user) { build_stubbed(:user, preference:) }let(:representer) { described_class.new(preference, current_user: user) }diff --git a/spec/services/user_preferences/update_service_integration_spec.rb b/spec/services/user_preferences/update_service_integration_spec.rbindex 4519acbd4f9..1cd668ac8d6 100644--- a/spec/services/user_preferences/update_service_integration_spec.rb+++ b/spec/services/user_preferences/update_service_integration_spec.rb@@ -122,7 +122,14 @@ describe UserPreferences::UpdateService, 'integration', type: :model doNotificationSetting.all_settings.each do |key|val = subject.first.send key- expect(val).to eq(key == :mentioned)++ if [NotificationSetting::START_DATE, NotificationSetting::DUE_DATE].include?(key)+ expect(val).to eq(24)+ elsif key == NotificationSetting::OVERDUE+ expect(val).to be_nil+ else+ expect(val).to eq(key == :mentioned)+ endendexpect(current_user.notification_settings.count).to eq(1)