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.

Avoid running webhooks callbacks within migrations.

Sebastian Palma3a3279474a3f
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
index 827c263b39..867523d25e 100644
--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb
+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
@@ -5,38 +5,42 @@ module Spree
included do
after_create_commit proc { queue_webhooks_requests!(event_name(:create)) },
- unless: proc { webhooks_descendant? }
+ unless: proc { migrating? || webhooks_descendant? }
after_destroy_commit proc { queue_webhooks_requests!(event_name(:destroy)) },
- unless: proc { webhooks_descendant? }
+ unless: proc { migrating? || webhooks_descendant? }
after_update_commit proc { queue_webhooks_requests!(event_name(:update)) },
- unless: proc { webhooks_descendant? }
+ unless: proc { migrating? || webhooks_descendant? }
def queue_webhooks_requests!(event)
unless payload.blank?
Spree::Webhooks::Endpoints::QueueRequests.call(event: event, payload: payload)
end
end
+ end
- private
+ private
- def event_name(operation)
- "#{self.class.name.demodulize.tableize.singularize}.#{operation}"
- end
+ def event_name(operation)
+ "#{self.class.name.demodulize.tableize.singularize}.#{operation}"
+ end
- def webhooks_descendant?
- self.class.module_parents.include?(Spree::Webhooks)
- end
+ def webhooks_descendant?
+ self.class.module_parents.include?(Spree::Webhooks)
+ end
- def payload
- resource_serializer.new(self).serializable_hash
- rescue NameError, NoMethodError
- {}
- end
+ def payload
+ resource_serializer.new(self).serializable_hash
+ rescue NameError, NoMethodError
+ {}
+ end
- def resource_serializer
- demodulized_class_name = self.class.to_s.demodulize
- "Spree::Api::V2::Platform::#{demodulized_class_name}Serializer".safe_constantize
- end
+ def resource_serializer
+ demodulized_class_name = self.class.to_s.demodulize
+ "Spree::Api::V2::Platform::#{demodulized_class_name}Serializer".constantize
+ end
+
+ def migrating?
+ ARGV.include?('db:migrate')
end
end
end
diff --git a/core/db/migrate/20210902162826_create_spree_webhooks_tables.rb b/api/db/migrate/20210902162826_create_spree_webhooks_tables.rb
similarity index 76%
rename from core/db/migrate/20210902162826_create_spree_webhooks_tables.rb
rename to api/db/migrate/20210902162826_create_spree_webhooks_tables.rb
index e6e7a20ce7..2e20bb8608 100644
--- a/core/db/migrate/20210902162826_create_spree_webhooks_tables.rb
+++ b/api/db/migrate/20210902162826_create_spree_webhooks_tables.rb
@@ -1,12 +1,5 @@
class CreateSpreeWebhooksTables < ActiveRecord::Migration[6.1]
def change
- create_table :spree_webhooks_events do |t|
- t.string :name, null: false
- t.boolean :enabled, default: true
-
- t.timestamps
- end
-
create_table :spree_webhooks_endpoints do |t|
t.string :url, null: false
t.boolean :enabled, default: false
diff --git a/api/spec/models/spree/api/webhooks/order_spec.rb b/api/spec/models/spree/api/webhooks/order_spec.rb
index 86c37de87f..df9c160181 100644
--- a/api/spec/models/spree/api/webhooks/order_spec.rb
+++ b/api/spec/models/spree/api/webhooks/order_spec.rb
@@ -14,7 +14,7 @@ describe Spree::Order do
context '#cancel' do
let(:order) { create(:completed_order_with_totals, store: store) }
- it 'executes QueueRequests.call with a order.cancel event and {} payload after calling super' do
+ it 'executes QueueRequests.call with a order.cancel event and {} payload after invoking cancel' do
order.cancel
expect(queue_requests).to have_received(:call).with(event: 'order.cancel', payload: payload).once
end
@@ -23,9 +23,7 @@ describe Spree::Order do
context '#finalize!' do
let(:order) { Spree::Order.create(email: '[email protected]', store: store) }
- it 'executes QueueRequests.call with a order.complete event and {} payload after calling super' do
- # check the last statement in the method body is actually
- # executed, because Order doesn't have a complete method.
+ it 'executes QueueRequests.call with a order.complete event and {} payload after invoking finalize!' do
order.finalize!
expect(queue_requests).to have_received(:call).with(event: 'order.complete', payload: payload).once
end
diff --git a/api/spec/models/spree/api/webhooks/shipment_spec.rb b/api/spec/models/spree/api/webhooks/shipment_spec.rb
index b53be83fb5..46e0a3bb35 100644
--- a/api/spec/models/spree/api/webhooks/shipment_spec.rb
+++ b/api/spec/models/spree/api/webhooks/shipment_spec.rb
@@ -12,8 +12,7 @@ describe Spree::Shipment do
let(:payload) { {} }
let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
- it 'executes QueueRequests.call with a shipment.ship event and {} payload after calling super' do
- expect(shipment).to receive(:after_ship).once
+ it 'executes QueueRequests.call with a shipment.ship event and {} payload after invoking ship' do
shipment.cancel # previous state that allows the object be shipped
shipment.ship
expect(queue_requests).to have_received(:call).with(event: 'shipment.ship', payload: payload).once
diff --git a/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
index a4a3843b4d..13e6c7eedc 100644
--- a/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
@@ -5,7 +5,10 @@ describe Spree::Api::V2::Platform::UserSerializer do
subject { described_class.new(user, params: serializer_params) }
- before { allow_any_instance_of(Spree::Order).to receive(:queue_webhooks_requests!); user }
+ before do
+ allow_any_instance_of(Spree::Order).to receive(:queue_webhooks_requests!)
+ user
+ end
let(:user) { create(:user_with_addresses) }