Class: WorkItems::BulkMoveService

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

Instance Method Summary collapse

Constructor Details

#initialize(current_user:, work_item_ids:, source_namespace:, target_namespace:) ⇒ BulkMoveService

Returns a new instance of BulkMoveService.



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

def initialize(current_user:, work_item_ids:, source_namespace:, target_namespace:)
  @current_user = current_user
  @work_item_ids = work_item_ids
  @source_namespace = source_namespace
  @target_namespace = target_namespace
end

Instance Method Details

#executeObject



12
13
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
44
45
46
# File 'app/services/work_items/bulk_move_service.rb', line 12

def execute
  if @target_namespace&.user_namespace?
    return response(:error, message: "User namespaces are not supported as target namespaces.")
  end

  # If the source namespace is a project and the target is a group, raise an error
  if @source_namespace&.project_namespace? && @target_namespace&.group_namespace?
    return response(:error, message: "Cannot move work items from projects to groups.")
  end

  unless @current_user.can?(:create_work_item, @target_namespace)
    return response(:error, message: "You do not have permission to move items to this namespace.")
  end

  moved_work_items =
    scoped_work_items
      .find_each(batch_size: 100) # rubocop:disable CodeReuse/ActiveRecord -- Implementation would be identical in model
      .filter_map do |work_item|
        next unless can_move_work_item?(work_item)

        begin
          move_result = ::WorkItems::DataSync::MoveService.new(
            work_item: work_item,
            current_user: @current_user,
            target_namespace: @target_namespace
          ).execute

          work_item if move_result.success? && move_result[:work_item].present?
        rescue StandardError
          nil
        end
      end

  response(:success, moved_count: moved_work_items.count)
end