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 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.rbindex 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 Spreemodule Apimodule Webhooksmodule 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')endenddiff --git a/api/spec/models/spree/api/webhooks/product_decorator_spec.rb b/api/spec/models/spree/api/webhooks/product_decorator_spec.rbindex 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 dolet(: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')+ endendendend