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

[#42923] Add job to reschedule on weekend days changes

Christophe Bliard51d8c618b1f1
diff --git a/app/workers/work_packages/apply_working_days_change_job.rb b/app/workers/work_packages/apply_working_days_change_job.rb
new file mode 100644
index 00000000000..5d389361e7e
--- /dev/null
+++ b/app/workers/work_packages/apply_working_days_change_job.rb
@@ -0,0 +1,58 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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.
+#++
+
+class WorkPackages::ApplyWorkingDaysChangeJob < ApplicationJob
+ queue_with_priority :above_normal
+
+ def perform(user_id:)
+ user = User.find(user_id)
+
+ WorkPackage
+ .where(ignore_non_working_days: false)
+ .find_each do |work_package|
+ next if dates_and_duration_match?(work_package)
+
+ WorkPackages::SetAttributesService
+ .new(user:, model: work_package, contract_class: EmptyContract)
+ .call(duration: work_package.duration)
+ work_package.save
+ end
+ end
+
+ private
+
+ def dates_and_duration_match?(work_package)
+ days.working?(work_package.start_date) \
+ && days.working?(work_package.due_date) \
+ && days.duration(work_package.start_date, work_package.due_date) == work_package.duration
+ end
+
+ def days
+ @days ||= WorkPackages::Shared::WorkingDays.new
+ end
+end
diff --git a/spec/factories/week_day_factory.rb b/spec/factories/week_day_factory.rb
index 414eeff2f6a..0fb265b80b9 100644
--- a/spec/factories/week_day_factory.rb
+++ b/spec/factories/week_day_factory.rb
@@ -43,7 +43,7 @@ FactoryBot.define do
end
# Factory to create all 7 week days at once, Saturday and Sunday being weekend days
- factory :week_days, class: 'Array' do
+ factory :week_days, aliases: [:week_days_with_saturday_and_sunday_as_weekend], class: 'Array' do
# Skip the create callback to be able to use non-AR models. Otherwise FactoryBot will
# try to call #save! on any created object.
skip_create
diff --git a/spec/factories/work_package_factory.rb b/spec/factories/work_package_factory.rb
index 20a3feecb69..b892e9afb58 100644
--- a/spec/factories/work_package_factory.rb
+++ b/spec/factories/work_package_factory.rb
@@ -40,7 +40,6 @@ FactoryBot.define do
author factory: :user
created_at { Time.zone.now }
updated_at { Time.zone.now }
- duration { WorkPackages::Shared::Days.for(self).duration(start_date&.to_date, due_date&.to_date) }
trait :is_milestone do
type factory: :type_milestone
@@ -49,6 +48,16 @@ FactoryBot.define do
callback(:after_build) do |work_package, evaluator|
work_package.type = work_package.project.types.first unless work_package.type
+ # set duration / start date / due date according to two others
+ days = WorkPackages::Shared::Days.for(work_package)
+ if work_package.duration.nil? && work_package.start_date && work_package.due_date
+ work_package.duration = days.duration(work_package.start_date.to_date, work_package.due_date.to_date)
+ elsif work_package.start_date.nil? && work_package.due_date && work_package.duration
+ work_package.start_date = days.start_date(work_package.due_date.to_date, work_package.duration)
+ elsif work_package.due_date.nil? && work_package.start_date && work_package.duration
+ work_package.due_date = days.due_date(work_package.start_date.to_date, work_package.duration)
+ end
+
custom_values = evaluator.custom_values || {}
if custom_values.is_a? Hash
diff --git a/spec/workers/work_packages/apply_working_days_change_job_spec.rb b/spec/workers/work_packages/apply_working_days_change_job_spec.rb
new file mode 100644
index 00000000000..13e90770997
--- /dev/null
+++ b/spec/workers/work_packages/apply_working_days_change_job_spec.rb
@@ -0,0 +1,77 @@
+require 'rails_helper'
+
+RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob,
+ with_flag: { work_packages_duration_field_active: true } do
+ subject(:job) { described_class }
+
+ shared_let(:user) { create(:user) }
+ shared_let(:week_days) { create(:week_days_with_saturday_and_sunday_as_weekend) }
+
+ def set_non_working_week_day(day)
+ wday = %w[xxx monday tuesday wednesday thursday friday saturday sunday].index(day.downcase)
+ WeekDay.find_by(day: wday).update(working: false)
+ end
+
+ it 'updates finish date of work packages after marking week days as non-working' do
+ work_package = create(:work_package,
+ ignore_non_working_days: false,
+ start_date: Date.parse('Mon 2022-08-22'),
+ duration: 5)
+
+ expect do
+ set_non_working_week_day('wednesday')
+ job.perform_now(user_id: user.id)
+ end
+ .to change { work_package.reload.slice(:due_date) }
+ .from(due_date: Date.parse('Fri 2022-08-26'))
+ .to(due_date: Date.parse('Mon 2022-08-29'))
+ end
+
+ it 'does not change work packages ignoring non-working days' do
+ work_package = create(:work_package,
+ ignore_non_working_days: true,
+ start_date: Date.parse('Mon 2022-08-22'),
+ duration: 5)
+
+ expect do
+ set_non_working_week_day('wednesday')
+ job.perform_now(user_id: user.id)
+ end
+ .not_to change { work_package.reload }
+ end
+
+ it 'updates multiple work packages' do
+ create_list(:work_package,
+ 5,
+ ignore_non_working_days: false,
+ start_date: Date.parse('Mon 2022-08-22'),
+ duration: 5)
+
+ set_non_working_week_day('wednesday')
+ job.perform_now(user_id: user.id)
+
+ new_due_dates = WorkPackage.order(:id).pluck(:due_date)
+ expect(new_due_dates).to all(eq(Date.parse('Mon 2022-08-29')))
+ end
+
+ xit 'updates followers if needed' do
+ work_package = create(:work_package,
+ ignore_non_working_days: false,
+ start_date: Date.parse('Mon 2022-08-22'),
+ duration: 5)
+ follower = create(:work_package,
+ ignore_non_working_days: true,
+ start_date: Date.parse('Sat 2022-08-27'),
+ duration: 2)
+ create(:follows_relation, from: follower, to: work_package)
+
+ set_non_working_week_day('wednesday')
+ job.perform_now(user_id: user.id)
+
+ follower.reload
+ work_package.reload
+ expect(follower.start_date).not_to eq(Date.parse('Sat 2022-08-27'))
+ expect(follower.start_date).to eq(work_package.due_date + 1)
+ expect(follower.due_date).to eq(work_package.due_date + 2)
+ end
+end