2022-03-02 08:16:31 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module WorkItems
|
2022-08-13 15:12:31 +05:30
|
|
|
class CreateService < Issues::CreateService
|
2022-04-04 11:22:00 +05:30
|
|
|
include ::Services::ReturnServiceResponses
|
2022-08-13 15:12:31 +05:30
|
|
|
include WidgetableService
|
2022-04-04 11:22:00 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
def initialize(project:, current_user: nil, params: {}, spam_params:, widget_params: {})
|
|
|
|
super(
|
2022-03-02 08:16:31 +05:30
|
|
|
project: project,
|
|
|
|
current_user: current_user,
|
|
|
|
params: params,
|
|
|
|
spam_params: spam_params,
|
|
|
|
build_service: ::WorkItems::BuildService.new(project: project, current_user: current_user, params: params)
|
|
|
|
)
|
2022-08-13 15:12:31 +05:30
|
|
|
@widget_params = widget_params
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2022-04-04 11:22:00 +05:30
|
|
|
unless @current_user.can?(:create_work_item, @project)
|
|
|
|
return error(_('Operation not allowed'), :forbidden)
|
|
|
|
end
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
work_item = super
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
if work_item.valid?
|
|
|
|
success(payload(work_item))
|
|
|
|
else
|
|
|
|
error(work_item.errors.full_messages, :unprocessable_entity, pass_back: payload(work_item))
|
|
|
|
end
|
2022-08-13 15:12:31 +05:30
|
|
|
rescue ::WorkItems::Widgets::BaseService::WidgetError => e
|
|
|
|
error(e.message, :unprocessable_entity)
|
|
|
|
end
|
|
|
|
|
|
|
|
def transaction_create(work_item)
|
|
|
|
super.tap do |save_result|
|
|
|
|
if save_result
|
|
|
|
execute_widgets(work_item: work_item, callback: :after_create_in_transaction,
|
|
|
|
widget_params: @widget_params)
|
|
|
|
end
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def payload(work_item)
|
|
|
|
{ work_item: work_item }
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|