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.

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.rb
index 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 Spree
private
def 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?
yield
touch # 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')
+ end
end
- # rewriting `variant.backorderable?` as is currently being cached
def variant_backorderable?
Spree::Variant.backorderable.exists?(id: variant.id)
end
diff --git a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb
index 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 Spree
base.around_save :queue_webhooks_requests_for_variant_back_in_stock!
end
+ # [TODO]: update, this remains as it was
def update_stock_item_quantity
variant_in_stock_before_update = stock_item.variant.in_stock?
super
@@ -15,12 +16,10 @@ module Spree
private
- 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')
end
end
@@ -28,14 +27,6 @@ module Spree
def variant
stock_item.variant
end
-
- 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
- end
end
end
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 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 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(: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.save
end
+ 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 = true
end
- 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.save
end
+ 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 = false
end
- end
-
- context 'when variant was not out of stock' do
- before { variant.stock_items.update_all(count_on_hand: 1, backorderable: true) }
it do
expect do
Timecop.freeze do
- create(:stock_item, variant: variant, backorderable: true, count_on_hand: 1)
+ stock_item.save
end
end.not_to emit_webhook_event('variant.backorderable')
end
end
- 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 = true
end
- 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 do
expect do
Timecop.freeze do
- stock_item.update(backorderable: true, count_on_hand: 0)
+ stock_item.save
end
- end.not_to emit_webhook_event('variant.backorderable')
+ end.to emit_webhook_event('variant.backorderable')
end
end
end
+ 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.save
end
+ stock_item.backorderable = true
+ end
+
+ it do
+ expect do
+ Timecop.freeze do
+ stock_item.save
+ end
+ end.not_to emit_webhook_event('variant.backorderable')
end
end
end
diff --git a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb
index 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 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.quantity = 1 # does make it to be in stock
stock_movement.stock_item = stock_location.set_up_stock_item(variant)
stock_movement.save
end
@@ -33,7 +33,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator 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.quantity = 0 # does not make it to be in stock
stock_movement.stock_item = stock_location.set_up_stock_item(variant)
stock_movement.save
end
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 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