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.

SD-1544 Do not emit webhooks on touch events (#11539)

Aleksandar Petrushevcf09552d5933
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
index 7c08f99c79..8beb55903b 100644
--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb
+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
@@ -9,7 +9,8 @@ module Spree
after_update_commit(proc { queue_webhooks_requests!(inferred_event_name(:update)) })
def queue_webhooks_requests!(event_name)
- return if disable_spree_webhooks? || updating_only_timestamps? || body.blank?
+ return if disable_spree_webhooks? || body.blank?
+ return if default_event?(event_name) && updating_only_timestamps?
Spree::Webhooks::Subscribers::QueueRequests.call(body: body, event_name: event_name)
end
@@ -42,7 +43,11 @@ module Spree
end
def updating_only_timestamps?
- saved_changes.present? && (saved_changes.keys - %w[created_at updated_at]).empty?
+ (saved_changes.keys - %w[created_at updated_at deleted_at]).empty?
+ end
+
+ def default_event?(event_name)
+ self.class.default_webhook_events.include?(event_name)
end
def disable_spree_webhooks?
diff --git a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
index 584791ed50..26f5f77bb5 100644
--- a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
+++ b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
@@ -107,11 +107,23 @@ describe Spree::Webhooks::HasWebhooks do
context 'when using touch with an argument other than created_at/updated_at' do
it do
expect do
- product.touch(:deleted_at)
+ product.touch(:available_on)
end.to emit_webhook_event(event_name)
end
end
end
+
+ context 'on touch events from callbacks' do
+ let!(:store2) { create(:store) }
+ let!(:cms_page) { create(:cms_homepage, store: store2, locale: 'en') }
+ let(:body) { Spree::Api::V2::Platform::StoreSerializer.new(store2).serializable_hash }
+
+ before { store2.changes_applied }
+
+ it 'does not emit the touched model\'s update event' do
+ expect { cms_page.update(title: 'Homepage #1') }.not_to emit_webhook_event('store.update')
+ end
+ end
end
describe '.default_webhook_events' do