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.

Emit order.shipped when all order shipments were shipped. (#11443)

sebde2807278f25
diff --git a/api/app/models/spree/api/webhooks/shipment_decorator.rb b/api/app/models/spree/api/webhooks/shipment_decorator.rb
index 19824edbc0..c7c6ae2d7e 100644
--- a/api/app/models/spree/api/webhooks/shipment_decorator.rb
+++ b/api/app/models/spree/api/webhooks/shipment_decorator.rb
@@ -5,6 +5,13 @@ module Spree
def after_ship
super
queue_webhooks_requests!('shipment.shipped')
+ order.queue_webhooks_requests!('order.shipped') if all_order_shipments_were_shipped?
+ end
+
+ private
+
+ def all_order_shipments_were_shipped?
+ order.shipments.shipped.size == order.shipments.size
end
end
end
diff --git a/api/spec/models/spree/api/webhooks/order_spec.rb b/api/spec/models/spree/api/webhooks/order_decorator_spec.rb
similarity index 100%
rename from api/spec/models/spree/api/webhooks/order_spec.rb
rename to api/spec/models/spree/api/webhooks/order_decorator_spec.rb
diff --git a/api/spec/models/spree/api/webhooks/payment_spec.rb b/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb
similarity index 100%
rename from api/spec/models/spree/api/webhooks/payment_spec.rb
rename to api/spec/models/spree/api/webhooks/payment_decorator_spec.rb
diff --git a/api/spec/models/spree/api/webhooks/shipment_spec.rb b/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb
similarity index 50%
rename from api/spec/models/spree/api/webhooks/shipment_spec.rb
rename to api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb
index 7df966e8f2..d47385d726 100644
--- a/api/spec/models/spree/api/webhooks/shipment_spec.rb
+++ b/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb
@@ -19,10 +19,44 @@ describe Spree::Shipment do
let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }
- it 'executes QueueRequests.call with a shipment.shipped event and {} body after invoking ship' do
+ it 'emits a shipment.shipped event' do
shipment.cancel # previous state that allows the object be shipped
shipment.ship
expect(queue_requests).to have_received(:call).with(event: 'shipment.shipped', body: body).once
end
+
+ context 'emitting order.shipped' do
+ let(:body) do
+ order.reload
+ Spree::Api::V2::Platform::OrderSerializer.new(order).serializable_hash.to_json
+ end
+ let(:order) { create(:order) }
+ let!(:shipments) do
+ create_list(
+ :shipment, 2,
+ order: order,
+ shipping_methods: [create(:shipping_method)],
+ stock_location: build(:stock_location)
+ )
+ end
+
+ context 'when all order shipments were shipped' do
+ it 'emits an order.shipped event' do
+ shipments[0].cancel
+ shipments[0].ship
+ shipments[1].cancel
+ shipments[1].ship
+ expect(queue_requests).to have_received(:call).with(event: 'order.shipped', body: body).once
+ end
+ end
+
+ context 'when not all order shipments were shipped' do
+ it 'does not emits an order.shipped event' do
+ shipments[0].cancel
+ shipments[0].ship
+ expect(queue_requests).not_to have_received(:call).with(event: 'order.shipped', body: body)
+ end
+ end
+ end
end
end