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 product.discountinued on after_update_commit.

Sebastian Palma9b0c8ceeb58e
diff --git a/api/app/models/spree/api/webhooks/product_decorator.rb b/api/app/models/spree/api/webhooks/product_decorator.rb
index 887467aa68..22d1299509 100644
--- a/api/app/models/spree/api/webhooks/product_decorator.rb
+++ b/api/app/models/spree/api/webhooks/product_decorator.rb
@@ -2,8 +2,16 @@ module Spree
module Api
module Webhooks
module ProductDecorator
- def discontinue!
- super
+ def self.prepended(base)
+ base.after_update_commit :queue_webhooks_requests_for_product_discontinued!
+ end
+
+ private
+
+ def queue_webhooks_requests_for_product_discontinued!
+ return unless discontinue_on_previously_changed?
+ return if (change = discontinue_on_previous_change).blank? || change.last.blank?
+
queue_webhooks_requests!('product.discontinued')
end
end
diff --git a/api/spec/models/spree/api/webhooks/product_decorator_spec.rb b/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
index 3ef91bbf10..a429178795 100644
--- a/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
@@ -2,11 +2,36 @@ require 'spec_helper'
describe Spree::Api::Webhooks::ProductDecorator do
let(:product) { create(:product) }
- let(:body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash.to_json }
- describe '#discontinue!' do
- it 'emits the product.discontinued event' do
- expect { product.discontinue! }.to emit_webhook_event('product.discontinued')
+ context 'emitting product.discontinued' do
+ let(:body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash.to_json }
+
+ context 'when product discontinued_on changes' do
+ context 'when the new value is "present"' do
+ it do
+ expect do
+ product.discontinue!
+ end.to emit_webhook_event('product.discontinued')
+ end
+ end
+
+ context 'when the new value is not "present"' do
+ before { product.update(discontinue_on: Date.yesterday) }
+
+ it do
+ expect do
+ product.update(discontinue_on: nil)
+ end.not_to emit_webhook_event('product.discontinued')
+ end
+ end
+ end
+
+ context 'when product discontinued_on does not change' do
+ it do
+ expect do
+ product.update(width: 180)
+ end.not_to emit_webhook_event('product.discontinued')
+ end
end
end
end