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.out_of_stock. (#11527)
seba13550090811
diff --git a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb b/api/app/models/spree/api/webhooks/stock_movement_decorator.rbindex 1ded7fd443..f0520c091d 100644--- a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb+++ b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb@@ -5,6 +5,7 @@ module Spreedef 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@@ -35,6 +36,14 @@ module Spreeproduct.queue_webhooks_requests!('product.back_in_stock')endend++ 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+ endendendenddiff --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 1c2647ce05..892bbf3156 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@@ -5,62 +5,152 @@ describe Spree::Api::Webhooks::StockMovementDecorator dolet(:stock_item) { create(:stock_item) }let(:stock_location) { variant.stock_locations.first }- describe 'emitting product.back_in_stock' do+ describe 'emitting product events' dolet!(: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 }- let(:params) { 'product.back_in_stock' }- before { Spree::StockItem.update_all(backorderable: false) }+ describe 'emitting product.out_of_stock' do+ let(:event_name) { 'product.out_of_stock' }++ 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+ context 'when all product variants are in stock' do+ before do+ product.variants.each do |variant|+ variant.stock_items.each do |stock_item|+ stock_item.set_count_on_hand(1)+ end+ end+ end++ context 'when one of the variants goes out of stock' do+ subject 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++ it { expect { subject }.not_to emit_webhook_event(event_name) }+ end++ context 'when all the variants go out of stock' do+ subject do+ stock_location.stock_movements.new.tap do |stock_movement|+ stock_movement.quantity = -1+ stock_movement.stock_item = stock_location.set_up_stock_item(product.variants[1])+ stock_movement.save+ end+ product.reload+ end++ before do+ # Set one of the two product variants as out of stock+ stock_location.stock_movements.new.tap do |stock_movement|+ stock_movement.quantity = -1+ stock_movement.stock_item = stock_location.set_up_stock_item(product.variants[0])+ stock_movement.save+ end+ end++ it { expect { subject }.to emit_webhook_event(event_name) }+ end+ end++ context 'when only one product variant is in stock' dosubject dostock_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.quantity = -1+ stock_movement.stock_item = stock_location.set_up_stock_item(product.variants[0])stock_movement.saveendproduct.reloadend- it { expect { subject }.to emit_webhook_event(params) }+ before { product.variants[0].stock_items[0].set_count_on_hand(1) }++ it { expect { subject }.to emit_webhook_event(event_name) }end- context 'when none of the variants is back in stock' do+ context 'when no product variant is in stock' dosubject dostock_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.stock_item = stock_location.set_up_stock_item(product.variants[0])stock_movement.saveendproduct.reloadend- it { expect { subject }.not_to emit_webhook_event(params) }+ before do+ product.variants.each do |variant|+ variant.stock_items.each do |stock_item|+ stock_item.set_count_on_hand(0)+ end+ end+ end++ it { expect { subject }.not_to emit_webhook_event(event_name) }endend- 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+ describe 'emitting product.back_in_stock' do+ let(:params) { 'product.back_in_stock' }++ 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+ 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++ it { expect { subject }.to emit_webhook_event(params) }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+ context 'when none of the variants is back in stock' do+ subject 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++ it { expect { subject }.not_to emit_webhook_event(params) }endend- it { expect { subject }.not_to emit_webhook_event(params) }+ 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(params) }+ endendend