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.

Prepend after_transition methods, not events. Add Spree::Webhooks::Base.

Sebastian Palmac8c57b34b99a
diff --git a/api/app/models/spree/api/webhooks/order.rb b/api/app/models/spree/api/webhooks/order.rb
index 6265132cd1..1a7eaa7b01 100644
--- a/api/app/models/spree/api/webhooks/order.rb
+++ b/api/app/models/spree/api/webhooks/order.rb
@@ -2,7 +2,7 @@ module Spree
module Api
module Webhooks
module Order
- def cancel
+ def after_cancel
super
Spree::Webhooks::Endpoints::QueueRequests.call(event: 'order.cancel', payload: {})
end
diff --git a/api/app/models/spree/api/webhooks/payment.rb b/api/app/models/spree/api/webhooks/payment.rb
index 6e3dda54b0..ade2261f19 100644
--- a/api/app/models/spree/api/webhooks/payment.rb
+++ b/api/app/models/spree/api/webhooks/payment.rb
@@ -2,7 +2,7 @@ module Spree
module Api
module Webhooks
module Payment
- def void
+ def after_void
super
Spree::Webhooks::Endpoints::QueueRequests.call(event: 'payment.void', payload: {})
end
diff --git a/api/app/models/spree/api/webhooks/shipment.rb b/api/app/models/spree/api/webhooks/shipment.rb
index 1594bcdca3..7f46977c5d 100644
--- a/api/app/models/spree/api/webhooks/shipment.rb
+++ b/api/app/models/spree/api/webhooks/shipment.rb
@@ -2,7 +2,7 @@ module Spree
module Api
module Webhooks
module Shipment
- def ship
+ def after_ship
super
Spree::Webhooks::Endpoints::QueueRequests.call(event: 'shipment.ship', payload: {})
end
diff --git a/api/app/models/spree/webhooks/base.rb b/api/app/models/spree/webhooks/base.rb
new file mode 100644
index 0000000000..8c42ffd3e4
--- /dev/null
+++ b/api/app/models/spree/webhooks/base.rb
@@ -0,0 +1,11 @@
+module Spree
+ module Webhooks
+ def self.table_name_prefix
+ 'spree_webhooks_'
+ end
+
+ class Base < Spree::Base
+ self.abstract_class = true
+ end
+ end
+end
diff --git a/api/app/models/spree/webhooks/endpoint.rb b/api/app/models/spree/webhooks/endpoint.rb
index 9ec80b5f46..b047db4714 100644
--- a/api/app/models/spree/webhooks/endpoint.rb
+++ b/api/app/models/spree/webhooks/endpoint.rb
@@ -1,7 +1,7 @@
module Spree
module Webhooks
- class Endpoint < Spree::Base
- self.table_name = 'spree_webhooks_endpoints'
+ class Endpoint < Spree::Webhooks::Base
+ validates :url, presence: true
end
end
end
diff --git a/api/app/services/spree/webhooks/endpoints/make_request.rb b/api/app/services/spree/webhooks/endpoints/make_request.rb
index 5101bcb4f8..bdf1fcc291 100644
--- a/api/app/services/spree/webhooks/endpoints/make_request.rb
+++ b/api/app/services/spree/webhooks/endpoints/make_request.rb
@@ -2,29 +2,30 @@ module Spree
module Webhooks
module Endpoints
class MakeRequest
- prepend Spree::ServiceModule::Base
+ prepend Spree::ServiceModule::Base
- def call(url:)
- run :make_request
- end
+ def call(url:)
+ run :make_request
+ end
- private
+ private
- HEADERS = { 'Content-Type' => 'application/json' }.freeze
- private_constant :HEADERS
+ 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))
+ 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
end
end
-
diff --git a/api/spec/models/spree/api/webhooks/order_spec.rb b/api/spec/models/spree/api/webhooks/order_spec.rb
index df9c160181..18d8094a2e 100644
--- a/api/spec/models/spree/api/webhooks/order_spec.rb
+++ b/api/spec/models/spree/api/webhooks/order_spec.rb
@@ -11,7 +11,7 @@ describe Spree::Order do
allow(queue_requests).to receive(:call).with(any_args)
end
- context '#cancel' do
+ context '#after_cancel' do
let(:order) { create(:completed_order_with_totals, store: store) }
it 'executes QueueRequests.call with a order.cancel event and {} payload after invoking cancel' do
diff --git a/api/spec/models/spree/api/webhooks/shipment_spec.rb b/api/spec/models/spree/api/webhooks/shipment_spec.rb
index 46e0a3bb35..381892a9d1 100644
--- a/api/spec/models/spree/api/webhooks/shipment_spec.rb
+++ b/api/spec/models/spree/api/webhooks/shipment_spec.rb
@@ -3,7 +3,7 @@ require 'spec_helper'
describe Spree::Shipment do
let(:shipment) { create(:shipment) }
- context '#ship' do
+ context '#after_ship' do
before do
allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)
allow(queue_requests).to receive(:call).with(any_args)
diff --git a/api/spec/models/spree/webhooks/endpoint_spec.rb b/api/spec/models/spree/webhooks/endpoint_spec.rb
new file mode 100644
index 0000000000..dbd68569c9
--- /dev/null
+++ b/api/spec/models/spree/webhooks/endpoint_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe Spree::Store do
+ describe 'validations' do
+ context 'url presence' do
+ it 'is valid with url' do
+ expect(build(:endpoint, url: 'https://google.com/').valid?).to be(true)
+ end
+
+ it 'is invalid without url' do
+ expect(build(:endpoint, url: nil).valid?).to be(false)
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index be52a2231a..85313d24b5 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -97,6 +97,7 @@ module Spree
event :void do
transition from: [:pending, :processing, :completed, :checkout], to: :void
end
+ after_transition to: :void, do: :after_void
# when the card brand isnt supported
event :invalidate do
transition from: [:checkout], to: :invalid
@@ -195,6 +196,10 @@ module Spree
private
+ def after_void
+ # this method is prepended in api/ to queue Webhooks requests
+ end
+
def has_invalid_state?
INVALID_STATES.include?(state)
end
diff --git a/core/lib/spree/testing_support/factories/endpoint_factory.rb b/core/lib/spree/testing_support/factories/endpoint_factory.rb
new file mode 100644
index 0000000000..31951814b8
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/endpoint_factory.rb
@@ -0,0 +1,4 @@
+FactoryBot.define do
+ factory :endpoint, class: Spree::Webhooks::Endpoint do
+ end
+end