diff options
Diffstat (limited to 'app/services/work_items/create_and_link_service.rb')
-rw-r--r-- | app/services/work_items/create_and_link_service.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/app/services/work_items/create_and_link_service.rb b/app/services/work_items/create_and_link_service.rb new file mode 100644 index 00000000000..534d220a846 --- /dev/null +++ b/app/services/work_items/create_and_link_service.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module WorkItems + # Create and link operations are not run inside a transaction in this class + # because CreateFromTaskService also creates a transaction. + # This class should always be run inside a transaction as we could end up with + # new work items that were never associated with other work items as expected. + class CreateAndLinkService + def initialize(project:, current_user: nil, params: {}, spam_params:, link_params: {}) + @create_service = CreateService.new( + project: project, + current_user: current_user, + params: params, + spam_params: spam_params + ) + @project = project + @current_user = current_user + @link_params = link_params + end + + def execute + create_result = @create_service.execute + return create_result if create_result.error? + + work_item = create_result[:work_item] + return ::ServiceResponse.success(payload: payload(work_item)) if @link_params.blank? + + result = IssueLinks::CreateService.new(work_item, @current_user, @link_params).execute + + if result[:status] == :success + ::ServiceResponse.success(payload: payload(work_item)) + else + ::ServiceResponse.error(message: result[:message], http_status: 404) + end + end + + private + + def payload(work_item) + { work_item: work_item } + end + end +end |