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
[SD-1544] Don't fire webhooks touch events if only timestamps were changed (#11503)
seb97301fb9fe75
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rbindex 9b4ce25fad..097a127b95 100644--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb@@ -9,7 +9,7 @@ module Spreeafter_update_commit(proc { queue_webhooks_requests!(event_name(:update)) })def queue_webhooks_requests!(event)- return if disable_spree_webhooks? || body.blank?+ return if disable_spree_webhooks? || updating_only_timestamps? || body.blank?Spree::Webhooks::Subscribers::QueueRequests.call(event: event, body: body)end@@ -30,6 +30,10 @@ module Spree"Spree::Api::V2::Platform::#{demodulized_class_name}Serializer".constantizeend+ def updating_only_timestamps?+ saved_changes.present? && (saved_changes.keys - %w[created_at updated_at]).empty?+ end+def disable_spree_webhooks?ENV['DISABLE_SPREE_WEBHOOKS'] == 'true'enddiff --git a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb b/api/app/models/spree/api/webhooks/stock_movement_decorator.rbindex a5e823b351..1ded7fd443 100644--- a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb+++ b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb@@ -23,6 +23,7 @@ module Spreevariant_was_out_of_stock = !variant.in_stock_or_backorderable?yieldif variant_was_out_of_stock && variant.in_stock_or_backorderable?+ reloadvariant.queue_webhooks_requests!('variant.back_in_stock')endenddiff --git a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rbindex b8bb8e1676..a06c07e862 100644--- a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb+++ b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb@@ -68,5 +68,43 @@ describe Spree::Webhooks::HasWebhooks doexpect { cms_page }.to emit_webhook_event('cms_page.create')endend++ context 'when only timestamps change' do+ before { product.save }++ context 'on created_at change' do+ it do+ expect do+ product.update(created_at: Date.yesterday)+ end.not_to emit_webhook_event('product.update')+ end+ end++ context 'on updated_at change' do+ it do+ expect do+ product.update(updated_at: Date.yesterday)+ end.not_to emit_webhook_event('product.update')+ end+ end++ context 'when using touch without arguments' do+ it do+ expect do+ # Doing product.touch in Rails 5.2 doesn't work at the first time.+ # It must be done twice in order to update the updated_at column.+ Spree::Product.find(product.id).touch+ end.not_to emit_webhook_event('product.update')+ end+ end++ context 'when using touch with an argument other than created_at/updated_at' do+ it do+ expect do+ product.touch(:deleted_at)+ end.to emit_webhook_event('product.update')+ end+ end+ 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 2273f1cb74..8ed36f940a 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@@ -64,6 +64,8 @@ describe Spree::Api::Webhooks::StockItemDecorator doenddescribe 'emitting variant.backorderable' do+ subject { stock_item.save }+context 'when variant was out of stock' dobefore dostock_location.stock_movements.new.tap do |stock_movement|@@ -74,48 +76,24 @@ describe Spree::Api::Webhooks::StockItemDecorator doendcontext 'when variant was backorderable' do- before do- stock_item.backorderable = true- end+ before { stock_item.backorderable = true }- it do- expect do- Timecop.freeze do- stock_item.save- end- end.not_to emit_webhook_event('variant.backorderable')- end+ it { expect { subject }.not_to emit_webhook_event('variant.backorderable') }endcontext 'when variant was not backorderable' dobefore { variant.stock_items.update_all(backorderable: false) }context 'when variant is not set as backorderable' do- before do- stock_item.backorderable = false- end+ before { stock_item.backorderable = false }- it do- expect do- Timecop.freeze do- stock_item.save- end- end.not_to emit_webhook_event('variant.backorderable')- end+ it { expect { subject }.not_to emit_webhook_event('variant.backorderable') }endcontext 'when variant is set as backorderable' do- before do- stock_item.backorderable = true- end+ before { stock_item.backorderable = true }- it do- expect do- Timecop.freeze do- stock_item.save- end- end.to emit_webhook_event('variant.backorderable')- end+ it { expect { subject }.to emit_webhook_event('variant.backorderable') }endendend@@ -131,13 +109,7 @@ describe Spree::Api::Webhooks::StockItemDecorator dostock_item.backorderable = trueend- it do- expect do- Timecop.freeze do- stock_item.save- end- end.not_to emit_webhook_event('variant.backorderable')- end+ it { expect { subject }.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 4f2db81baf..2b69988034 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@@ -17,14 +17,12 @@ describe Spree::Api::Webhooks::StockMovementDecorator docontext 'when all product variants are out of stock' docontext 'when one of the variants is back in stock' dosubject do- Timecop.freeze do- 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- product.reload+ 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+ product.reloadendit { expect { subject }.to emit_webhook_event('product.back_in_stock') }@@ -32,14 +30,12 @@ describe Spree::Api::Webhooks::StockMovementDecorator docontext 'when none of the variants is back in stock' dosubject do- Timecop.freeze 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- product.reload+ 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+ product.reloadendit { expect { subject }.not_to emit_webhook_event('product.back_in_stock') }@@ -74,13 +70,11 @@ describe Spree::Api::Webhooks::StockMovementDecorator docontext 'when stock item changes to be in stock' doit doexpect do- Timecop.freeze do- variant.stock_items.update_all(backorderable: false)- stock_location.stock_movements.new.tap do |stock_movement|- 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+ variant.stock_items.update_all(backorderable: false)+ stock_location.stock_movements.new.tap do |stock_movement|+ stock_movement.quantity = 1 # does make it to be in stock+ stock_movement.stock_item = stock_location.set_up_stock_item(variant)+ stock_movement.saveendend.to emit_webhook_event('variant.back_in_stock')end@@ -89,13 +83,11 @@ describe Spree::Api::Webhooks::StockMovementDecorator docontext 'when stock item does not change to be in stock' doit doexpect do- Timecop.freeze do- variant.stock_items.update_all(backorderable: false)- stock_location.stock_movements.new.tap do |stock_movement|- 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+ variant.stock_items.update_all(backorderable: false)+ stock_location.stock_movements.new.tap do |stock_movement|+ 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.saveendend.not_to emit_webhook_event('variant.back_in_stock')end@@ -105,14 +97,12 @@ describe Spree::Api::Webhooks::StockMovementDecorator docontext 'when stock item was in stock' doit doexpect 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+ # 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.saveendend.not_to emit_webhook_event('variant.back_in_stock')end@@ -144,9 +134,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator doenddescribe 'when the variant was out of stock before the update and after the update' do- before do- stock_item.set_count_on_hand(0)- end+ before { stock_item.set_count_on_hand(0) }let(:movement_quantity) { 0 }