Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Verify product was out of stock before emitting product.backorderable.

Sebastian Palma03e1f3d508e5
diff --git a/api/app/models/spree/api/webhooks/stock_item_decorator.rb b/api/app/models/spree/api/webhooks/stock_item_decorator.rb
index b1026d8902..689d48ddb8 100644
--- a/api/app/models/spree/api/webhooks/stock_item_decorator.rb
+++ b/api/app/models/spree/api/webhooks/stock_item_decorator.rb
@@ -20,9 +20,10 @@ module Spree
end
def queue_webhooks_requests_for_product_backorderable!
+ product_was_out_of_stock = !product_full_in_stock?
product_was_not_backorderable = !product_backorderable?
yield
- if product_was_not_backorderable && product_backorderable?
+ if product_was_out_of_stock && product_was_not_backorderable && product_backorderable?
touch
variant.product.queue_webhooks_requests!('product.backorderable')
end
@@ -32,6 +33,11 @@ module Spree
Spree::StockItem.exists?(backorderable: true, variant_id: variant.product.variants.ids)
end
+ def product_full_in_stock?
+ Spree::Product.joins(:variants).merge(Spree::Variant.full_in_stock).
+ exists?(id: product.id)
+ end
+
def variant_backorderable?
variant.stock_items.exists?(backorderable: true)
end
diff --git a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
index 47d930c590..133b51ca3d 100644
--- a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
@@ -13,35 +13,50 @@ describe Spree::Api::Webhooks::StockItemDecorator do
let(:product) { variant.product }
let!(:variant2) { create(:variant, product: product) }
- context 'when none of the product variants is backorderable' do
- before { Spree::StockItem.update_all(backorderable: false) }
+ before { Spree::StockItem.update_all(backorderable: false) }
- context 'when one of the variants is made backorderable' do
- let(:backorderable) { true }
+ context 'when product was out of stock' do
+ context 'when none of the product variants is backorderable' do
+ context 'when one of the variants is made backorderable' do
+ let(:backorderable) { true }
- it { expect { subject }.to emit_webhook_event('product.backorderable') }
- end
+ it { expect { subject }.to emit_webhook_event('product.backorderable') }
+ end
- context 'when none of the variants is made backorderable' do
- let(:backorderable) { false }
+ context 'when none of the variants is made backorderable' do
+ let(:backorderable) { false }
- it { expect { subject }.not_to emit_webhook_event('product.backorderable') }
+ it { expect { subject }.not_to emit_webhook_event('product.backorderable') }
+ end
end
- end
- context 'when other variant is already backorderable' do
- let(:another_stock_item) { variant2.stock_items.first }
+ context 'when other variant is already backorderable' do
+ let(:another_stock_item) { variant2.stock_items.first }
- context 'when other variants is made backorderable' do
- let(:backorderable) { true }
+ context 'when other variant is made backorderable' do
+ let(:backorderable) { true }
- before do
- Spree::StockItem.update_all(backorderable: false)
- another_stock_item.update(backorderable: true)
+ before do
+ another_stock_item.update(backorderable: true)
+ end
+
+ it { expect { subject }.not_to emit_webhook_event('product.backorderable') }
end
+ end
+ end
+
+ context 'when product was in stock' do
+ let(:backorderable) { true }
- it { expect { subject }.not_to emit_webhook_event('product.backorderable') }
+ 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(variant)
+ stock_movement.save
+ end
end
+
+ it { expect { subject }.not_to emit_webhook_event('product.backorderable') }
end
end