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

Initial implementation of adding the cause for changed working days

Klaus Zanders3374d4bb311d
diff --git a/app/models/journal.rb b/app/models/journal.rb
index d9a8a5dbd30..1f82ef75c99 100644
--- a/app/models/journal.rb
+++ b/app/models/journal.rb
@@ -30,6 +30,8 @@ class Journal < ApplicationRecord
self.table_name = 'journals'
self.ignored_columns += ['activity_type']
+ WorkingDayUpdate = Struct.new(:working_days, :non_working_days, keyword_init: true)
+
include ::JournalChanges
include ::JournalFormatter
include ::Acts::Journalized::FormatHooks
@@ -52,11 +54,12 @@ class Journal < ApplicationRecord
# Attributes related to the cause are stored in a JSONB column so we can easily add new relations and related
# attributes without a heavy database migration. Fields will be prefixed with `cause_` but are stored in the JSONB
# hash without that prefix
- store_accessor :cause, %i[type work_package_id], prefix: true
+ store_accessor :cause, %i[type work_package_id changed_days], prefix: true
VALID_CAUSE_TYPES = %w[
work_package_predecessor_changed_times
work_package_successor_changed_times
work_package_parent_changed_times
+ working_days_changed
].freeze
# Make sure each journaled model instance only has unique version ids
diff --git a/app/services/journals/create_service.rb b/app/services/journals/create_service.rb
index 7f4ceab70e2..01f0f750cb6 100644
--- a/app/services/journals/create_service.rb
+++ b/app/services/journals/create_service.rb
@@ -634,7 +634,7 @@ module Journals
end
def same_cause?(predecessor, cause)
- predecessor.cause == cause
+ (predecessor.cause.blank? && cause.blank?) || predecessor.cause == cause
end
def log_journal_creation(predecessor)
diff --git a/app/services/work_packages/set_schedule_service.rb b/app/services/work_packages/set_schedule_service.rb
index e846266a76f..951de6214b3 100644
--- a/app/services/work_packages/set_schedule_service.rb
+++ b/app/services/work_packages/set_schedule_service.rb
@@ -180,12 +180,12 @@ class WorkPackages::SetScheduleService
end
def assign_cause_for_journaling(work_package, relation)
- return if initiated_by.nil?
+ return {} if initiated_by.nil?
if initiated_by.is_a?(WorkPackage)
assign_cause_initiated_by_work_package(work_package, relation)
- elsif initiated_by.is_a?(NonWorkingDay)
- assign_cause_initiated_by_non_working_day(work_package, relation)
+ elsif initiated_by.is_a?(Journal::WorkingDayUpdate)
+ assign_cause_initiated_by_changed_working_days(work_package)
end
end
@@ -202,5 +202,10 @@ class WorkPackages::SetScheduleService
}
end
- def assign_cause_initiated_by_non_working_day(work_package, relation); end
+ def assign_cause_initiated_by_changed_working_days(work_package)
+ work_package.journal_cause = {
+ type: 'working_days_changed',
+ changed_days: initiated_by.to_h
+ }
+ 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 2a262735ece..fe700cc8152 100644
--- a/app/workers/work_packages/apply_working_days_change_job.rb
+++ b/app/workers/work_packages/apply_working_days_change_job.rb
@@ -34,30 +34,39 @@ class WorkPackages::ApplyWorkingDaysChangeJob < ApplicationJob
user = User.find(user_id)
User.execute_as user do
+ wd_update = Journal::WorkingDayUpdate.new(
+ working_days: changed_days(previous_working_days),
+ non_working_days: changed_non_working_dates(previous_non_working_days)
+ )
+
updated_work_package_ids = collect_id_for_each(applicable_work_package(previous_working_days,
previous_non_working_days)) do |work_package|
- apply_change_to_work_package(user, work_package)
- end
- updated_work_package_ids += collect_id_for_each(applicable_predecessor(updated_work_package_ids)) do |predecessor|
- apply_change_to_predecessor(user, predecessor)
+ apply_change_to_work_package(user, work_package, wd_update)
end
- set_journal_notice(updated_work_package_ids, previous_working_days, previous_non_working_days)
+ applicable_predecessor(updated_work_package_ids).each do |predecessor|
+ apply_change_to_predecessor(user, predecessor, wd_update)
+ end
end
end
private
- def apply_change_to_work_package(user, work_package)
+ def apply_change_to_work_package(user, work_package, changed_working_days)
+ cause = {
+ type: "working_days_changed",
+ changed_days: changed_working_days.to_h
+ }
+
WorkPackages::UpdateService
.new(user:, model: work_package, contract_class: EmptyContract)
- .call(duration: work_package.duration) # trigger a recomputation of start and due date
+ .call(duration: work_package.duration, journal_cause: cause) # trigger a recomputation of start and due date
.all_results
end
- def apply_change_to_predecessor(user, predecessor)
+ def apply_change_to_predecessor(user, predecessor, initiated_by)
schedule_result = WorkPackages::SetScheduleService
- .new(user:, work_package: predecessor)
+ .new(user:, work_package: predecessor, initiated_by:)
.call
# The SetScheduleService does not save. It has to be done by the caller.
@@ -102,38 +111,6 @@ class WorkPackages::ApplyWorkingDaysChangeJob < ApplicationJob
.where.not(id: excluded)
end
- def set_journal_notice(updated_work_package_ids, previous_working_days, previous_non_working_days)
- day_changes = changed_days(previous_working_days)
- date_changes = changed_non_working_dates(previous_non_working_days)
- journal_note = journal_notice_text(day_changes, date_changes)
-
- WorkPackage
- .where(id: updated_work_package_ids.uniq)
- .in_batches
- .each_record do |work_package|
- work_package.journal_notes = journal_note
- work_package.save
- end
- end
-
- def journal_notice_text(day_changes, date_changes)
- I18n.with_locale(Setting.default_language) do
- day_changes_messages = day_changes.collect { |day, working| working_day_change_message(day, working) }
- date_changes_messages = date_changes.collect { |date, working| working_date_change_message(date, working) }
- I18n.t(:'working_days.journal_note.changed',
- changes: (day_changes_messages + date_changes_messages).join(', '))
- end
- end
-
- def working_day_change_message(day, working)
- I18n.t(:"working_days.journal_note.days.#{working ? :working : :non_working}",
- day: WeekDay.find_by!(day:).name)
- end
-
- def working_date_change_message(date, working)
- I18n.t(:"working_days.journal_note.dates.#{working ? :working : :non_working}", date:)
- end
-
def collect_id_for_each(scope)
scope.pluck(:id).map do |id|
yield(WorkPackage.find(id)).pluck(:id)