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
Emit order.resumed after_update_commit alongside order.resume!.
Sebastian Palma951fe5af7cbe
diff --git a/api/app/models/spree/api/webhooks/order_decorator.rb b/api/app/models/spree/api/webhooks/order_decorator.rbindex d3df9c9a69..c3896a0c6a 100644--- a/api/app/models/spree/api/webhooks/order_decorator.rb+++ b/api/app/models/spree/api/webhooks/order_decorator.rb@@ -2,6 +2,10 @@ module Spreemodule Apimodule Webhooksmodule OrderDecorator+ def self.prepended(base)+ base.after_update_commit :queue_webhooks_requests_for_order_resumed!+ end+def after_cancelsuperqueue_webhooks_requests!('order.canceled')@@ -15,6 +19,17 @@ module Spreedef after_resumesuperqueue_webhooks_requests!('order.resumed')+ self.state_machine_resumed = false+ end++ private++ def queue_webhooks_requests_for_order_resumed!+ return if state_machine_resumed?+ return unless state_previously_changed?+ return unless state_previous_change&.last == 'resumed'++ queue_webhooks_requests!('order.resumed')endendenddiff --git a/api/spec/models/spree/api/webhooks/order_decorator_spec.rb b/api/spec/models/spree/api/webhooks/order_decorator_spec.rbindex d64ce87068..ccae91e7f9 100644--- a/api/spec/models/spree/api/webhooks/order_decorator_spec.rb+++ b/api/spec/models/spree/api/webhooks/order_decorator_spec.rb@@ -2,9 +2,7 @@ require 'spec_helper'describe Spree::Api::Webhooks::OrderDecorator dolet(:store) { create(:store, default: true) }- let(:body) do- Spree::Api::V2::Platform::OrderSerializer.new(order).serializable_hash.to_json- end+ let(:body) { Spree::Api::V2::Platform::OrderSerializer.new(order).serializable_hash.to_json }describe 'order.canceled' dodescribe 'completed -> canceled' do@@ -23,10 +21,46 @@ describe Spree::Api::Webhooks::OrderDecorator doenddescribe 'order.resumed' do- describe 'canceled -> resumed' do- let(:order) { create(:order, store: store, state: :canceled) }+ let(:order) { create(:order, store: store, state: :canceled) }++ context 'when order state changes' do+ context 'when order state changes "resumed"' do+ context 'when manually setting the state' do+ # this case does not create a state change record+ subject { Timecop.freeze { order.update(state: 'resumed') } }++ it { expect { subject }.to emit_webhook_event('order.resumed') }+ end++ context 'when doing it through resume!' do+ it do+ expect do+ Timecop.freeze do+ order.resume!+ end+ end.to emit_webhook_event('order.resumed')+ end++ context 'after emitting the webhook' do+ it 'correctly sets state_machine_resumed used to avoid emitting the same event twice' do+ # it does so because an update is for setting the state+ # and another one is for setting the order state_changes+ order.state_machine_resumed = true+ expect(order.state_machine_resumed).to eq(true)+ order.resume!+ expect(order.state_machine_resumed).to eq(false)+ end+ end+ end+ end- it { expect { Timecop.freeze { order.resume! } }.to emit_webhook_event('order.resumed') }+ context 'when order state does not change to "resumed"' do+ it do+ expect do+ order.update(email: '[email protected]')+ end.not_to emit_webhook_event('order.resumed')+ end+ endendendenddiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex 16802a174b..f1d4e374c2 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -73,6 +73,7 @@ module Spreeattr_reader :coupon_codeattr_accessor :temporary_address, :temporary_credit_card+ attribute :state_machine_resumed, :booleanif Spree.user_classbelongs_to :user, class_name: Spree.user_class.to_s, optional: truediff --git a/core/app/models/spree/order/checkout.rb b/core/app/models/spree/order/checkout.rbindex 16cd449040..2ec504070f 100644--- a/core/app/models/spree/order/checkout.rb+++ b/core/app/models/spree/order/checkout.rb@@ -112,6 +112,7 @@ module Spreebefore_transition to: :resumed, do: :ensure_line_item_variants_are_not_discontinuedbefore_transition to: :resumed, do: :ensure_line_items_are_in_stock+ before_transition to: :resumed, do: proc { |order| order.state_machine_resumed = true }after_transition to: :complete, do: :finalize!after_transition to: :resumed, do: :after_resume