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

contract and service handling date ranges only

Ivan Kuchin0cba9a64af53
diff --git a/app/contracts/project_life_cycle_steps/update_single_contract.rb b/app/contracts/project_life_cycle_steps/update_single_contract.rb
new file mode 100644
index 00000000000..58240d62ad4
--- /dev/null
+++ b/app/contracts/project_life_cycle_steps/update_single_contract.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module ProjectLifeCycleSteps
+ class UpdateSingleContract < BaseContract
+ validate :validate_start_after_preceeding_phases
+
+ delegate :project, to: :model
+
+ def writable_attributes = %w[start_date finish_date active]
+
+ def validate_start_after_preceeding_phases
+ return unless model.active?
+ return unless model.range_set?
+ return if start_after_preceding_phases?
+
+ model.errors.add(:date_range, :non_continuous_dates)
+ end
+
+ private
+
+ def start_after_preceding_phases?
+ preceding_phases
+ .select(&:range_set?)
+ .all? { valid_dates?(current: model, previous: it) }
+ end
+
+ def valid_dates?(current:, previous:)
+ current.start_date > previous.finish_date
+ end
+
+ def preceding_phases
+ project.available_phases.select { it.position < model.position }
+ end
+ end
+end
diff --git a/app/services/project_life_cycle_steps/update_single_service.rb b/app/services/project_life_cycle_steps/update_single_service.rb
new file mode 100644
index 00000000000..9fb44dfa212
--- /dev/null
+++ b/app/services/project_life_cycle_steps/update_single_service.rb
@@ -0,0 +1,98 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module ProjectLifeCycleSteps
+ class UpdateSingleService < ::BaseServices::Update
+ def after_validate(*)
+ set_duration
+
+ super
+ end
+
+ def after_perform(*)
+ reschedule_following_phases if model.range_set?
+
+ super
+ end
+
+ private
+
+ def default_contract_class = ProjectLifeCycleSteps::UpdateSingleContract
+
+ def set_duration
+ model.duration = calculate_duration
+ end
+
+ def calculate_duration
+ return nil unless model.range_set?
+
+ Day.working.from_range(from: model.start_date, to: model.finish_date).count
+ end
+
+ def reschedule_following_phases
+ from = initial_reschedule_date
+
+ following_phases.each do |phase|
+ next unless phase.range_set?
+ next unless phase.duration # not updated using service that sets duration
+
+ date_range = calculate_date_range(from, duration: phase.duration)
+ next unless date_range
+
+ next unless phase.update(date_range:)
+
+ from = date_range.end + 1
+ end
+ end
+
+ def initial_reschedule_date
+ model.active? ? model.finish_date + 1 : model.start_date
+ end
+
+ def following_phases
+ model.project.available_phases.select { it.position > model.position }
+ end
+
+ def calculate_date_range(from, duration:)
+ days = working_days_from(from, count: duration)
+
+ days.first.date..days.last.date if days.length == duration
+ end
+
+ def working_days_from(from, count:)
+ days = Day.working.from_range(from:, to: from.next_year).first(count)
+ if days.length < count && !days.empty?
+ years = count.ceildiv(days.length) + 1
+ days = Day.working.from_range(from:, to: from.next_year(years)).first(count)
+ end
+ days
+ end
+ end
+end
diff --git a/spec/contracts/project_life_cycle_steps/update_single_contract_spec.rb b/spec/contracts/project_life_cycle_steps/update_single_contract_spec.rb
new file mode 100644
index 00000000000..8a740a8b2f8
--- /dev/null
+++ b/spec/contracts/project_life_cycle_steps/update_single_contract_spec.rb
@@ -0,0 +1,147 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+require "contracts/shared/model_contract_shared_context"
+
+RSpec.describe ProjectLifeCycleSteps::UpdateSingleContract do
+ include_context "ModelContract shared context"
+
+ let(:user) { build_stubbed(:user) }
+
+ subject(:contract) { described_class.new(phase, user) }
+
+ context "with authorized user" do
+ let(:phase) { build_stubbed(:project_phase) }
+ let(:project) { phase.project }
+ let(:date) { Date.current }
+
+ before do
+ mock_permissions_for(user) do |mock|
+ mock.allow_in_project(:edit_project_phases, project:)
+ end
+ end
+
+ it_behaves_like "contract is valid"
+ include_examples "contract reuses the model errors"
+
+ context "when phase is invalid" do
+ let(:phase) 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
+ end
+
+ context "when trying to change extra attributes" do
+ before do
+ phase.duration = 42
+ end
+
+ it_behaves_like "contract is invalid", duration: :error_readonly
+ end
+
+ describe "#validate_start_after_preceeding_phases" do
+ def build_phase(**) = build_stubbed(:project_phase, project:, **)
+
+ let(:project) { build_stubbed(:project) }
+ let(:phases) { [preceding, phase, following] }
+ let(:phase) { build_phase(date_range:, active:) }
+ let(:preceding) { build_phase(date_range: preceding_date_range) }
+ let(:following) { build_phase(date_range: following_date_range) }
+ let(:active) { true }
+ let(:date_range) { date - 1..date + 1 }
+ let(:preceding_date_range) { date - 6..date - 5 }
+ let(:following_date_range) { date + 5..date + 6 }
+
+ before do
+ allow(project).to receive(:available_phases).and_return(phases)
+ end
+
+ context "with successive non overlapping dates" do
+ it_behaves_like "contract is valid"
+ end
+
+ context "without dates" do
+ let(:date_range) { nil }
+
+ it_behaves_like "contract is valid"
+ end
+
+ 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
+
+ context "when inactive" do
+ let(:active) { false }
+
+ it_behaves_like "contract is valid"
+ end
+ end
+
+ 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
+
+ context "when inactive" do
+ let(:active) { false }
+
+ it_behaves_like "contract is valid"
+ end
+ end
+
+ context "with preceding phase without dates" do
+ let(:preceding_date_range) { nil }
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "with following phase overlapping with start" do
+ let(:following_date_range) { date - 1..date + 6 }
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "with following phase preceding this" do
+ let(:following_date_range) { date - 4..date - 2 }
+
+ it_behaves_like "contract is valid"
+ end
+ end
+ end
+
+ context "with unauthorized user" do
+ let(:phase) { build_stubbed(:project_phase) }
+
+ it_behaves_like "contract user is unauthorized"
+ end
+end
diff --git a/spec/services/project_life_cycle_steps/update_single_service_spec.rb b/spec/services/project_life_cycle_steps/update_single_service_spec.rb
new file mode 100644
index 00000000000..145069d38b1
--- /dev/null
+++ b/spec/services/project_life_cycle_steps/update_single_service_spec.rb
@@ -0,0 +1,219 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+require "spec_helper"
+require "services/base_services/behaves_like_update_service"
+
+RSpec.describe ProjectLifeCycleSteps::UpdateSingleService, type: :model do
+ shared_let(:week_days) { week_with_saturday_and_sunday_as_weekend }
+
+ it_behaves_like "BaseServices update service" do
+ let(:factory) { :project_phase }
+ let(:contract_class) { ProjectLifeCycleSteps::UpdateSingleContract }
+ end
+
+ describe "#set_duration" do
+ shared_let(:phase) { create(:project_phase, duration: 0) }
+
+ let(:user) { build_stubbed(:user) }
+ let(:project) { phase.project }
+ let(:service) { described_class.new(user:, model: phase) }
+ let(:date) { Date.current }
+
+ before do
+ mock_permissions_for(user) do |mock|
+ mock.allow_in_project(:edit_project_phases, project:)
+ end
+ end
+
+ it "calculates and sets the duration based on the date range" do
+ expect(service.call(date_range: date..date + 27)).to be_success
+
+ expect(phase.reload.duration).to eq(20)
+ end
+
+ it "nullifies duration without start date" do
+ expect(service.call(start_date: nil)).to be_success
+
+ expect(phase.reload.duration).to be_nil
+ end
+
+ it "nullifies duration without finish date" do
+ expect(service.call(finish_date: nil)).to be_success
+
+ expect(phase.reload.duration).to be_nil
+ end
+
+ it "nullifies duration without date range" do
+ expect(service.call(date_range: nil)).to be_success
+
+ expect(phase.reload.duration).to be_nil
+ end
+ end
+
+ describe "#reschedule_following_phases" do
+ let(:date) { Date.new(2025, 4, 9) }
+ let(:user) { build_stubbed(:user) }
+ let(:service) { described_class.new(user:, model: phase) }
+ let(:project) { create(:project) }
+
+ before do
+ mock_permissions_for(user) do |mock|
+ mock.allow_in_project(:edit_project_phases, project:)
+ end
+
+ allow(project).to receive(:available_phases).and_return(phases)
+ end
+
+ context "for invalid date range" do
+ let(:phases) { [phase, following] }
+ let(:phase) { create(:project_phase, project:, date_range: date..date) }
+ let(:following) { build(:project_phase, project:, date_range: date - 10..date - 10, duration: 3) }
+
+ it "doesn't get rescheduled" do
+ expect do
+ expect(service.call(date_range: date..date - 1)).not_to be_success
+ end.not_to change(following, :attributes)
+ end
+ end
+
+ context "for preceding phase" do
+ let(:phases) { [preceding, phase] }
+ let(:phase) { create(:project_phase, project:, date_range: date..date) }
+ let(:preceding) { create(:project_phase, project:, date_range: date - 7..date - 7, duration: 1) }
+
+ it "doesn't get rescheduled" do
+ expect do
+ expect(service.call(date_range: date..date + 1)).to be_success
+ end.not_to change(preceding, :attributes)
+ end
+ end
+
+ context "for following phases" do
+ let(:phases) { [phase, following1, following2, following3] }
+ let(:phase) { create(:project_phase, project:, date_range: date..date) }
+ let(:following1) { create(:project_phase, project:, date_range: date + 6..date + 8, duration: 3) }
+ let(:following2) { create(:project_phase, project:, date_range: date + 6..date + 8, duration: 2) }
+ let(:following3) { create(:project_phase, project:, date_range: date + 6..date + 8, duration: 1) }
+
+ it "reschedules all of them relying on duration" do
+ expect(service.call(date_range: date..date + 1)).to be_success
+
+ expect(following1).to have_attributes(start_date: date + 2, finish_date: date + 6, duration: 3)
+ expect(following2).to have_attributes(start_date: date + 7, finish_date: date + 8, duration: 2)
+ expect(following3).to have_attributes(start_date: date + 9, finish_date: date + 9, duration: 1)
+ end
+ end
+
+ context "for following phase without duration" do
+ let(:phases) { [phase, following] }
+ let(:phase) { create(:project_phase, project:, date_range: date..date) }
+ let(:following) { build(:project_phase, project:, date_range: date + 6..date + 8, duration: nil) }
+
+ it "doesn't get rescheduled" do
+ expect do
+ expect(service.call(date_range: date..date + 1)).to be_success
+ end.not_to change(following, :attributes)
+ end
+ end
+
+ context "for following phase without date range" do
+ let(:phases) { [phase, following] }
+ let(:phase) { create(:project_phase, project:, date_range: date..date) }
+ let(:following) { build(:project_phase, project:, date_range: nil, duration: 3) }
+
+ it "doesn't get rescheduled" do
+ expect do
+ expect(service.call(date_range: date..date + 1)).to be_success
+ end.not_to change(following, :attributes)
+ end
+ end
+
+ context "for following phase with date range in the past" do
+ let(:phases) { [phase, following] }
+ let(:phase) { create(:project_phase, project:, date_range: date..date) }
+ let(:following) { build(:project_phase, project:, date_range: date - 10..date - 10, duration: 3) }
+
+ it "reschedules it" do
+ expect(service.call(date_range: date..date + 1)).to be_success
+
+ expect(following).to have_attributes(start_date: date + 2, finish_date: date + 6, duration: 3)
+ end
+ end
+
+ context "for following phase with duration over a year" do
+ let(:phases) { [phase, following] }
+ let(:phase) { create(:project_phase, project:, date_range: date..date) }
+ let(:following) { build(:project_phase, project:, date_range: date..date, duration: 500) }
+
+ it "reschedules it" do
+ expect(service.call(date_range: date..date + 1)).to be_success
+
+ expect(following).to have_attributes(start_date: date + 2, finish_date: date + 701, duration: 500)
+ end
+ end
+
+ context "for following phase with duration over 10 years" do
+ let(:phases) { [phase, following] }
+ let(:phase) { create(:project_phase, project:, date_range: date..date) }
+ let(:following) { build(:project_phase, project:, date_range: date..date, duration: 3000) }
+
+ it "reschedules it" do
+ expect(service.call(date_range: date..date + 1)).to be_success
+
+ expect(following).to have_attributes(start_date: date + 2, finish_date: date + 4201, duration: 3000)
+ end
+ end
+
+ context "when activating" do
+ let(:phases) { [phase, following] }
+ let(:phase) { create(:project_phase, project:, date_range: date - 1..date + 1, active: false) }
+ let(:following) { create(:project_phase, project:, date_range: date + 6..date + 8, duration: 3) }
+
+ it "reschedules starting with date after phase finish date" do
+ expect(service.call(active: true)).to be_success
+
+ expect(following).to have_attributes(start_date: date + 2, finish_date: date + 6, duration: 3)
+ end
+ end
+
+ context "when deactivating" do
+ let(:phases) { [phase, following] }
+ let(:phase) { create(:project_phase, project:, date_range: date - 1..date + 1, active: true) }
+ let(:following) { create(:project_phase, project:, date_range: date + 6..date + 8, duration: 3) }
+
+ it "reschedules starting with phase start date" do
+ expect(service.call(active: false)).to be_success
+
+ expect(following).to have_attributes(start_date: date - 1, finish_date: date + 1, duration: 3)
+ end
+ end
+ end
+end