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

[#45473] Update the WorkPackages::ApplyWorkingDaysChangeJob to take NonWorkingDays into account.

Dombi Attila2e12903bdc31
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index a3422cdcab2..a8ca34007da 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -120,7 +120,7 @@ class WorkPackage < ApplicationRecord
where(author_id: author.id)
}
- scopes :covering_days_of_week,
+ scopes :covering_dates_and_days_of_week,
:for_scheduling,
:include_derived_dates,
:include_spent_time,
diff --git a/app/models/work_packages/scopes/covering_days_of_week.rb b/app/models/work_packages/scopes/covering_dates_and_days_of_week.rb
similarity index 71%
rename from app/models/work_packages/scopes/covering_days_of_week.rb
rename to app/models/work_packages/scopes/covering_dates_and_days_of_week.rb
index f9c7217e77e..8641c216f0e 100644
--- a/app/models/work_packages/scopes/covering_days_of_week.rb
+++ b/app/models/work_packages/scopes/covering_dates_and_days_of_week.rb
@@ -26,27 +26,29 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module WorkPackages::Scopes::CoveringDaysOfWeek
+module WorkPackages::Scopes::CoveringDatesAndDaysOfWeek
extend ActiveSupport::Concern
using CoreExtensions::SquishSql
class_methods do
- # Fetches all work packages that cover specific days of the week.
+ # Fetches all work packages that cover specific days of the week, and/or specific dates.
#
# The period considered is from the work package start date to the due date.
#
+ # @param dates Date[] An array of the Date objects.
# @param days_of_week number[] An array of the ISO days of the week to
# consider. 1 is Monday, 7 is Sunday.
- def covering_days_of_week(days_of_week)
+ def covering_dates_and_days_of_week(days_of_week: [], dates: [])
days_of_week = Array(days_of_week)
- return none if days_of_week.empty?
+ dates = Array(dates)
+ return none if days_of_week.empty? && dates.empty?
- where("id IN (#{query(days_of_week)})")
+ where("id IN (#{query(days_of_week, dates)})")
end
private
- def query(days_of_week)
+ def query(days_of_week, dates)
sql = <<~SQL.squish
-- select work packages dates with their followers dates
WITH work_packages_with_dates AS (
@@ -68,29 +70,29 @@ module WorkPackages::Scopes::CoveringDaysOfWeek
GREATEST(work_package_start_date, work_package_due_date) AS end_date
FROM work_packages_with_dates
),
- -- expand period into days of the week. Limit to 7 days (more would be useless).
- work_packages_days_of_week AS (
- SELECT id,
- extract(
- isodow
- from generate_series(
- work_packages_periods.start_date,
- LEAST(
- work_packages_periods.start_date + 6,
- work_packages_periods.end_date
- ),
- '1 day'
- )
+ -- expand period into days.
+ work_packages_expaned_periods AS (
+ SELECT id, generate_series(
+ work_packages_periods.start_date,
+ work_packages_periods.end_date,
+ '1 day') AS dates,
+ -- limit to 7 days (more would be useless).
+ extract(isodow from generate_series(
+ work_packages_periods.start_date,
+ LEAST(
+ work_packages_periods.start_date + 6,
+ work_packages_periods.end_date
+ ),
+ '1 day')
) AS dow
FROM work_packages_periods
)
-- select id of work packages covering the given days
- SELECT DISTINCT id
- FROM work_packages_days_of_week
- WHERE dow IN (:days_of_week)
+ SELECT id FROM work_packages_expaned_periods
+ WHERE dow IN (:days_of_week) OR dates IN (:dates)
SQL
- sanitize_sql([sql, { days_of_week: }])
+ sanitize_sql([sql, { days_of_week:, dates: }])
end
end
end
diff --git a/app/workers/work_packages/apply_working_days_change_job.rb b/app/workers/work_packages/apply_working_days_change_job.rb
index 8853338c99c..1a5a38509a4 100644
--- a/app/workers/work_packages/apply_working_days_change_job.rb
+++ b/app/workers/work_packages/apply_working_days_change_job.rb
@@ -72,7 +72,7 @@ class WorkPackages::ApplyWorkingDaysChangeJob < ApplicationJob
changed_days = changed_days(previous_working_days)
WorkPackage
- .covering_days_of_week(changed_days)
+ .covering_dates_and_days_of_week(changed_days)
.order(WorkPackage.arel_table[:start_date].asc.nulls_first,
WorkPackage.arel_table[:due_date].asc)
end
diff --git a/spec/models/work_packages/scopes/covering_dates_and_days_of_week_spec.rb b/spec/models/work_packages/scopes/covering_dates_and_days_of_week_spec.rb
new file mode 100644
index 00000000000..c61f12c558c
--- /dev/null
+++ b/spec/models/work_packages/scopes/covering_dates_and_days_of_week_spec.rb
@@ -0,0 +1,192 @@
+#-- 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.
+#++
+
+require 'rails_helper'
+
+RSpec.describe WorkPackages::Scopes::CoveringDatesAndDaysOfWeek do
+ create_shared_association_defaults_for_work_package_factory
+
+ shared_let(:next_monday) { Date.current.next_occurring(:monday) }
+
+ shared_examples_for 'covering days' do
+ # Construct the keyword arguments for the `#covering_dates_and_days_of_week` method.
+ # It builds the expected day values based on the argument type provided (`:dates`, `:days_of_week`)
+ # and based on the day values provided.
+ # By providing the `expected_days`, the specific tetscase can decide which days are expected.
+ let(:day_args) do
+ ->(*expected_days) do
+ days.keys.reduce(Hash.new { |hsh, key| hsh[key] = [] }) do |day_args, dt|
+ Array(expected_days).each do |day|
+ day_args[dt] << days[dt][day] if days[dt][day]
+ end
+ day_args
+ end
+ end
+ end
+
+ it 'returns work packages having start date or due date being in the given days of week' do
+ schedule =
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ covered1 | XX |
+ covered2 | XX |
+ covered3 | X |
+ covered4 | [ |
+ covered5 | ] |
+ not_covered1 | X |
+ not_covered2 | X |
+ not_covered3 | XX |
+ not_covered4 | |
+ CHART
+
+ expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday]))
+ .to contain_exactly(
+ schedule.work_package("covered1"),
+ schedule.work_package("covered2"),
+ schedule.work_package("covered3"),
+ schedule.work_package("covered4"),
+ schedule.work_package("covered5")
+ )
+ end
+
+ it 'returns work packages having days between start date and due date being in the given days of week' do
+ schedule =
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ covered1 | XXXX |
+ covered2 | XXX |
+ not_covered1 | XX |
+ not_covered2 | X |
+ CHART
+
+ expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday, :wednesday]))
+ .to contain_exactly(
+ schedule.work_package("covered1"),
+ schedule.work_package("covered2")
+ )
+ end
+
+ context 'if work package ignores non working days' do
+ it 'does not returns it' do
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ not_covered | XXXXXXX | working days include weekends
+ CHART
+
+ expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:wednesday]))
+ .to eq([])
+ end
+ end
+
+ it 'does not return work packages having follows relation covering the given days of week' do
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ not_covered1 | X |
+ follower1 | X | follows not_covered1
+ not_covered2 | X |
+ follower2 | X | follows not_covered2
+ CHART
+
+ expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday, :thursday]))
+ .to eq([])
+ end
+
+ it 'does not return work packages having follows relation with delay covering the given days of week' do
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ not_covered1 | X |
+ follower1 | X | follows not_covered1 with delay 3
+ not_covered2 | X |
+ follower2 | X | follows not_covered2 with delay 1
+ CHART
+
+ expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday, :thursday]))
+ .to eq([])
+ end
+
+ it 'accepts a single day of week or an array of days' do
+ schedule =
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ covered | X |
+ not_covered | X |
+ CHART
+
+ single_value = day_args[:tuesday].transform_values { |v| Array(v).first }
+
+ expect(WorkPackage.covering_dates_and_days_of_week(**single_value))
+ .to eq([schedule.work_package("covered")])
+ expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday]))
+ .to eq([schedule.work_package("covered")])
+ expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday, :wednesday]))
+ .to eq([schedule.work_package("covered")])
+ end
+ end
+
+ context 'with the days of week' do
+ let(:days) do
+ {
+ days_of_week: {
+ tuesday: 2,
+ wednesday: 3,
+ thursday: 4
+ }
+ }
+ end
+
+ it_behaves_like 'covering days'
+ end
+
+ context 'with specific dates' do
+ let(:days) do
+ {
+ dates: {
+ tuesday: next_monday.next_occurring(:tuesday),
+ wednesday: next_monday.next_occurring(:wednesday),
+ thursday: next_monday.next_occurring(:thursday)
+ }
+ }
+ end
+
+ it_behaves_like 'covering days'
+ end
+
+ context 'with days of week and specific dates mixed' do
+ let(:days) do
+ {
+ days_of_week: { wednesday: 3 },
+ dates: {
+ tuesday: next_monday.next_occurring(:tuesday),
+ thursday: next_monday.next_occurring(:thursday)
+ }
+ }
+ end
+
+ it_behaves_like 'covering days'
+ end
+end
diff --git a/spec/models/work_packages/scopes/covering_days_of_week_spec.rb b/spec/models/work_packages/scopes/covering_days_of_week_spec.rb
deleted file mode 100644
index 40ab10a5ffb..00000000000
--- a/spec/models/work_packages/scopes/covering_days_of_week_spec.rb
+++ /dev/null
@@ -1,129 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2023 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 'rails_helper'
-
-RSpec.describe WorkPackages::Scopes::CoveringDaysOfWeek do
- create_shared_association_defaults_for_work_package_factory
-
- it 'returns work packages having start date or due date being in the given days of week' do
- schedule =
- create_schedule(<<~CHART)
- days | MTWTFSS |
- covered1 | XX |
- covered2 | XX |
- covered3 | X |
- covered4 | [ |
- covered5 | ] |
- not_covered1 | X |
- not_covered2 | X |
- not_covered3 | XX |
- not_covered4 | |
- CHART
-
- expect(WorkPackage.covering_days_of_week(2))
- .to contain_exactly(
- schedule.work_package("covered1"),
- schedule.work_package("covered2"),
- schedule.work_package("covered3"),
- schedule.work_package("covered4"),
- schedule.work_package("covered5")
- )
- end
-
- it 'returns work packages having days between start date and due date being in the given days of week' do
- schedule =
- create_schedule(<<~CHART)
- days | MTWTFSS |
- covered1 | XXXX |
- covered2 | XXX |
- not_covered1 | XX |
- not_covered2 | X |
- CHART
-
- expect(WorkPackage.covering_days_of_week([2, 3]))
- .to contain_exactly(
- schedule.work_package("covered1"),
- schedule.work_package("covered2")
- )
- end
-
- context 'if work package ignores non working days' do
- it 'does not returns it' do
- create_schedule(<<~CHART)
- days | MTWTFSS |
- not_covered | XXXXXXX | working days include weekends
- CHART
-
- expect(WorkPackage.covering_days_of_week(3))
- .to eq([])
- end
- end
-
- it 'does not return work packages having follows relation covering the given days of week' do
- create_schedule(<<~CHART)
- days | MTWTFSS |
- not_covered1 | X |
- follower1 | X | follows not_covered1
- not_covered2 | X |
- follower2 | X | follows not_covered2
- CHART
-
- expect(WorkPackage.covering_days_of_week([2, 4]))
- .to eq([])
- end
-
- it 'does not return work packages having follows relation with delay covering the given days of week' do
- create_schedule(<<~CHART)
- days | MTWTFSS |
- not_covered1 | X |
- follower1 | X | follows not_covered1 with delay 3
- not_covered2 | X |
- follower2 | X | follows not_covered2 with delay 1
- CHART
-
- expect(WorkPackage.covering_days_of_week([2, 4]))
- .to eq([])
- end
-
- it 'accepts a single day of week or an array of days' do
- schedule =
- create_schedule(<<~CHART)
- days | MTWTFSS |
- covered | X |
- not_covered | X |
- CHART
-
- expect(WorkPackage.covering_days_of_week(2))
- .to eq([schedule.work_package("covered")])
- expect(WorkPackage.covering_days_of_week([2]))
- .to eq([schedule.work_package("covered")])
- expect(WorkPackage.covering_days_of_week([2, 3]))
- .to eq([schedule.work_package("covered")])
- end
-end
diff --git a/spec/services/work_packages/set_schedule_service_working_days_spec.rb b/spec/services/work_packages/set_schedule_service_working_days_spec.rb
index d0df975324e..09deb60dfb9 100644
--- a/spec/services/work_packages/set_schedule_service_working_days_spec.rb
+++ b/spec/services/work_packages/set_schedule_service_working_days_spec.rb
@@ -972,13 +972,6 @@ describe WorkPackages::SetScheduleService, 'working days' do
end
end
- def set_non_working_week_days(*days)
- non_working_days = days.map do |day|
- %w[xxx monday tuesday wednesday thursday friday saturday sunday].index(day.downcase)
- end
- Setting.working_days -= non_working_days
- end
-
context 'when moving forward due to days and predecessor due date now being non-working days' do
let_schedule(<<~CHART)
days | MTWTFSS |