1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-11 22:24:21 +01:00
fab-manager/app/services/cart/update_total_service.rb

26 lines
559 B
Ruby
Raw Normal View History

# frozen_string_literal: true
# Provides methods for update total of cart
class Cart::UpdateTotalService
2023-01-09 17:36:11 +01:00
# @param order[Order]
def call(order)
total = 0
order.order_items.each do |item|
2023-01-09 17:36:11 +01:00
update_item_price(item)
total += (item.amount * item.quantity) unless item.is_offered
end
order.total = total
order.save
order
end
2023-01-09 17:36:11 +01:00
private
# @param item[OrderItem]
def update_item_price(item)
return unless item.orderable_type.match(/^CartItem::/)
item.update(amount: item.orderable.price[:amount] || 0)
end
end