Sha256: 5cabed8f20d309c9793a7907995616a4903f222333f5f3c4d9598ca150429976

Contents?: true

Size: 1.05 KB

Versions: 62

Compression:

Stored size: 1.05 KB

Contents

module Workarea
  class InventoryAdjustment
    attr_reader :cart, :errors

    def initialize(cart)
      @cart = cart
    end

    def perform
      @errors = []

      insufficiencies.each do |sku, quantity_short|
        item = cart.items.detect { |i| i.sku == sku }
        next unless item.present? && quantity_short > 0

        new_quantity = item.quantity - quantity_short

        if new_quantity == 0
          cart.remove_item(item.id)
          @errors << I18n.t('workarea.errors.messages.sku_unavailable', sku: sku)
        else
          cart.update_item(item.id, quantity: new_quantity)
          @errors << I18n.t(
            'workarea.errors.messages.sku_limited_quantity',
            quantity: new_quantity,
            sku: item[:sku]
          )
        end
      end
    end

    private

    def insufficiencies
      @insufficiencies ||= Inventory.find_insufficiencies(serialized_items)
    end

    def serialized_items
      cart.items.inject({}) do |memo, item|
        memo[item.sku] = item.quantity
        memo
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/services/workarea/inventory_adjustment.rb
workarea-core-3.5.26 app/services/workarea/inventory_adjustment.rb
workarea-core-3.4.45 app/services/workarea/inventory_adjustment.rb
workarea-core-3.5.25 app/services/workarea/inventory_adjustment.rb
workarea-core-3.5.23 app/services/workarea/inventory_adjustment.rb
workarea-core-3.4.44 app/services/workarea/inventory_adjustment.rb
workarea-core-3.5.22 app/services/workarea/inventory_adjustment.rb
workarea-core-3.4.43 app/services/workarea/inventory_adjustment.rb
workarea-core-3.5.21 app/services/workarea/inventory_adjustment.rb
workarea-core-3.4.42 app/services/workarea/inventory_adjustment.rb
workarea-core-3.5.20 app/services/workarea/inventory_adjustment.rb
workarea-core-3.4.41 app/services/workarea/inventory_adjustment.rb
workarea-core-3.5.19 app/services/workarea/inventory_adjustment.rb
workarea-core-3.4.40 app/services/workarea/inventory_adjustment.rb
workarea-core-3.5.18 app/services/workarea/inventory_adjustment.rb
workarea-core-3.4.39 app/services/workarea/inventory_adjustment.rb
workarea-core-3.5.17 app/services/workarea/inventory_adjustment.rb
workarea-core-3.4.38 app/services/workarea/inventory_adjustment.rb
workarea-core-3.5.16 app/services/workarea/inventory_adjustment.rb
workarea-core-3.4.37 app/services/workarea/inventory_adjustment.rb