-
-
Notifications
You must be signed in to change notification settings - Fork 573
Expand file tree
/
Copy pathitem_update_service.rb
More file actions
34 lines (29 loc) · 819 Bytes
/
item_update_service.rb
File metadata and controls
34 lines (29 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true
class ItemUpdateService
attr_reader :item, :params, :request_unit_ids
def initialize(item:, params:, request_unit_ids: [])
@item = item
@request_unit_ids = request_unit_ids
@params = params
end
def call
ActiveRecord::Base.transaction do
item.update!(params)
update_kit_value
if Flipper.enabled?(:enable_packs)
item.sync_request_units!(request_unit_ids)
end
end
Result.new(value: item)
rescue => e
Result.new(error: e)
end
private
def update_kit_value
return unless item.kit
kit_value_in_cents = item.kit.items.reduce(0) do |sum, i|
sum + i.value_in_cents.to_i * item.kit.line_items.find_by(item_id: i.id).quantity.to_i
end
item.kit.update!(value_in_cents: kit_value_in_cents)
end
end