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
Move files from api/. Add callback services and job.
Sebastian Palmac95ec2c2dfb0
diff --git a/core/app/jobs/spree/webhooks/endpoints/make_request_job.rb b/core/app/jobs/spree/webhooks/endpoints/make_request_job.rbnew file mode 100644index 0000000000..e7796fae0d--- /dev/null+++ b/core/app/jobs/spree/webhooks/endpoints/make_request_job.rb@@ -0,0 +1,9 @@+module Spree+ class Webhooks::Endpoints::MakeRequestJob < Spree::BaseJob+ queue_as :spree_webhooks++ def perform(url)+ Spree::Webhooks::Endpoints::MakeRequest.call(url: url)+ end+ end+enddiff --git a/core/app/models/spree/webhooks/endpoint.rb b/core/app/models/spree/webhooks/endpoint.rbnew file mode 100644index 0000000000..97b1f830d4--- /dev/null+++ b/core/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/models/spree/webhooks/event.rb b/core/app/models/spree/webhooks/event.rbnew file mode 100644index 0000000000..586d0ffa6a--- /dev/null+++ b/core/app/models/spree/webhooks/event.rb@@ -0,0 +1,7 @@+module Spree+ module Webhooks+ class Event < Spree::Base+ self.table_name = 'spree_webhooks_events'+ end+ end+enddiff --git a/core/app/services/spree/webhooks/endpoints/make_request.rb b/core/app/services/spree/webhooks/endpoints/make_request.rbnew file mode 100644index 0000000000..452f99d061--- /dev/null+++ b/core/app/services/spree/webhooks/endpoints/make_request.rb@@ -0,0 +1,28 @@+module Spree+ class Webhooks::Endpoints::MakeRequest+ prepend Spree::ServiceModule::Base++ def call(url:)+ run :make_request+ end++ private++ HEADERS = { 'Content-Type' => 'application/json' }.freeze+ private_constant :HEADERS++ def make_request(url:)+ uri = URI(url)+ if uri.path == "" && uri.host.nil? && uri.port.nil?+ failure(false)+ else+ request = Net::HTTP::Post.new(uri.path, HEADERS)+ request.body = {foo: :bar}.to_json+ http = Net::HTTP.new(uri.host, uri.port)+ http.use_ssl = true+ success(http.request(request))+ end+ end+ end+end+diff --git a/core/app/services/spree/webhooks/endpoints/queue_requests.rb b/core/app/services/spree/webhooks/endpoints/queue_requests.rbnew file mode 100644index 0000000000..a487284844--- /dev/null+++ b/core/app/services/spree/webhooks/endpoints/queue_requests.rb@@ -0,0 +1,20 @@+module Spree+ class Webhooks::Endpoints::QueueRequests+ prepend Spree::ServiceModule::Base++ def call(event:)+ urls_subscribed_to(event).each do |url|+ Spree::Webhooks::Endpoints::MakeRequestJob.perform_later(url)+ end+ end++ private++ def urls_subscribed_to(event)+ Spree::Webhooks::Endpoint+ .where(enabled: true)+ .where('subscriptions @> ? OR subscriptions @> ?', ['*'].to_json, [event].to_json)+ .pluck(:url)+ end+ end+enddiff --git a/api/db/migrate/20210902162826_create_spree_webhooks_tables.rb b/core/db/migrate/20210902162826_create_spree_webhooks_tables.rbsimilarity index 92%rename from api/db/migrate/20210902162826_create_spree_webhooks_tables.rbrename to core/db/migrate/20210902162826_create_spree_webhooks_tables.rbindex a8c036dbb8..e6e7a20ce7 100644--- a/api/db/migrate/20210902162826_create_spree_webhooks_tables.rb+++ b/core/db/migrate/20210902162826_create_spree_webhooks_tables.rb@@ -24,7 +24,7 @@ class CreateSpreeWebhooksTables < ActiveRecord::Migration[6.1]when 'Mysql2'table.json :subscriptionswhen 'PostgreSQL'- table.jsonb :subscriptions, default: ['*']+ table.jsonb :subscriptionsendendenddiff --git a/core/lib/spree/testing_support/factories/endpoint.rb b/core/lib/spree/testing_support/factories/endpoint.rbindex 7b9fc161b9..1332403c84 100644--- a/core/lib/spree/testing_support/factories/endpoint.rb+++ b/core/lib/spree/testing_support/factories/endpoint.rb@@ -1,7 +1,6 @@FactoryBot.define dofactory :endpoint, class: Spree::Webhooks::Endpoint doenabled { true }- subscriptions { ['*'] }- url { 'https://google.com:81' }+ url { 'https://google.com:81/' }endenddiff --git a/core/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb b/core/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rbnew file mode 100644index 0000000000..e85da09f97--- /dev/null+++ b/core/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb@@ -0,0 +1,12 @@+require 'spec_helper'++describe Spree::Webhooks::Endpoints::MakeRequestJob, :inline_jobs do+:wa+ let(:url) { 'http://url.com/' }++ it 'it enqueues a HTTP request using Spree::Webhooks::Endpoints::MakeRequest' do+ expect { described_class.perform_later(url) }.to(+ have_enqueued_job.on_queue('spree_webhooks')+ )+ end+enddiff --git a/core/spec/models/spree/webhooks/has_webhooks_spec.rb b/core/spec/models/spree/webhooks/has_webhooks_spec.rbindex 5ef262f063..5309ce403d 100644--- a/core/spec/models/spree/webhooks/has_webhooks_spec.rb+++ b/core/spec/models/spree/webhooks/has_webhooks_spec.rb@@ -24,7 +24,6 @@ module Spreelet(:url) { 'https://google.com' }before do- WebMock.disable_net_connect!stub_request(:any, url)connection.create_table :test_products, force: true do |table|table.string :name@@ -32,7 +31,6 @@ module Spreeendafter do- WebMock.allow_net_connect!connection.drop_table :test_products, if_exists: trueenddiff --git a/core/spec/services/spree/webhooks/endpoints/make_request_spec.rb b/core/spec/services/spree/webhooks/endpoints/make_request_spec.rbnew file mode 100644index 0000000000..08397fa4f1--- /dev/null+++ b/core/spec/services/spree/webhooks/endpoints/make_request_spec.rb@@ -0,0 +1,32 @@+require 'spec_helper'++describe Spree::Webhooks::Endpoints::MakeRequest do+ describe '#call' do+ context 'with a valid URL' do+ let(:url) { 'https://google.com/' }+ let(:headers) { { 'Content-Type' => 'application/json' } }++ before { stub_request(:post, url) }++ it 'makes a post HTTP request to the given url' do+ described_class.call(url: url)+ expect(WebMock).to(+ have_requested(:post, url).with(body: {foo: :bar}.to_json, headers: headers).once+ )+ end++ it 'returns a success object with true success value' do+ expect(described_class.call(url: url).success).to eq(true)+ end+ end++ context 'without a valid URL' do+ let(:url) { '' }++ it 'does not instantiate a Net::HTTP::Post to make a request' do+ expect(Net::HTTP::Post).not_to receive(:new)+ expect(described_class.call(url: url).success).to eq(false)+ end+ end+ end+enddiff --git a/core/spec/services/spree/webhooks/endpoints/queue_request_spec.rb b/core/spec/services/spree/webhooks/endpoints/queue_request_spec.rbnew file mode 100644index 0000000000..13aae1366f--- /dev/null+++ b/core/spec/services/spree/webhooks/endpoints/queue_request_spec.rb@@ -0,0 +1,69 @@+require 'spec_helper'++describe Spree::Webhooks::Endpoints::QueueRequests do+ describe '#call' do+ let(:event) { 'order.finalize' }++ context 'without subscriptions for the given event' do+ it 'does not make any HTTP request' do+ expect { subject.call(event: event) }.not_to(+ have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequest).on_queue('spree_webhooks')+ )+ end+ end++ context 'with subscriptions for the given event' do+ context 'when endpoint subscriptions includes all events (*)' do+ before { stub_request(:post, endpoint.url) }++ let(:endpoint) { create(:endpoint, url: 'https://url1.com/', subscriptions: ['*']) }++ it 'makes a HTTP request to its URL' do+ expect { subject.call(event: event) }.to(+ have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob)+ .with(endpoint.url)+ .on_queue('spree_webhooks')+ )+ end+ end++ context 'when endpoint subscriptions includes the specific event being used' do+ before { stub_request(:post, endpoint.url) }++ let(:endpoint) { create(:endpoint, url: 'https://url2.com/', subscriptions: [event]) }++ it 'makes a HTTP request to its URL' do+ expect { subject.call(event: event) }.to(+ have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob)+ .with(endpoint.url)+ .on_queue('spree_webhooks')+ )+ end+ end++ context 'when endpoint subscriptions are not enabled' do+ let(:endpoint3) { create(:endpoint, url: 'https://url3.com/', subscriptions: [event], enabled: false) }++ it 'does not make a HTTP request' do+ expect { subject.call(event: event) }.not_to(+ have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob)+ .with(endpoint3.url)+ .on_queue('spree_webhooks')+ )+ end+ end++ context 'when endpoint subscriptions do not include the event or "*"' do+ let(:endpoint4) { create(:endpoint, url: 'https://url4.com/', subscriptions: ['order.resume']) }++ it 'does not make a HTTP request' do+ expect { subject.call(event: event) }.not_to(+ have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob)+ .with(endpoint4.url)+ .on_queue('spree_webhooks')+ )+ end+ end+ end+ end+end