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
Update variant.backorderable logic.
Sebastian Palma7a518ba898d1
diff --git a/api/app/models/spree/api/webhooks/stock_item_decorator.rb b/api/app/models/spree/api/webhooks/stock_item_decorator.rbindex b27c7d1ace..23e81d6ea4 100644--- a/api/app/models/spree/api/webhooks/stock_item_decorator.rb+++ b/api/app/models/spree/api/webhooks/stock_item_decorator.rb@@ -9,25 +9,15 @@ module Spreeprivatedef queue_webhooks_requests_for_variant_backorderable!- was_out_of_stock = !variant_in_stock?+ was_out_of_stock = !variant.full_in_stock?was_not_backorderable = !variant_backorderable?yieldtouch # changes must be reflected before instantiating the serializer- return unless emits_webhook_event?(was_not_backorderable, was_out_of_stock)-- variant.queue_webhooks_requests!('variant.backorderable')- end-- def emits_webhook_event?(was_not_backorderable, was_out_of_stock)- was_out_of_stock && was_not_backorderable && variant_in_stock? && variant_backorderable?- end-- # rewriting `variant.in_stock?` as is currently being cached- def variant_in_stock?- Spree::Variant.in_stock.exists?(id: variant.id)+ if was_out_of_stock && was_not_backorderable && variant_backorderable?+ variant.queue_webhooks_requests!('variant.backorderable')+ endend- # rewriting `variant.backorderable?` as is currently being cacheddef variant_backorderable?Spree::Variant.backorderable.exists?(id: variant.id)enddiff --git a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb b/api/app/models/spree/api/webhooks/stock_movement_decorator.rbindex 90bd96a9ab..c89785503c 100644--- a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb+++ b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb@@ -6,6 +6,7 @@ module Spreebase.around_save :queue_webhooks_requests_for_variant_back_in_stock!end+ # [TODO]: update, this remains as it wasdef update_stock_item_quantityvariant_in_stock_before_update = stock_item.variant.in_stock?super@@ -15,12 +16,10 @@ module Spreeprivate- delegate :id, to: :variant, prefix: true-def queue_webhooks_requests_for_variant_back_in_stock!- variant_was_out_of_stock = !variant_in_stock?+ variant_was_out_of_stock = !variant.full_in_stock?yield- if variant_was_out_of_stock && variant_in_stock?+ if variant_was_out_of_stock && variant.full_in_stock?variant.queue_webhooks_requests!('variant.back_in_stock')endend@@ -28,14 +27,6 @@ module Spreedef variantstock_item.variantend-- def 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- SQL- endendendenddiff --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 d791f5c5d8..ba274e330d 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@@ -4,163 +4,82 @@ describe Spree::Api::Webhooks::StockItemDecorator dolet(: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(:stock_location) { variant.stock_locations.first }describe 'emitting variant.backorderable' do- before do- # makes sure variant.backorderable == false- stock_item.update(backorderable: false)- end-- context 'when creating a variant stock item' do- context 'when variant has no stock items' do- before { variant.stock_items.delete_all }-- context 'when variant changes to be in stock' do- context 'when variant changes to be backorderable' do- it do- expect do- variant.reload- Timecop.freeze do- create(:stock_item, variant: variant, backorderable: true, count_on_hand: 1)- end- end.to emit_webhook_event('variant.backorderable')- end- end-- context 'when variant does not change to be backorderable' do- it do- expect do- Timecop.freeze do- create(:stock_item, variant: variant, backorderable: false, count_on_hand: 1)- end- end.not_to emit_webhook_event('variant.backorderable')- end- end+ context 'when variant was out of stock' do+ before 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.saveend+ end- context 'when variant does not change to be in stock' do- it do- expect do- Timecop.freeze do- create(:stock_item, variant: variant, backorderable: true, count_on_hand: 0)- end- end.not_to emit_webhook_event('variant.backorderable')- end+ context 'when variant was backorderable' do+ before do+ stock_item.backorderable = trueend- end- context 'when variant has stock items' do- context 'when variant was out of stock' do- context 'when variant changes to be in stock' do- context 'when variant changes to be backorderable' do- it do- expect do- Timecop.freeze do- create(:stock_item, variant: variant, backorderable: true, count_on_hand: 1)- end- end.to emit_webhook_event('variant.backorderable')- end+ it do+ expect do+ Timecop.freeze do+ stock_item.saveend+ end.not_to emit_webhook_event('variant.backorderable')+ end+ end- context 'when variant does not change to be backorderable' do- it do- expect do- Timecop.freeze do- create(:stock_item, variant: variant, backorderable: false, count_on_hand: 1)- end- end.not_to emit_webhook_event('variant.backorderable')- end- end- end+ context 'when variant was not backorderable' do+ before { variant.stock_items.update_all(backorderable: false) }- context 'when variant does not change to be in stock' do- it do- expect do- Timecop.freeze do- create(:stock_item, variant: variant, backorderable: true, count_on_hand: 0)- end- end.not_to emit_webhook_event('variant.backorderable')- end+ context 'when variant is not set as backorderable' do+ before do+ stock_item.backorderable = falseend- end-- context 'when variant was not out of stock' do- before { variant.stock_items.update_all(count_on_hand: 1, backorderable: true) }it doexpect doTimecop.freeze do- create(:stock_item, variant: variant, backorderable: true, count_on_hand: 1)+ stock_item.saveendend.not_to emit_webhook_event('variant.backorderable')endend- end- end-- context 'when updating a variant stock item' do- context 'when variant was out of stock' do- context 'when variant changes to be in stock' do- context 'when variant changes to backorderable' do- before { variant.stock_items.update_all(backorderable: false) }-- it do- expect do- Timecop.freeze do- stock_item.update(backorderable: true, count_on_hand: 1)- end- end.to emit_webhook_event('variant.backorderable')- end- end-- context 'when variant does not change to backorderable' do- before { variant.stock_items.update_all(backorderable: false) }- it do- expect do- Timecop.freeze do- stock_item.update(backorderable: false, count_on_hand: 1)- end- end.not_to emit_webhook_event('variant.backorderable')- end+ context 'when variant is set as backorderable' do+ before do+ stock_item.backorderable = trueend- context 'when variant was already backorderable' do- before { stock_item.update(backorderable: true) }-- it do- expect do- Timecop.freeze do- stock_item.update(backorderable: true, count_on_hand: 1)- end- end.not_to emit_webhook_event('variant.backorderable')- end- end- end-- context 'when variant does not change to be in stock' do- before { variant.stock_items.update_all(backorderable: false) }-it doexpect doTimecop.freeze do- stock_item.update(backorderable: true, count_on_hand: 0)+ stock_item.saveend- end.not_to emit_webhook_event('variant.backorderable')+ end.to emit_webhook_event('variant.backorderable')endendend+ end- context 'when variant was not out of stock' do- before { stock_item.set_count_on_hand(1) }-- it do- expect do- Timecop.freeze do- stock_item.update(backorderable: true)- end- end.not_to emit_webhook_event('variant.backorderable')+ context 'when variant was not out of stock' do+ before 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.saveend+ stock_item.backorderable = true+ end++ it do+ expect do+ Timecop.freeze do+ stock_item.save+ end+ end.not_to emit_webhook_event('variant.backorderable')endendenddiff --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 e4149e38b4..c25e3e20ba 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@@ -18,7 +18,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator doTimecop.freeze dovariant.stock_items.update_all(backorderable: false)stock_location.stock_movements.new.tap do |stock_movement|- stock_movement.quantity = 1+ stock_movement.quantity = 1 # does make it to be in stockstock_movement.stock_item = stock_location.set_up_stock_item(variant)stock_movement.saveend@@ -33,7 +33,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator doTimecop.freeze dovariant.stock_items.update_all(backorderable: false)stock_location.stock_movements.new.tap do |stock_movement|- stock_movement.quantity = 0+ stock_movement.quantity = 0 # does not make it to be in stockstock_movement.stock_item = stock_location.set_up_stock_item(variant)stock_movement.saveenddiff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 11936b5224..1f963048c7 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -91,6 +91,20 @@ module Spree)end+ # [TODO]: test+ scope :full_in_stock, -> do+ joins(:stock_items).where(<<~SQL)+ #{Spree::StockItem.table_name}.count_on_hand > 0 OR+ #{Spree::Variant.table_name}.track_inventory = FALSE OR+ #{Spree::StockItem.table_name}.backorderable = TRUE+ SQL+ end++ # [TODO]: test+ def full_in_stock?+ Spree::Variant.full_in_stock.exists?(id: id)+ end+scope :not_deleted, -> { where("#{Spree::Variant.quoted_table_name}.deleted_at IS NULL") }scope :for_currency_and_available_price_amount, ->(currency = nil) do