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
Move variant.back_in_stock logic to StockMovement.
Sebastian Palma2385e7b5e21e
diff --git a/api/app/models/spree/api/webhooks/stock_item_decorator.rb b/api/app/models/spree/api/webhooks/stock_item_decorator.rbindex a28ac09fed..b27c7d1ace 100644--- a/api/app/models/spree/api/webhooks/stock_item_decorator.rb+++ b/api/app/models/spree/api/webhooks/stock_item_decorator.rb@@ -3,20 +3,11 @@ 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?diff --git a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb b/api/app/models/spree/api/webhooks/stock_movement_decorator.rbindex f27ac58eac..90bd96a9ab 100644--- a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb+++ b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb@@ -2,17 +2,39 @@ module Spreemodule Apimodule Webhooksmodule StockMovementDecorator+ def self.prepended(base)+ base.around_save :queue_webhooks_requests_for_variant_back_in_stock!+ end+def update_stock_item_quantity- variant_in_stock_before_update = variant_in_stock?+ variant_in_stock_before_update = stock_item.variant.in_stock?supervariant = stock_item.variant- variant.queue_webhooks_requests!('variant.out_of_stock') if variant_in_stock_before_update && !variant_in_stock?+ variant.queue_webhooks_requests!('variant.out_of_stock') if variant_in_stock_before_update && !stock_item.variant.in_stock?endprivate+ delegate :id, to: :variant, prefix: true++ def queue_webhooks_requests_for_variant_back_in_stock!+ variant_was_out_of_stock = !variant_in_stock?+ yield+ if variant_was_out_of_stock && variant_in_stock?+ variant.queue_webhooks_requests!('variant.back_in_stock')+ end+ end++ def variant+ stock_item.variant+ end+def variant_in_stock?- stock_item.variant.in_stock?+ Spree::Variant.joins(:stock_items).where(id: variant_id).where(<<~SQL).exists?+ #{Spree::StockItem.table_name}.count_on_hand > 0 OR+ #{Spree::Variant.table_name}.track_inventory = FALSE OR+ #{Spree::StockItem.table_name}.backorderable = TRUE+ SQLendendenddiff --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 556e59ed10..d791f5c5d8 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@@ -5,49 +5,6 @@ describe Spree::Api::Webhooks::StockItemDecorator dolet(:variant) { create(:variant) }let(:stock_item) { variant.stock_items.first }- 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 == falsediff --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 e8f16f9a05..e4149e38b4 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@@ -1,14 +1,70 @@require 'spec_helper'-describe Spree::StockMovement do+describe Spree::Api::Webhooks::StockMovementDecorator dolet(:stock_item) { create(:stock_item) }- let(:variant) { stock_item.variant }+ 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!(:images) { create_list(:image, 2) }++ describe 'emitting variant.back_in_stock' do+ let(:variant) { create(:variant, track_inventory: true) }++ context 'when stock item was out of stock' do+ context 'when variant changes to be in stock' do+ it do+ expect do+ Timecop.freeze do+ variant.stock_items.update_all(backorderable: false)+ 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+ 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 do+ variant.stock_items.update_all(backorderable: false)+ 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+ end+ end.not_to emit_webhook_event('variant.back_in_stock')+ end+ end+ end++ context 'when variant was in stock' do+ it do+ expect do+ Timecop.freeze do+ # make in_stock? return false based on track_inventory, the easiest case+ variant.update(track_inventory: false)+ stock_location.stock_movements.new.tap do |stock_movement|+ stock_movement.quantity = 2+ stock_movement.stock_item = stock_location.set_up_stock_item(variant)+ stock_movement.save+ end+ end+ end.not_to emit_webhook_event('variant.back_in_stock')+ end+ end+ end+describe '#update_stock_item_quantity' dosubject { stock_movement }+ let(:variant) { stock_item.variant }+describe 'when the variant goes out of stock' dolet(:movement_quantity) { -stock_item.count_on_hand }