Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Emit product.back_in_stock.
Sebastian Palmacd7caaab58b7
diff --git a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb b/api/app/models/spree/api/webhooks/stock_movement_decorator.rbindex 8001fa58c2..14897c61d9 100644--- a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb+++ b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb@@ -4,6 +4,7 @@ module Spreemodule StockMovementDecoratordef self.prepended(base)base.around_save :queue_webhooks_requests_for_variant_back_in_stock!+ base.around_save :queue_webhooks_requests_for_product_back_in_stock!end# [TODO]: update, this remains as it was@@ -16,6 +17,9 @@ module Spreeprivate+ delegate :variant, to: :stock_item+ delegate :product, to: :variant+def queue_webhooks_requests_for_variant_back_in_stock!variant_was_out_of_stock = !variant.in_stock_or_backorderable?yield@@ -24,8 +28,17 @@ module Spreeendend- def variant- stock_item.variant+ def queue_webhooks_requests_for_product_back_in_stock!+ product_was_out_of_stock = !product_full_in_stock?+ yield+ if product_was_out_of_stock && product_full_in_stock?+ product.queue_webhooks_requests!('product.back_in_stock')+ end+ end++ def product_full_in_stock?+ Spree::Product.joins(:variants).merge(Spree::Variant.full_in_stock).+ exists?(id: product.id)endendenddiff --git a/api/lib/spree/api/testing_support/matchers/webhooks.rb b/api/lib/spree/api/testing_support/matchers/webhooks.rbindex a418bee0e0..0fd46a34c0 100644--- a/api/lib/spree/api/testing_support/matchers/webhooks.rb+++ b/api/lib/spree/api/testing_support/matchers/webhooks.rb@@ -50,7 +50,7 @@ RSpec::Matchers.define :emit_webhook_event do |event_to_emit|block_body_def = block_body_definition(obj_method)"Expected that executing `#{block_body_def}` emits the `#{event_to_emit}` Webhook event.\n" \"Check that `#{block_body_def}` does implement `queue_webhooks_requests!` for " \- "`#{event_to_emit}` with the following body: #{body}."+ "`#{event_to_emit}` with the following body: \n\n#{body}."endfailure_message_when_negated do |obj_method|diff --git a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rbindex 2acf7a88f9..f8cdae2211 100644--- a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb+++ b/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb@@ -3,8 +3,70 @@ require 'spec_helper'describe Spree::Api::Webhooks::StockMovementDecorator dolet(:stock_item) { create(:stock_item) }let(:stock_location) { variant.stock_locations.first }- let(:stock_movement) { create(:stock_movement, stock_item: stock_item, quantity: movement_quantity) }let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash.to_json }+ let!(:f) { create_list(:image, 2) }++ describe 'emitting product.back_in_stock' do+ let!(:store) { create(:store) }+ let!(:product) { create(:product, stores: [store]) }+ let!(:variant) { create(:variant, product: product) }+ let!(:variant2) { create(:variant, product: product) }+ let(:body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash.to_json }++ before { Spree::StockItem.update_all(backorderable: false) }++ context 'when all product variants are out of stock' do+ context 'when one of the variants is back in stock' do+ subject do+ Timecop.freeze do+ stock_location.stock_movements.new.tap do |stock_movement|+ stock_movement.quantity = 1+ stock_movement.stock_item = stock_location.set_up_stock_item(variant)+ stock_movement.save+ end+ product.reload+ end+ end++ it { expect { subject }.to emit_webhook_event('product.back_in_stock') }+ end++ context 'when none of the variants is back in stock' do+ subject do+ Timecop.freeze do+ stock_location.stock_movements.new.tap do |stock_movement|+ stock_movement.quantity = 0+ stock_movement.stock_item = stock_location.set_up_stock_item(variant)+ stock_movement.save+ end+ product.reload+ end+ end++ it { expect { subject }.not_to emit_webhook_event('product.back_in_stock') }+ end+ end++ context 'when other variant is already in stock' do+ subject do+ stock_location.stock_movements.new.tap do |stock_movement|+ stock_movement.quantity = 100+ stock_movement.stock_item = stock_location.set_up_stock_item(variant)+ stock_movement.save+ end+ end++ before do+ stock_location.stock_movements.new.tap do |stock_movement|+ stock_movement.quantity = 1+ stock_movement.stock_item = stock_location.set_up_stock_item(variant2)+ stock_movement.save+ end+ end++ it { expect { subject }.not_to emit_webhook_event('product.back_in_stock') }+ end+ enddescribe 'emitting variant.back_in_stock' dolet(:variant) { create(:variant, track_inventory: true) }@@ -61,6 +123,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator dodescribe '#update_stock_item_quantity' dosubject { stock_movement }+ let(:stock_movement) { create(:stock_movement, stock_item: stock_item, quantity: movement_quantity) }let(:variant) { stock_item.variant }describe 'when the variant goes out of stock' do