Sha256: 6ad5ff60e098a2f8294396c255da8ac08da8b55502b6fcb300d53a170f400c9a

Contents?: true

Size: 1.91 KB

Versions: 26

Compression:

Stored size: 1.91 KB

Contents

module Spree
  module Api
    module Webhooks
      module StockMovementDecorator
        def self.prepended(base)
          base.around_save :queue_webhooks_requests_for_variant_out_of_stock!
          base.around_save :queue_webhooks_requests_for_variant_back_in_stock!
          base.around_save :queue_webhooks_requests_for_product_out_of_stock!
          base.around_save :queue_webhooks_requests_for_product_back_in_stock!
        end

        private

        def queue_webhooks_requests_for_variant_out_of_stock!
          variant_in_stock_before_update = variant.in_stock_or_backorderable?
          yield
          if variant_in_stock_before_update && !variant.in_stock_or_backorderable?
            reload
            stock_item.variant.queue_webhooks_requests!('variant.out_of_stock')
          end
        end

        def queue_webhooks_requests_for_variant_back_in_stock!
          variant_was_out_of_stock = !variant.in_stock_or_backorderable?
          yield
          if variant_was_out_of_stock && variant.in_stock_or_backorderable?
            reload
            variant.queue_webhooks_requests!('variant.back_in_stock')
          end
        end

        def queue_webhooks_requests_for_product_back_in_stock!
          product_was_out_of_stock = !product.any_variant_in_stock_or_backorderable?
          yield
          if product_was_out_of_stock && product.any_variant_in_stock_or_backorderable?
            product.queue_webhooks_requests!('product.back_in_stock')
          end
        end

        def queue_webhooks_requests_for_product_out_of_stock!
          product_was_in_stock = product.any_variant_in_stock_or_backorderable?
          yield
          if product_was_in_stock && !product.any_variant_in_stock_or_backorderable?
            product.queue_webhooks_requests!('product.out_of_stock')
          end
        end
      end
    end
  end
end

Spree::StockMovement.prepend(Spree::Api::Webhooks::StockMovementDecorator)

Version data entries

26 entries across 26 versions & 3 rubygems

Version Path
spree_api-4.8.3 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.8.2 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.7.3 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.6.6 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.5.5 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.4.1 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.5.4 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.6.5 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.7.2 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.7.1 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.6.4 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.7.0 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.6.3 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.6.2 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.6.1 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.6.0 app/models/spree/api/webhooks/stock_movement_decorator.rb
spree_api-4.5.3 app/models/spree/api/webhooks/stock_movement_decorator.rb
atkhayar-dev-spree-api-0.0.1 app/models/spree/api/webhooks/stock_movement_decorator.rb
atkhayar-dev-spree-api-0.0.0 app/models/spree/api/webhooks/stock_movement_decorator.rb
atkhayar-spree-dev-api-0.0.0 app/models/spree/api/webhooks/stock_movement_decorator.rb