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

Ensure backend validation for days_per_week/days_per_month consistency

Aaron Contrerasbe3c0c83a1c0
diff --git a/app/contracts/settings/working_days_and_hours_params_contract.rb b/app/contracts/settings/working_days_and_hours_params_contract.rb
index 7ce38caaeaa..2a73109218a 100644
--- a/app/contracts/settings/working_days_and_hours_params_contract.rb
+++ b/app/contracts/settings/working_days_and_hours_params_contract.rb
@@ -34,6 +34,7 @@ module Settings
validate :hours_per_day_are_present
validate :days_per_week_are_present
validate :days_per_month_are_present
+ validate :days_per_week_and_days_per_month_are_consistent
validate :unique_job
protected
@@ -62,6 +63,14 @@ module Settings
end
end
+ def days_per_week_and_days_per_month_are_consistent
+ if days_per_week &&
+ days_per_month &&
+ days_per_week.to_i != days_per_month.to_i / ChronicDuration::FULL_WEEKS_PER_MONTH
+ errors.add :base, :days_per_week_and_days_per_month_are_inconsistent
+ end
+ end
+
def unique_job
WorkPackages::ApplyWorkingDaysChangeJob.new.check_concurrency do
errors.add :base, :previous_working_day_changes_unprocessed
diff --git a/app/services/duration_converter.rb b/app/services/duration_converter.rb
index 885ec15805a..921a4784c44 100644
--- a/app/services/duration_converter.rb
+++ b/app/services/duration_converter.rb
@@ -66,7 +66,9 @@ class DurationConverter
end
def duration_length_options
- { hours_per_day: Setting.hours_per_day, days_per_month: Setting.days_per_month }
+ { hours_per_day: Setting.hours_per_day,
+ days_per_month: Setting.days_per_month,
+ weeks: true }
end
end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index f2e5360153f..2c9ad9191e6 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1020,6 +1020,7 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
hours_per_day_are_missing: "The number of hours per day must be defined."
days_per_week_are_missing: "The number of days per week must be defined."
days_per_month_are_missing: "The number of days per month must be defined."
+ days_per_week_and_days_per_month_are_inconsistent: "The number of days per week and the number of days per month must be consistent."
time_entry:
attributes:
hours:
diff --git a/frontend/src/app/core/datetime/timezone.service.ts b/frontend/src/app/core/datetime/timezone.service.ts
index fe8042b6e1e..64c49f5386d 100644
--- a/frontend/src/app/core/datetime/timezone.service.ts
+++ b/frontend/src/app/core/datetime/timezone.service.ts
@@ -147,8 +147,11 @@ export class TimezoneService {
public formattedChronicDuration(durationString:string, opts = {
format: 'short',
hoursPerDay: this.configurationService.hoursPerDay(),
- hoursPerWeek: this.configurationService.hoursPerWeek(),
+ // daysPerWeek is solely a convenience unit for the user's comprehension.
+ // It's not an accepted unit by chronicDuration. daysPerMonth is the value
+ // we provide it.
daysPerMonth: this.configurationService.daysPerMonth(),
+ weeks: true,
}):string {
return outputChronicDuration(this.toHours(durationString) * 3600, opts) || '0h';
}
diff --git a/spec/contracts/settings/working_days_and_hours_params_contract_spec.rb b/spec/contracts/settings/working_days_and_hours_params_contract_spec.rb
index 4272ed1c6bd..faf60b034ab 100644
--- a/spec/contracts/settings/working_days_and_hours_params_contract_spec.rb
+++ b/spec/contracts/settings/working_days_and_hours_params_contract_spec.rb
@@ -33,22 +33,24 @@ RSpec.describe Settings::WorkingDaysAndHoursParamsContract do
include_context "ModelContract shared context"
shared_let(:current_user) { create(:admin) }
let(:setting) { Setting }
- let(:params) { { working_days: [1] } }
+ let(:params) { { working_days: [1], hours_per_day: 8, days_per_week: 5, days_per_month: 20 } }
let(:contract) do
described_class.new(setting, current_user, params:)
end
it_behaves_like "contract is valid for active admins and invalid for regular users"
- context "without working days" do
- let(:params) { { working_days: [] } }
+ %i[working_days hours_per_day days_per_week days_per_month].each do |attribute|
+ context "without #{attribute}" do
+ let(:params) { { working_days: [1], hours_per_day: 8, days_per_week: 5, days_per_month: 20 }.except(attribute) }
- include_examples "contract is invalid", base: :working_days_are_missing
+ include_examples "contract is invalid", base: :"#{attribute}_are_missing"
+ end
end
context "with an ApplyWorkingDaysChangeJob already existing",
with_good_job: WorkPackages::ApplyWorkingDaysChangeJob do
- let(:params) { { working_days: [1, 2, 3] } }
+ let(:params) { { working_days: [1, 2, 3], hours_per_day: 8, days_per_week: 5, days_per_month: 20 } }
before do
WorkPackages::ApplyWorkingDaysChangeJob
@@ -60,4 +62,11 @@ RSpec.describe Settings::WorkingDaysAndHoursParamsContract do
include_examples "contract is invalid", base: :previous_working_day_changes_unprocessed
end
+
+ context "when days_per_week and days_per_month aren't consistent with each other" do
+ # There are 4 weeks per month on average, so 10 days per month in non-sensical given 5 days per week
+ let(:params) { { working_days: [1], hours_per_day: 8, days_per_week: 5, days_per_month: 10 } }
+
+ include_examples "contract is invalid", base: :days_per_week_and_days_per_month_are_inconsistent
+ end
end