2022-02-17 21:14:40 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module WorkItems
|
|
|
|
class CreateFromTaskService
|
|
|
|
def initialize(work_item:, current_user: nil, work_item_params: {}, spam_params:)
|
|
|
|
@work_item = work_item
|
|
|
|
@current_user = current_user
|
|
|
|
@work_item_params = work_item_params
|
|
|
|
@spam_params = spam_params
|
|
|
|
@errors = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
transaction_result = ApplicationRecord.transaction do
|
|
|
|
create_and_link_result = CreateAndLinkService.new(
|
|
|
|
project: @work_item.project,
|
|
|
|
current_user: @current_user,
|
|
|
|
params: @work_item_params.slice(:title, :work_item_type_id),
|
|
|
|
spam_params: @spam_params,
|
2022-07-14 12:08:33 +00:00
|
|
|
link_params: { parent_work_item: @work_item }
|
2022-02-17 21:14:40 +00:00
|
|
|
).execute
|
|
|
|
|
|
|
|
if create_and_link_result.error?
|
|
|
|
@errors += create_and_link_result.errors
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
|
|
|
|
replacement_result = TaskListReferenceReplacementService.new(
|
|
|
|
work_item: @work_item,
|
2022-07-15 15:15:01 +00:00
|
|
|
current_user: @current_user,
|
2022-02-17 21:14:40 +00:00
|
|
|
work_item_reference: create_and_link_result[:work_item].to_reference,
|
|
|
|
line_number_start: @work_item_params[:line_number_start],
|
|
|
|
line_number_end: @work_item_params[:line_number_end],
|
|
|
|
title: @work_item_params[:title],
|
|
|
|
lock_version: @work_item_params[:lock_version]
|
|
|
|
).execute
|
|
|
|
|
|
|
|
if replacement_result.error?
|
|
|
|
@errors += replacement_result.errors
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
|
|
|
|
create_and_link_result
|
|
|
|
end
|
|
|
|
|
|
|
|
return transaction_result if transaction_result
|
|
|
|
|
|
|
|
::ServiceResponse.error(message: @errors, http_status: 422)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|