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.

Emit product.backorderable.

Sebastian Palma5e8ef81ff915
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 9732726a3c..b1026d8902 100644
--- a/api/app/models/spree/api/webhooks/stock_item_decorator.rb
+++ b/api/app/models/spree/api/webhooks/stock_item_decorator.rb
@@ -4,6 +4,7 @@ module Spree
module StockItemDecorator
def self.prepended(base)
base.around_save :queue_webhooks_requests_for_variant_backorderable!
+ base.around_save :queue_webhooks_requests_for_product_backorderable!
end
private
@@ -18,6 +19,19 @@ module Spree
end
end
+ def queue_webhooks_requests_for_product_backorderable!
+ product_was_not_backorderable = !product_backorderable?
+ yield
+ if product_was_not_backorderable && product_backorderable?
+ touch
+ variant.product.queue_webhooks_requests!('product.backorderable')
+ end
+ end
+
+ def product_backorderable?
+ Spree::StockItem.exists?(backorderable: true, variant_id: variant.product.variants.ids)
+ 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 ba274e330d..47d930c590 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
@@ -6,6 +6,45 @@ describe Spree::Api::Webhooks::StockItemDecorator do
let(:stock_item) { variant.stock_items.first }
let(:stock_location) { variant.stock_locations.first }
+ describe 'emitting product.backorderable' do
+ subject { Timecop.freeze { stock_item.update(backorderable: backorderable) } }
+
+ let(:body) { Spree::Api::V2::Platform::ProductSerializer.new(product.reload).serializable_hash.to_json }
+ 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) }
+
+ context 'when one of the variants is made backorderable' do
+ let(:backorderable) { true }
+
+ it { expect { subject }.to emit_webhook_event('product.backorderable') }
+ end
+
+ context 'when none of the variants is made backorderable' do
+ let(:backorderable) { false }
+
+ it { expect { subject }.not_to emit_webhook_event('product.backorderable') }
+ end
+ end
+
+ 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 }
+
+ before do
+ Spree::StockItem.update_all(backorderable: false)
+ another_stock_item.update(backorderable: true)
+ end
+
+ it { expect { subject }.not_to emit_webhook_event('product.backorderable') }
+ end
+ end
+ end
+
describe 'emitting variant.backorderable' do
context 'when variant was out of stock' do
before do