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 within api/.

Sebastian Palmac094774887fd
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
index 3523ee8d31..827c263b39 100644
--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb
+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
@@ -20,7 +20,7 @@ module Spree
private
def event_name(operation)
- "#{self.class.name.demodulize.downcase}.#{operation}"
+ "#{self.class.name.demodulize.tableize.singularize}.#{operation}"
end
def webhooks_descendant?
@@ -28,7 +28,7 @@ module Spree
end
def payload
- resource_serializer.new(self, params: { store: Spree::Store.default }).serializable_hash
+ resource_serializer.new(self).serializable_hash
rescue NameError, NoMethodError
{}
end
diff --git a/api/app/models/spree/api/order.rb b/api/app/models/spree/api/order.rb
new file mode 100644
index 0000000000..b0167db770
--- /dev/null
+++ b/api/app/models/spree/api/order.rb
@@ -0,0 +1,15 @@
+module Spree
+ module Api
+ module Order
+ def cancel
+ super
+ Spree::Webhooks::Endpoints::QueueRequests.call(event: 'order.cancel', payload: {})
+ end
+
+ def finalize!
+ super
+ Spree::Webhooks::Endpoints::QueueRequests.call(event: 'order.complete', payload: {})
+ end
+ end
+ end
+end
diff --git a/api/app/models/spree/api/payment.rb b/api/app/models/spree/api/payment.rb
new file mode 100644
index 0000000000..b0ecc66d1b
--- /dev/null
+++ b/api/app/models/spree/api/payment.rb
@@ -0,0 +1,12 @@
+module Spree
+ module Api
+ module Payment
+ private
+
+ def after_void
+ super
+ Spree::Webhooks::Endpoints::QueueRequests.call(event: 'payment.void', payload: {})
+ end
+ end
+ end
+end
diff --git a/api/app/models/spree/api/shipment.rb b/api/app/models/spree/api/shipment.rb
new file mode 100644
index 0000000000..cbade45d3e
--- /dev/null
+++ b/api/app/models/spree/api/shipment.rb
@@ -0,0 +1,12 @@
+module Spree
+ module Api
+ module Shipment
+ private
+
+ def after_ship
+ super
+ Spree::Webhooks::Endpoints::QueueRequests.call(event: 'shipment.ship', payload: {})
+ end
+ end
+ end
+end
diff --git a/api/lib/spree/api/engine.rb b/api/lib/spree/api/engine.rb
index 89298616bf..242a7ec7c1 100644
--- a/api/lib/spree/api/engine.rb
+++ b/api/lib/spree/api/engine.rb
@@ -20,6 +20,9 @@ module Spree
# FIXME: we should only notify about deprecated preferences that are in use, not all of them
# warn "[DEPRECATION] Spree::Api::Config[:#{pref[:name]}] is deprecated. #{pref[:message]}"
end
+ Spree::Order.prepend(Spree::Api::Order)
+ Spree::Payment.prepend(Spree::Api::Payment)
+ Spree::Shipment.prepend(Spree::Api::Shipment)
end
def self.root
diff --git a/api/spec/models/spree/order_spec.rb b/api/spec/models/spree/order_spec.rb
new file mode 100644
index 0000000000..d45c24344d
--- /dev/null
+++ b/api/spec/models/spree/order_spec.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+class TextFormatter
+end
+
+describe Spree::Order do
+ describe 'sending webhooks after transitioning from states' do
+ let(:order) { Spree::Order.create(store: build(:store, default: true)) }
+ let(:payload) { {} }
+ let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
+
+ shared_examples 'queues a webhook request' do |event, method_name|
+ before do
+ allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)
+ allow(queue_requests).to receive(:call).with(any_args)
+ # redefine `method_name` method body to assert super is being used
+ described_class.class_eval do
+ define_method(method_name) do
+ TextFormatter.new.to_s
+ end
+ end
+ end
+
+ it "executes QueueRequests.call with a order.#{event} event and {} payload after calling super" do
+ expect(TextFormatter).to(
+ receive(:new).and_return(
+ double(:scope).tap { |scope| expect(scope).to receive(:to_s) }
+ )
+ )
+ order.send(method_name)
+ expect(queue_requests).to have_received(:call).with(event: "order.#{event}", payload: payload).once
+ end
+ end
+
+ context '#cancel' do
+ include_examples 'queues a webhook request', :cancel, :cancel
+ end
+
+ context '#finalize!' do
+ before { order.update_column :state, 'complete' }
+
+ include_examples 'queues a webhook request', :complete, :finalize!
+ end
+ end
+end
diff --git a/api/spec/models/spree/payment_spec.rb b/api/spec/models/spree/payment_spec.rb
new file mode 100644
index 0000000000..bffc042079
--- /dev/null
+++ b/api/spec/models/spree/payment_spec.rb
@@ -0,0 +1,64 @@
+require 'spec_helper'
+
+class TextFormatter
+end
+
+describe Spree::Payment do
+ let(:payment) do
+ create(:payment,
+ source: build(:credit_card),
+ order: build(:order),
+ payment_method: Spree::Gateway::Bogus.create(active: true))
+ end
+
+ describe '#after_void' do
+ let(:payload) { {} }
+ let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
+
+ before do
+ # redefine the after_void method body to assert super is being used
+ described_class.class_eval do
+ def after_void
+ TextFormatter.new.to_s
+ end
+ end
+
+ allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)
+ allow(queue_requests).to receive(:call).with(any_args)
+ end
+
+ shared_examples 'queues a webhook request' do
+ it 'executes QueueRequests.call with a payment.void event and {} payload after calling super' do
+ expect(TextFormatter).to(
+ receive(:new).and_return(
+ double(:scope).tap { |scope| expect(scope).to receive(:to_s) }
+ )
+ )
+ payment.void
+ expect(queue_requests).to have_received(:call).with(event: 'payment.void', payload: payload).once
+ end
+ end
+
+ context 'when transitioning from pending to void' do
+ before { payment.pend }
+
+ include_examples 'queues a webhook request'
+ end
+
+ context 'when transitioning from processing to void' do
+ before { payment.started_processing }
+
+ include_examples 'queues a webhook request'
+ end
+
+ context 'when transitioning from completed to void' do
+ before { payment.complete }
+
+ include_examples 'queues a webhook request'
+ end
+
+ context 'when transitioning from checkout to void' do
+ include_examples 'queues a webhook request'
+ end
+ end
+end
diff --git a/api/spec/models/spree/shipment_spec.rb b/api/spec/models/spree/shipment_spec.rb
new file mode 100644
index 0000000000..66d1f2fdaf
--- /dev/null
+++ b/api/spec/models/spree/shipment_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+class TextFormatter
+end
+
+describe Spree::Shipment do
+ let(:order) do
+ mock_model Spree::Order, backordered?: false,
+ canceled?: false,
+ can_ship?: true,
+ currency: 'USD',
+ number: 'S12345',
+ paid?: false,
+ touch_later: false
+ end
+ let(:shipment) { create(:shipment, stock_location: create(:stock_location)) }
+
+ context '#after_ship' do
+ before do
+ # redefine the after_ship method body to assert super is being used
+ described_class.class_eval do
+ def after_ship
+ TextFormatter.new.to_s
+ end
+ end
+
+ allow_any_instance_of(Spree::ShipmentHandler).to receive(:update_order_shipment_state)
+ allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)
+ allow(queue_requests).to receive(:call).with(any_args)
+ allow(shipment).to receive(:determine_state).and_return('shipped')
+ end
+
+ 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(TextFormatter).to(
+ receive(:new).and_return(
+ double(:scope).tap { |scope| expect(scope).to receive(:to_s) }
+ )
+ )
+ shipment.update!(order)
+ expect(queue_requests).to have_received(:call).with(event: 'shipment.ship', payload: payload).once
+ end
+ end
+end
diff --git a/api/spec/models/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/spree/webhooks/has_webhooks_spec.rb
index 3de5b8b0e2..14221a6d6c 100644
--- a/api/spec/models/spree/webhooks/has_webhooks_spec.rb
+++ b/api/spec/models/spree/webhooks/has_webhooks_spec.rb
@@ -13,7 +13,7 @@ describe Spree::Webhooks::HasWebhooks do
let(:url) { 'https://google.com' }
before do
- stub_request(:any, url)
+ stub_request(:post, url)
connection.create_table :test_products, force: true do |t|
t.string :name
end
@@ -54,12 +54,13 @@ describe Spree::Webhooks::HasWebhooks do
end
context 'when able to infer the serializer from the class' do
+ let!(:images) { create_list(:image, 2) }
let!(:product) do
create(:product_in_stock,
name: 'Test Product',
price: 10.00,
compare_at_price: 15.00,
- variants_including_master: [create(:variant)])
+ variants_including_master: [create(:variant, images: images), create(:variant)])
end
let(:payload) do
Spree::Api::V2::Platform::ProductSerializer.new(
@@ -69,21 +70,32 @@ describe Spree::Webhooks::HasWebhooks do
end
let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
+ context 'when the class name contains more than one word' do
+ let(:product_property) { create(:product_property) }
+ let(:payload) do
+ Spree::Api::V2::Platform::ProductPropertySerializer.new(product_property).serializable_hash
+ end
+
+ it 'splits the words and joins them with an underscore to create the event_name' do
+ expect(queue_requests).to have_received(:call).with(event: 'product_property.create', payload: payload).once
+ end
+ end
+
context 'after commit on create' do
- it 'does not queue HTTP requests' do
+ it 'executes QueueRequests.call with a create event and the product serializer as the payload' 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
+ it 'executes QueueRequests.call with a destroy event and the product serializer as the payload' 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
+ it 'executes QueueRequests.call with an update event and the product serializer as the payload' do
product.update(name: 'updated')
expect(queue_requests).to have_received(:call).with(event: 'product.update', payload: payload).once
end
diff --git a/core/app/models/spree/base.rb b/core/app/models/spree/base.rb
index 2754f7f661..cf05b0e7af 100644
--- a/core/app/models/spree/base.rb
+++ b/core/app/models/spree/base.rb
@@ -3,7 +3,6 @@ class Spree::Base < ApplicationRecord
serialize :preferences, Hash
include Spree::RansackableAttributes
- include Spree::Webhooks::HasWebhooks
after_initialize do
if has_attribute?(:preferences) && !preferences.nil?
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 2e44161a09..eb363198f3 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -375,8 +375,6 @@ module Spree
deliver_store_owner_order_notification_email if deliver_store_owner_order_notification_email?
consider_risk
-
- execute_webhook_logic!
end
def fulfill!
@@ -700,8 +698,6 @@ module Spree
send_cancel_email
update_with_updater!
-
- execute_webhook_logic!
end
def after_resume
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index d9258fa72e..85313d24b5 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -197,7 +197,7 @@ module Spree
private
def after_void
- execute_webhook_logic!
+ # this method is prepended in api/ to queue Webhooks requests
end
def has_invalid_state?
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index f055f6f286..1b142e690b 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -326,6 +326,8 @@ module Spree
end
def taxons_for_store(store)
+ return if store.blank?
+
Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store.id}") do
taxons.for_store(store)
end
diff --git a/core/app/models/spree/shipment_handler.rb b/core/app/models/spree/shipment_handler.rb
index ca481fab37..ccfb4f1eb4 100644
--- a/core/app/models/spree/shipment_handler.rb
+++ b/core/app/models/spree/shipment_handler.rb
@@ -23,7 +23,6 @@ module Spree
@shipment.touch :shipped_at
update_order_shipment_state
send_shipped_email
- @shipment.execute_webhook_logic!
end
protected
diff --git a/core/lib/spree/testing_support/factories/endpoint.rb b/core/lib/spree/testing_support/factories/endpoint.rb
deleted file mode 100644
index 1332403c84..0000000000
--- a/core/lib/spree/testing_support/factories/endpoint.rb
+++ /dev/null
@@ -1,6 +0,0 @@
-FactoryBot.define do
- factory :endpoint, class: Spree::Webhooks::Endpoint do
- enabled { true }
- url { 'https://google.com:81/' }
- end
-end
diff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rb
index 77839f0a5c..f2b8c56cc9 100644
--- a/core/spec/models/spree/payment_spec.rb
+++ b/core/spec/models/spree/payment_spec.rb
@@ -36,10 +36,6 @@ describe Spree::Payment, type: :model do
ActiveMerchant::Billing::Response.new(false, '', {}, {})
end
- before(:all) do
- stub_request(:any, 'https://google.com')
- end
-
before do
# Rails >= 5.0.3 returns new object for association so we ugly mock it
allow(payment).to receive(:log_entries).and_return(payment.log_entries)
@@ -961,66 +957,4 @@ describe Spree::Payment, type: :model do
it { expect(payment.source).to be_nil }
end
end
-
- describe 'event void' do
- context 'when transitioning from pending to void' do
- before do
- payment.pend
- end
-
- it 'executes a webhook callback' do
- payment.void
- expect(WebMock).to(
- have_requested(:post, 'https://google.com').with(
- body: {foo: :bar}.to_json,
- headers: {'Content-Type' => 'application/json'}
- ).times(12)
- )
- end
- end
-
- context 'when transitioning from processing to void' do
- before do
- payment.started_processing
- end
-
- it 'executes a webhook callback' do
- payment.void
- expect(WebMock).to(
- have_requested(:post, 'https://google.com').with(
- body: {foo: :bar}.to_json,
- headers: {'Content-Type' => 'application/json'}
- ).times(12)
- )
- end
- end
-
- context 'when transitioning from completed to void' do
- before do
- payment.complete
- end
-
- it 'executes a webhook callback' do
- payment.void
- expect(WebMock).to(
- have_requested(:post, 'https://google.com').with(
- body: {foo: :bar}.to_json,
- headers: {'Content-Type' => 'application/json'}
- ).times(12)
- )
- end
- end
-
- context 'when transitioning from checkout to void' do
- it 'executes a webhook callback' do
- payment.void
- expect(WebMock).to(
- have_requested(:post, 'https://google.com').with(
- body: {foo: :bar}.to_json,
- headers: {'Content-Type' => 'application/json'}
- ).times(9)
- )
- end
- end
- end
end