Skip to content

Commit

Permalink
SD-1544 Do not emit webhooks on touch events (#11539)
Browse files Browse the repository at this point in the history
* Do not emit webhooks on touch events

* Make check if updating timestamps only be for default events
  • Loading branch information
Aleksandar Petrushev committed Nov 30, 2021
1 parent 11fd397 commit cf09552
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
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

0 comments on commit cf09552

Please sign in to comment.