Class: WorkItems::BulkUpdateService

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

Instance Method Summary collapse

Constructor Details

#initialize(parent:, current_user:, work_item_ids:, attributes: {}) ⇒ BulkUpdateService

Returns a new instance of BulkUpdateService.



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

def initialize(parent:, current_user:, work_item_ids:, attributes: {})
  @parent = parent
  @work_item_ids = work_item_ids
  @current_user = current_user
  @attributes = attributes
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
# File 'app/services/work_items/bulk_update_service.rb', line 12

def execute
  unless @current_user.can?(:"read_#{@parent.to_ability_name}", @parent)
    return ServiceResponse.error(message: "User can't read parent", reason: :authorization)
  end

  non_widget_attributes = @attributes.except(*all_widget_keys)

  updated_work_items = scoped_work_items
    .includes(namespace: [:route]).find_each(batch_size: 100) # rubocop:disable CodeReuse/ActiveRecord -- Implementation would be identical in model
    .filter_map do |work_item|
      next unless @current_user.can?(:update_work_item, work_item)

      widget_params = extract_supported_widget_params(
        work_item.work_item_type,
        @attributes,
        work_item.resource_parent
      )
      # Skip if no applicable widgets for this work item type
      next if widget_params.blank? && non_widget_attributes.blank?

      update_result = WorkItems::UpdateService.new(
        container: work_item.resource_parent,
        widget_params: widget_params,
        params: non_widget_attributes,
        current_user: @current_user
      ).execute(work_item)

      work_item if update_result[:status] == :success
    end
  ServiceResponse.success(payload: { updated_work_item_count: updated_work_items.count })
end