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

Validate start and finish dates do not fall on a non working day.

Dombi Attila45ba1485f26a
diff --git a/app/contracts/project_life_cycle_steps/update_contract.rb b/app/contracts/project_life_cycle_steps/update_contract.rb
index fc475ef6fb4..448ea84305f 100644
--- a/app/contracts/project_life_cycle_steps/update_contract.rb
+++ b/app/contracts/project_life_cycle_steps/update_contract.rb
@@ -31,6 +31,8 @@
module ProjectLifeCycleSteps
class UpdateContract < BaseContract
validate :validate_start_after_preceeding_phases
+ validate :validate_start_date_is_a_working_day
+ validate :validate_finish_date_is_a_working_day
delegate :project, to: :model
@@ -44,6 +46,18 @@ module ProjectLifeCycleSteps
model.errors.add(:start_date, :non_continuous_dates)
end
+ def validate_start_date_is_a_working_day
+ if model.start_date.present? && !model.start_date.in?(working_days)
+ model.errors.add(:start_date, :cannot_be_a_non_working_day)
+ end
+ end
+
+ def validate_finish_date_is_a_working_day
+ if model.finish_date.present? && !model.finish_date.in?(working_days)
+ model.errors.add(:finish_date, :cannot_be_a_non_working_day)
+ end
+ end
+
private
def start_after_preceding_phases?
@@ -59,5 +73,16 @@ module ProjectLifeCycleSteps
def preceding_phases
project.available_phases.select { it.position < model.position }
end
+
+ def working_days
+ return @working_days if defined?(@working_days)
+
+ dates = [model.start_date, model.finish_date].compact
+ @working_days = if dates.any?
+ Day.from_range(from: dates.min, to: dates.max).working.pluck("days.date")
+ else
+ []
+ end
+ end
end
end
diff --git a/app/models/day.rb b/app/models/day.rb
index 51a1ab09395..a2c7261b838 100644
--- a/app/models/day.rb
+++ b/app/models/day.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -44,55 +46,57 @@ class Day < ApplicationRecord
scope :working, -> { where(working: true) }
- def self.default_scope
- today = Time.zone.today
- from = today.at_beginning_of_month
- to = today.next_month.at_end_of_month
- from_range(from:, to:)
- .includes(:non_working_days)
- .order("days.id")
- end
+ class << self
+ def default_scope
+ today = Time.zone.today
+ from = today.at_beginning_of_month
+ to = today.next_month.at_end_of_month
+ from_range(from:, to:)
+ .includes(:non_working_days)
+ .order("days.id")
+ end
- def self.from_range(from:, to:)
- from(Arel.sql(from_sql(from:, to:)))
- end
+ def from_range(from:, to:)
+ from(Arel.sql(from_sql(from:, to:)))
+ end
- def self.from_sql(from:, to:)
- from = from.to_date
- to = to.to_date
- <<~SQL.squish
- (
- SELECT
- to_char(dd, 'YYYYMMDD')::integer id,
- date_trunc('day', dd)::date date,
- extract(isodow from dd) day_of_week,
- (
- COALESCE(
- POSITION(
- extract(isodow from dd)::text IN settings.value
- ) > 0,
- TRUE
- )
- AND
- non_working_days.id IS NULL
- )::bool working
- FROM
- generate_series(
- '#{from}'::timestamp,
- '#{to}'::timestamp,
- '1 day'::interval
- ) dd
- LEFT JOIN settings
- ON settings.name = 'working_days'
- LEFT JOIN non_working_days
- ON dd = non_working_days.date
- ) days
- SQL
- end
+ def from_sql(from:, to:)
+ from = from.to_date
+ to = to.to_date
+ <<~SQL.squish
+ (
+ SELECT
+ to_char(dd, 'YYYYMMDD')::integer id,
+ date_trunc('day', dd)::date date,
+ extract(isodow from dd) day_of_week,
+ (
+ COALESCE(
+ POSITION(
+ extract(isodow from dd)::text IN settings.value
+ ) > 0,
+ TRUE
+ )
+ AND
+ non_working_days.id IS NULL
+ )::bool working
+ FROM
+ generate_series(
+ '#{from}'::timestamp,
+ '#{to}'::timestamp,
+ '1 day'::interval
+ ) dd
+ LEFT JOIN settings
+ ON settings.name = 'working_days'
+ LEFT JOIN non_working_days
+ ON dd = non_working_days.date
+ ) days
+ SQL
+ end
- def self.last_working
- # Look up only from 8 days ago, because the Setting.working_days must have at least 1 working weekday.
- from_range(from: 8.days.ago, to: Time.zone.yesterday).where(working: true).last
+ def last_working
+ # Look up only from 8 days ago, because the Setting.working_days must have at least 1 working weekday.
+ from_range(from: 8.days.ago, to: Time.zone.yesterday).where(working: true).last
+ end
end
def week_day
diff --git a/app/models/project/phase.rb b/app/models/project/phase.rb
index 5ba9ed6e74f..f470b2101c0 100644
--- a/app/models/project/phase.rb
+++ b/app/models/project/phase.rb
@@ -91,12 +91,11 @@ class Project::Phase < ApplicationRecord
def validate_date_range
if range_set? && (start_date > finish_date)
- date_field, error_message = if finish_date_changed?
- %i[finish_date must_be_after_start_date]
- else
- %i[start_date must_be_before_finish_date]
- end
- errors.add(date_field, error_message)
+ if finish_date_changed?
+ errors.add(:finish_date, :must_be_after_start_date)
+ else
+ errors.add(:start_date, :must_be_before_finish_date)
+ end
end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index c1303313b1a..8de74f9fc41 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1371,6 +1371,7 @@ en:
non_continuous_dates: "can't be earlier than the previous phase's end date."
finish_date:
must_be_after_start_date: "must be after the start date."
+ cannot_be_a_non_working_day: "can't be a non-working day."
query:
attributes:
project:
diff --git a/spec/contracts/project_life_cycle_steps/update_contract_spec.rb b/spec/contracts/project_life_cycle_steps/update_contract_spec.rb
index 40eb8ab86e8..b618dcbc9fc 100644
--- a/spec/contracts/project_life_cycle_steps/update_contract_spec.rb
+++ b/spec/contracts/project_life_cycle_steps/update_contract_spec.rb
@@ -57,7 +57,7 @@ RSpec.describe ProjectLifeCycleSteps::UpdateContract do
build_stubbed(:project_phase, start_date: date + 1, finish_date: date - 1)
end
- it_behaves_like "contract is invalid", date_range: :start_date_must_be_before_finish_date
+ it_behaves_like "contract is invalid", start_date: :must_be_before_finish_date
end
context "when trying to change extra attributes" do
@@ -98,7 +98,7 @@ RSpec.describe ProjectLifeCycleSteps::UpdateContract do
context "with preceding phase overlapping with start" do
let(:preceding_date_range) { date - 6..date - 1 }
- it_behaves_like "contract is invalid", date_range: :non_continuous_dates
+ it_behaves_like "contract is invalid", start_date: :non_continuous_dates
context "when inactive" do
let(:active) { false }
@@ -110,7 +110,7 @@ RSpec.describe ProjectLifeCycleSteps::UpdateContract do
context "with preceding phase following this" do
let(:preceding_date_range) { date + 2..date + 4 }
- it_behaves_like "contract is invalid", date_range: :non_continuous_dates
+ it_behaves_like "contract is invalid", start_date: :non_continuous_dates
context "when inactive" do
let(:active) { false }
@@ -137,6 +137,71 @@ RSpec.describe ProjectLifeCycleSteps::UpdateContract do
it_behaves_like "contract is valid"
end
end
+
+ describe "#validate_dates_must_be_on_working_days" do
+ let(:non_working_day) { Date.current }
+ let(:phase) do
+ build_stubbed(:project_phase, start_date:, finish_date:)
+ end
+
+ before do
+ set_non_working_days(non_working_day)
+ end
+
+ context "when the start date is a non working day" do
+ let(:start_date) { non_working_day }
+ let(:finish_date) { non_working_day + 1.day }
+
+ it_behaves_like "contract is invalid", start_date: :cannot_be_a_non_working_day
+
+ context "and the finish date is nil" do
+ let(:finish_date) { nil }
+
+ it_behaves_like "contract is invalid", start_date: :cannot_be_a_non_working_day
+ end
+ end
+
+ context "when the finish date is a non working day" do
+ let(:start_date) { non_working_day - 1.day }
+ let(:finish_date) { non_working_day }
+
+ it_behaves_like "contract is invalid", finish_date: :cannot_be_a_non_working_day
+
+ context "and the start date is nil" do
+ let(:start_date) { nil }
+
+ it_behaves_like "contract is invalid", finish_date: :cannot_be_a_non_working_day
+ end
+ end
+
+ context "when both dates are on non working day" do
+ let(:start_date) { non_working_day }
+ let(:finish_date) { non_working_day }
+
+ it_behaves_like "contract is invalid",
+ start_date: :cannot_be_a_non_working_day,
+ finish_date: :cannot_be_a_non_working_day
+ end
+
+ context "when both dates are on a working day" do
+ let(:start_date) { non_working_day - 1.day }
+ let(:finish_date) { non_working_day + 1 }
+
+ it_behaves_like "contract is valid"
+
+ context "and the start date is nil" do
+ let(:start_date) { nil }
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "and the finish date is nil" do
+ let(:finish_date) { nil }
+
+ it_behaves_like "contract is valid"
+ end
+ end
+ end
end
context "with unauthorized user" do