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
Add payload to MakeRequests service.
Sebastian Palma10ce09ec14a1
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rbnew file mode 100644index 0000000000..3523ee8d31--- /dev/null+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb@@ -0,0 +1,43 @@+module Spree+ module Webhooks+ module HasWebhooks+ extend ActiveSupport::Concern++ included do+ after_create_commit proc { queue_webhooks_requests!(event_name(:create)) },+ unless: proc { webhooks_descendant? }+ after_destroy_commit proc { queue_webhooks_requests!(event_name(:destroy)) },+ unless: proc { webhooks_descendant? }+ after_update_commit proc { queue_webhooks_requests!(event_name(:update)) },+ unless: proc { webhooks_descendant? }++ def queue_webhooks_requests!(event)+ unless payload.blank?+ Spree::Webhooks::Endpoints::QueueRequests.call(event: event, payload: payload)+ end+ end++ private++ def event_name(operation)+ "#{self.class.name.demodulize.downcase}.#{operation}"+ end++ def webhooks_descendant?+ self.class.module_parents.include?(Spree::Webhooks)+ end++ def payload+ resource_serializer.new(self, params: { store: Spree::Store.default }).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+ end+ end+ end+enddiff --git a/api/app/models/spree/webhooks/endpoint.rb b/api/app/models/spree/webhooks/endpoint.rbnew file mode 100644index 0000000000..97b1f830d4--- /dev/null+++ b/api/app/models/spree/webhooks/endpoint.rb@@ -0,0 +1,16 @@+module Spree+ module Webhooks+ class Endpoint < Spree::Base+ self.table_name = 'spree_webhooks_endpoints'++ before_save :assign_default_subscriptions,+ if: proc { ActiveRecord::Base.connection.adapter_name == 'Mysql2' }++ private++ def assign_default_subscriptions+ self.subscriptions = ['*'] if subscriptions.blank?+ end+ end+ end+enddiff --git a/core/app/services/spree/webhooks/endpoints/make_request.rb b/api/app/services/spree/webhooks/endpoints/make_request.rbsimilarity index 100%rename from core/app/services/spree/webhooks/endpoints/make_request.rbrename to api/app/services/spree/webhooks/endpoints/make_request.rbdiff --git a/core/app/services/spree/webhooks/endpoints/queue_requests.rb b/api/app/services/spree/webhooks/endpoints/queue_requests.rbsimilarity index 93%rename from core/app/services/spree/webhooks/endpoints/queue_requests.rbrename to api/app/services/spree/webhooks/endpoints/queue_requests.rbindex a487284844..271f85f3e8 100644--- a/core/app/services/spree/webhooks/endpoints/queue_requests.rb+++ b/api/app/services/spree/webhooks/endpoints/queue_requests.rb@@ -2,7 +2,7 @@ module Spreeclass Webhooks::Endpoints::QueueRequestsprepend Spree::ServiceModule::Base- def call(event:)+ def call(event:, payload:)urls_subscribed_to(event).each do |url|Spree::Webhooks::Endpoints::MakeRequestJob.perform_later(url)enddiff --git a/api/spec/models/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/spree/webhooks/has_webhooks_spec.rbnew file mode 100644index 0000000000..3de5b8b0e2--- /dev/null+++ b/api/spec/models/spree/webhooks/has_webhooks_spec.rb@@ -0,0 +1,118 @@+require 'spec_helper'++module Spree+ class Webhooks::Product < ActiveRecord::Base+ self.table_name = 'test_products'++ include Spree::Webhooks::HasWebhooks+ end+end++describe Spree::Webhooks::HasWebhooks do+ let(:connection) { ActiveRecord::Base.connection }+ let(:url) { 'https://google.com' }++ before do+ stub_request(:any, url)+ connection.create_table :test_products, force: true do |t|+ t.string :name+ end+ end++ after do+ connection.drop_table :test_products, if_exists: true+ end++ context 'with a Spree::Webhooks class including the module' do+ let!(:product) { Spree::Webhooks::Product.create(name: 'test') }++ context 'after commit on create' do+ it 'does not execute the webhook callback' do+ expect(Spree::Webhooks::Endpoints::QueueRequests).not_to receive(:new)+ end+ end++ context 'after commit on destroy' do+ it 'does not execute the webhook callback' do+ product.destroy+ expect(Spree::Webhooks::Endpoints::QueueRequests).not_to receive(:new)+ end+ end++ context 'after commit on update' do+ it 'does not execute the webhook callback' do+ product.update(name: 'updated')+ expect(Spree::Webhooks::Endpoints::QueueRequests).not_to receive(:new)+ end+ end+ end++ context 'with a non Spree::Webhooks class including the module' do+ before do+ allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)+ allow(queue_requests).to receive(:call).with(any_args)+ end++ context 'when able to infer the serializer from the class' do+ let!(:product) do+ create(:product_in_stock,+ name: 'Test Product',+ price: 10.00,+ compare_at_price: 15.00,+ variants_including_master: [create(:variant)])+ end+ let(:payload) do+ Spree::Api::V2::Platform::ProductSerializer.new(+ product,+ params: { store: Spree::Store.default }+ ).serializable_hash+ end+ let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }++ context 'after commit on create' do+ it 'does not queue HTTP requests' do+ expect(queue_requests).to have_received(:call).with(event: 'product.create', payload: payload).once+ end+ end++ context 'after commit on destroy' do+ it 'does not queue HTTP requests' do+ product.destroy+ expect(queue_requests).to have_received(:call).with(event: 'product.destroy', payload: payload).once+ end+ end++ context 'after commit on update' do+ it 'does not queue HTTP requests' do+ product.update(name: 'updated')+ expect(queue_requests).to have_received(:call).with(event: 'product.update', payload: payload).once+ end+ end+ end++ context 'when unable to infer the serializer from the class' do+ let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }+ let!(:shipment) { create(:shipment) }++ context 'after commit on create' do+ it 'does not queue HTTP requests' do+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'shipment.create'))+ end+ end++ context 'after commit on destroy' do+ it 'does not queue HTTP requests' do+ shipment.destroy+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'shipment.destroy'))+ end+ end++ context 'after commit on update' do+ it 'does not queue HTTP requests' do+ shipment.update(tracking: 'U10001')+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'shipment.update'))+ end+ end+ end+ end+enddiff --git a/core/spec/services/spree/webhooks/endpoints/make_request_spec.rb b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rbsimilarity index 100%rename from core/spec/services/spree/webhooks/endpoints/make_request_spec.rbrename to api/spec/services/spree/webhooks/endpoints/make_request_spec.rbdiff --git a/core/spec/services/spree/webhooks/endpoints/queue_request_spec.rb b/api/spec/services/spree/webhooks/endpoints/queue_request_spec.rbsimilarity index 100%rename from core/spec/services/spree/webhooks/endpoints/queue_request_spec.rbrename to api/spec/services/spree/webhooks/endpoints/queue_request_spec.rbdiff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rbindex ec98f89e76..998de3c110 100644--- a/api/spec/spec_helper.rb+++ b/api/spec/spec_helper.rb@@ -27,6 +27,7 @@ endrequire 'rspec/rails'require 'ffaker'+require 'webmock/rspec'# Requires supporting ruby files with custom matchers and macros, etc,# in spec/support/ and its subdirectories.diff --git a/core/app/models/concerns/spree/webhooks/has_webhooks.rb b/core/app/models/concerns/spree/webhooks/has_webhooks.rbdeleted file mode 100644index 9040b6a2d2..0000000000--- a/core/app/models/concerns/spree/webhooks/has_webhooks.rb+++ /dev/null@@ -1,30 +0,0 @@-module Spree- module Webhooks- module HasWebhooks- extend ActiveSupport::Concern-- included do- after_create_commit proc { execute_webhook_logic!(event_name(:create)) },- unless: proc { webhooks_descendant? }- after_destroy_commit proc { execute_webhook_logic!(event_name(:destroy)) },- unless: proc { webhooks_descendant? }- after_update_commit proc { execute_webhook_logic!(event_name(:update)) },- unless: proc { webhooks_descendant? }-- def execute_webhook_logic!(event)- Spree::Webhooks::Endpoints::QueueRequests.call(event: event)- end-- private-- def webhooks_descendant?- self.class.module_parents.include?(Spree::Webhooks)- end-- def event_name(operation)- "#{self.class.name.demodulize.downcase}.#{operation}"- end- end- end- end-enddiff --git a/core/spec/models/spree/webhooks/has_webhooks_spec.rb b/core/spec/models/spree/webhooks/has_webhooks_spec.rbdeleted file mode 100644index 6ce6a0e692..0000000000--- a/core/spec/models/spree/webhooks/has_webhooks_spec.rb+++ /dev/null@@ -1,104 +0,0 @@-require 'spec_helper'--module Test- class Product < ActiveRecord::Base- self.table_name = 'test_products'-- include Spree::Webhooks::HasWebhooks- end-end--module Spree- class Webhooks::Product < ActiveRecord::Base- self.table_name = 'test_products'-- include Spree::Webhooks::HasWebhooks- end-end--describe Spree::Webhooks::HasWebhooks do- let(:connection) { ActiveRecord::Base.connection }- let(:url) { 'https://google.com' }-- before do- stub_request(:any, url)- connection.create_table :test_products, force: true do |table|- table.string :name- end- end-- after do- connection.drop_table :test_products, if_exists: true- end-- context 'with a Spree::Webhooks class including the module' do- let!(:product) { Spree::Webhooks::Product.create(name: 'test') }-- context 'after commit on create' do- it 'does not execute the webhook callback' do- expect(Spree::Webhooks::Endpoints::QueueRequests).not_to(receive(:new))- end- end-- context 'after commit on destroy' do- it 'does not execute the webhook callback' do- product.destroy- expect(Spree::Webhooks::Endpoints::QueueRequests).not_to(receive(:new))- end- end-- context 'after commit on update' do- it 'does not execute the webhook callback' do- product.update(name: 'updated')- expect(Spree::Webhooks::Endpoints::QueueRequests).not_to(receive(:new))- end- end- end-- context 'with a non Spree::Webhooks class including the module' do- let(:product) { Test::Product.create(name: 'test') }-- context 'after commit on create' do- it 'executes the webhook callback' do- expect(Spree::Webhooks::Endpoints::QueueRequests).to(- receive(:new).once.and_return(- double(:new).tap do |scope|- expect(scope).to receive(:call).with(event: 'product.create').once- end- )- )- product- end- end-- context 'after commit on destroy' do- before { product }-- it 'executes the webhook callback' do- expect(Spree::Webhooks::Endpoints::QueueRequests).to(- receive(:new).once.and_return(- double(:new).tap do |scope|- expect(scope).to receive(:call).with(event: 'product.destroy').once- end- )- )- product.destroy- end- end-- context 'after commit on update' do- before { product }-- it 'executes the webhook callback' do- expect(Spree::Webhooks::Endpoints::QueueRequests).to(- receive(:new).once.and_return(- double(:new).tap do |scope|- expect(scope).to receive(:call).with(event: 'product.update').once- end- )- )- product.update(name: 'updated')- end- end- end-end