Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions api/app/models/concerns/spree/webhooks/has_webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module HasWebhooks
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
Expand Down Expand Up @@ -42,7 +43,11 @@ def resource_serializer
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?
Expand Down
14 changes: 13 additions & 1 deletion api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,23 @@
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
Expand Down