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.backorderable.
Sebastian Palma8e653dd03dc0
diff --git a/api/app/models/spree/api/webhooks/stock_item_decorator.rb b/api/app/models/spree/api/webhooks/stock_item_decorator.rbnew file mode 100644index 0000000000..6c5d630515--- /dev/null+++ b/api/app/models/spree/api/webhooks/stock_item_decorator.rb@@ -0,0 +1,45 @@+module Spree+ module Api+ module Webhooks+ module StockItemDecorator+ def self.prepended(base)+ base.around_create :queue_webhooks_requests_for_variant_backorderable_on_create!+ base.around_update :queue_webhooks_requests_for_variant_backorderable_on_update!+ end++ private++ def queue_webhooks_requests_for_variant_backorderable_on_create!+ was_out_of_stock = variant.out_of_stock?+ yield+ return unless emits_webhook_event_on_create?(variant.reload, was_out_of_stock)++ variant.queue_webhooks_requests!('variant.backorderable')+ end++ def emits_webhook_event_on_create?(variant, was_out_of_stock)+ was_out_of_stock && variant.in_stock? &&+ # rewritting `variant.backorderable?` as is currently being cached+ variant.stock_items.with_active_stock_location.any?(&:backorderable)+ end++ def queue_webhooks_requests_for_variant_backorderable_on_update!+ was_out_of_stock = variant.out_of_stock?+ was_not_orderable = !variant.backorderable?+ yield+ variant.reload+ variant.touch # changes must be reflected before instantiating the serializer+ return unless emits_webhook_event_on_update?(variant, was_out_of_stock, was_not_orderable)++ variant.queue_webhooks_requests!('variant.backorderable')+ end++ def emits_webhook_event_on_update?(variant, was_out_of_stock, was_not_orderable)+ was_out_of_stock && was_not_orderable && variant.in_stock? && variant.backorderable?+ end+ end+ end+ end+end++Spree::StockItem.prepend(Spree::Api::Webhooks::StockItemDecorator)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.rbnew file mode 100644index 0000000000..898e5c20a9--- /dev/null+++ b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb@@ -0,0 +1,167 @@+require 'spec_helper'++describe Spree::Api::Webhooks::StockItemDecorator do+ describe 'emitting variant.backorderable' do+ let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash.to_json }+ let(:variant) { create(:variant) }++ before do+ # makes sure variant.backorderable == false+ stock_item.update(backorderable: false)+ end++ let(:stock_item) { variant.stock_items.first }++ 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+ 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+ 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+ 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+ 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 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+ 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)+ 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+ 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)+ end+ end.not_to emit_webhook_event('variant.backorderable')+ 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')+ end+ end+ end+ end+enddiff --git a/core/app/models/spree/stock/quantifier.rb b/core/app/models/spree/stock/quantifier.rbindex aa337562da..69941c7cff 100644--- a/core/app/models/spree/stock/quantifier.rb+++ b/core/app/models/spree/stock/quantifier.rb@@ -31,7 +31,7 @@ module Spreeprivatedef scope_to_location(collection)- return collection.with_active_stock_location unless stock_location.present?+ return collection.with_active_stock_location if stock_location.blank?collection.where(stock_location: stock_location)enddiff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex a9c96b1a49..11936b5224 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -261,6 +261,10 @@ module Spreeendend+ def out_of_stock?+ !in_stock?+ end+def backorderable?@backorderable ||= Rails.cache.fetch(['variant-backorderable', cache_key_with_version]) doquantifier.backorderable?