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
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.rbindex 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 Spreeincluded doafter_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)endend+ 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')endendenddiff --git a/core/db/migrate/20210902162826_create_spree_webhooks_tables.rb b/api/db/migrate/20210902162826_create_spree_webhooks_tables.rbsimilarity index 76%rename from core/db/migrate/20210902162826_create_spree_webhooks_tables.rbrename to api/db/migrate/20210902162826_create_spree_webhooks_tables.rbindex 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: falset.boolean :enabled, default: falsediff --git a/api/spec/models/spree/api/webhooks/order_spec.rb b/api/spec/models/spree/api/webhooks/order_spec.rbindex 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 docontext '#cancel' dolet(: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' doorder.cancelexpect(queue_requests).to have_received(:call).with(event: 'order.cancel', payload: payload).onceend@@ -23,9 +23,7 @@ describe Spree::Order docontext '#finalize!' dolet(: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!' doorder.finalize!expect(queue_requests).to have_received(:call).with(event: 'order.complete', payload: payload).onceenddiff --git a/api/spec/models/spree/api/webhooks/shipment_spec.rb b/api/spec/models/spree/api/webhooks/shipment_spec.rbindex 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 dolet(: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' doshipment.cancel # previous state that allows the object be shippedshipment.shipexpect(queue_requests).to have_received(:call).with(event: 'shipment.ship', payload: payload).oncediff --git a/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rbindex 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 dosubject { 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+ endlet(:user) { create(:user_with_addresses) }