Class: SpreeCmCommissioner::RoutePrices::UpdatePriceRange

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/route_prices/update_price_range.rb

Instance Method Summary collapse

Instance Method Details

#call(route:, product:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/spree_cm_commissioner/route_prices/update_price_range.rb', line 18

def call(route:, product:)
  route_prices = route.prices.index_by(&:currency)
  product_prices = Spree::Price.joins(:variant).merge(product.variants_including_master).where(deleted_at: nil).index_by(&:currency)
  new_route_prices = product_prices.map do |currency, price|
    existing_route_price = route_prices[currency]

    if existing_route_price.present? && existing_route_price.amount == price.amount
      existing_route_price
    else
      SpreeCmCommissioner::RoutePrice.find_or_create_by!(route: route, price: price, price_type: :min_price)
    end
  end
  success(route_prices: new_route_prices)
rescue StandardError => e
  failure(nil, e.message)
end