Class: WorkItems::DeleteTaskService

Inherits:
Object
  • Object
show all
Defined in:
app/services/work_items/delete_task_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(work_item:, current_user: nil, task_params: {}, lock_version:) ⇒ DeleteTaskService

Returns a new instance of DeleteTaskService.


5
6
7
8
9
10
11
12
# File 'app/services/work_items/delete_task_service.rb', line 5

def initialize(work_item:, current_user: nil, task_params: {}, lock_version:)
  @work_item = work_item
  @current_user = current_user
  @task_params = task_params
  @lock_version = lock_version
  @task = task_params[:task]
  @errors = []
end

Instance Method Details

#executeObject


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/work_items/delete_task_service.rb', line 14

def execute
  transaction_result = ::WorkItem.transaction do
    replacement_result = TaskListReferenceRemovalService.new(
      work_item: @work_item,
      task: @task,
      line_number_start: @task_params[:line_number_start],
      line_number_end: @task_params[:line_number_end],
      lock_version: @lock_version,
      current_user: @current_user
    ).execute

    break ::ServiceResponse.error(message: replacement_result.errors, http_status: 422) if replacement_result.error?

    delete_result = ::WorkItems::DeleteService.new(
      project: @task.project,
      current_user: @current_user
    ).execute(@task)

    if delete_result.error?
      @errors += delete_result.errors
      raise ActiveRecord::Rollback
    end

    delete_result
  end

  return transaction_result if transaction_result

  ::ServiceResponse.error(message: @errors, http_status: 422)
end