2022-09-27 19:44:39 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for refresh amount of order item
|
|
|
|
class Cart::RefreshItemService
|
|
|
|
def call(order, orderable)
|
|
|
|
raise Cart::InactiveProductError unless orderable.is_active
|
|
|
|
|
2023-01-09 17:36:11 +01:00
|
|
|
item = order.order_items.find_by(orderable_type: orderable.class.name, orderable_id: orderable.id)
|
2022-09-27 19:44:39 +02:00
|
|
|
|
|
|
|
raise ActiveRecord::RecordNotFound if item.nil?
|
|
|
|
|
|
|
|
item.amount = orderable.amount || 0
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
item.save
|
2022-10-14 10:52:24 +02:00
|
|
|
Cart::UpdateTotalService.new.call(order)
|
2022-09-27 19:44:39 +02:00
|
|
|
order.save
|
|
|
|
end
|
|
|
|
order.reload
|
|
|
|
end
|
|
|
|
end
|