2022-08-19 19:59:13 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for update quantity of order item
|
|
|
|
class Cart::SetQuantityService
|
|
|
|
def call(order, orderable, quantity = nil)
|
|
|
|
return order if quantity.to_i.zero?
|
|
|
|
|
2022-09-13 16:36:44 +02:00
|
|
|
quantity = orderable.quantity_min > quantity.to_i ? orderable.quantity_min : quantity.to_i
|
|
|
|
|
2022-08-20 18:47:15 +02:00
|
|
|
raise Cart::OutStockError if quantity.to_i > orderable.stock['external']
|
2022-08-19 19:59:13 +02:00
|
|
|
|
2023-01-09 17:36:11 +01:00
|
|
|
item = order.order_items.find_by(orderable_type: orderable.class.name, orderable_id: orderable.id)
|
2022-08-19 19:59:13 +02:00
|
|
|
|
|
|
|
raise ActiveRecord::RecordNotFound if item.nil?
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2022-08-20 18:47:15 +02:00
|
|
|
item.update(quantity: quantity.to_i)
|
2022-10-14 10:52:24 +02:00
|
|
|
Cart::UpdateTotalService.new.call(order)
|
2022-08-19 19:59:13 +02:00
|
|
|
order.save
|
|
|
|
end
|
|
|
|
order.reload
|
|
|
|
end
|
|
|
|
end
|