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 variant.back_in_stock when variant changes to be in stock.
Sebastian Palma9c22062781c7
diff --git a/api/app/models/spree/api/webhooks/stock_item_decorator.rb b/api/app/models/spree/api/webhooks/stock_item_decorator.rbindex a98a2a6b88..a28ac09fed 100644--- a/api/app/models/spree/api/webhooks/stock_item_decorator.rb+++ b/api/app/models/spree/api/webhooks/stock_item_decorator.rb@@ -3,11 +3,20 @@ module Spreemodule Webhooksmodule StockItemDecoratordef self.prepended(base)+ base.around_save :queue_webhooks_requests_for_variant_back_in_stock!base.around_save :queue_webhooks_requests_for_variant_backorderable!endprivate+ def queue_webhooks_requests_for_variant_back_in_stock!+ was_out_of_stock = !variant_in_stock?+ yield+ if was_out_of_stock && variant_in_stock?+ variant.queue_webhooks_requests!('variant.back_in_stock')+ end+ end+def queue_webhooks_requests_for_variant_backorderable!was_out_of_stock = !variant_in_stock?was_not_backorderable = !variant_backorderable?@@ -24,16 +33,12 @@ module Spree# rewriting `variant.in_stock?` as is currently being cacheddef variant_in_stock?- stock_items.sum(:count_on_hand) > 0+ Spree::Variant.in_stock.exists?(id: variant.id)end# rewriting `variant.backorderable?` as is currently being cacheddef variant_backorderable?- stock_items.any?(&:backorderable)- end-- def stock_items- variant.stock_items.with_active_stock_location+ Spree::Variant.backorderable.exists?(id: variant.id)endendenddiff --git a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rbindex d797b6d0d6..cb2479e7ed 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@@ -1,11 +1,56 @@require 'spec_helper'describe Spree::Api::Webhooks::StockItemDecorator do- describe 'emitting variant.backorderable' do- let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant.reload).serializable_hash.to_json }- let(:variant) { create(:variant) }- let(:stock_item) { variant.stock_items.first }+ let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant.reload).serializable_hash.to_json }+ let(:variant) { create(:variant) }+ let(:stock_item) { variant.stock_items.first }++ let!(:images) { create_list(:image, 2) }+ describe 'emitting variant.back_in_stock' do+ context 'when variant was out of stock' do+ before do+ variant.stock_items.each do |stock_item|+ stock_item.set_count_on_hand(0)+ end+ end++ context 'when variant changes to be in stock' do+ it do+ expect do+ variant.stock_items.first.update(backorderable: true)+ Timecop.freeze { variant.stock_items.first.set_count_on_hand(1) }+ end.to emit_webhook_event('variant.back_in_stock')+ end+ end++ context 'when variant does not change to be in stock' do+ it do+ expect do+ Timecop.freeze { variant.stock_items.first.update(backorderable: true) }+ end.not_to emit_webhook_event('variant.back_in_stock')+ end+ end+ end++ context 'when variant was in stock' do+ before do+ variant.stock_items.each do |stock_item|+ stock_item.set_count_on_hand(1)+ end+ end++ it do+ expect do+ Timecop.freeze do+ variant.stock_items.first.update(backorderable: true)+ end+ end.not_to emit_webhook_event('variant.back_in_stock')+ end+ end+ end++ describe 'emitting variant.backorderable' dobefore do# makes sure variant.backorderable == falsestock_item.update(backorderable: false)