1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-19 08:52:25 +01:00
fab-manager/app/services/cart/set_quantity_service.rb
2022-08-26 10:46:30 +02:00

23 lines
639 B
Ruby

# 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?
raise Cart::OutStockError if quantity.to_i > orderable.stock['external']
item = order.order_items.find_by(orderable: orderable)
raise ActiveRecord::RecordNotFound if item.nil?
different_quantity = quantity.to_i - item.quantity
order.total += (orderable.amount * different_quantity)
ActiveRecord::Base.transaction do
item.update(quantity: quantity.to_i)
order.save
end
order.reload
end
end