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

Sebastian Palma2b1323860ace
diff --git a/api/app/models/spree/api/webhooks/variant_decorator.rb b/api/app/models/spree/api/webhooks/variant_decorator.rb
index dfa8548a07..7e041037de 100644
--- a/api/app/models/spree/api/webhooks/variant_decorator.rb
+++ b/api/app/models/spree/api/webhooks/variant_decorator.rb
@@ -2,8 +2,16 @@ module Spree
module Api
module Webhooks
module VariantDecorator
- def discontinue!
- super
+ def self.prepended(base)
+ base.after_update_commit :queue_webhooks_requests_for_variant_discontinued!
+ end
+
+ private
+
+ def queue_webhooks_requests_for_variant_discontinued!
+ return unless discontinue_on_previously_changed?
+ return if (change = discontinue_on_previous_change).blank? || change.last.blank?
+
queue_webhooks_requests!('variant.discontinued')
end
end
diff --git a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb
index c25e3e20ba..2acf7a88f9 100644
--- a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb
@@ -6,13 +6,11 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
let(:stock_movement) { create(:stock_movement, stock_item: stock_item, quantity: movement_quantity) }
let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash.to_json }
- let!(:images) { create_list(:image, 2) }
-
describe 'emitting variant.back_in_stock' do
let(:variant) { create(:variant, track_inventory: true) }
context 'when stock item was out of stock' do
- context 'when variant changes to be in stock' do
+ context 'when stock item changes to be in stock' do
it do
expect do
Timecop.freeze do
@@ -27,7 +25,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
end
end
- context 'when variant does not change to be in stock' do
+ context 'when stock item does not change to be in stock' do
it do
expect do
Timecop.freeze do
@@ -43,7 +41,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
end
end
- context 'when variant was in stock' do
+ context 'when stock item was in stock' do
it do
expect do
Timecop.freeze do
diff --git a/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb b/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb
index f0a7e95f23..d26842a34c 100644
--- a/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb
@@ -3,13 +3,35 @@ require 'spec_helper'
describe Spree::Api::Webhooks::VariantDecorator do
let(:variant) { create(:variant) }
- describe '#discontinue!' do
- context 'emitting variant.discontinued' do
- subject { variant.discontinue! }
+ context 'emitting variant.discontinued' do
+ let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash.to_json }
- let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash.to_json }
+ context 'when variant discontinued_on changes' do
+ context 'when the new value is "present"' do
+ it do
+ expect do
+ variant.discontinue!
+ end.to emit_webhook_event('variant.discontinued')
+ end
+ end
- it { expect { subject }.to emit_webhook_event('variant.discontinued') }
+ context 'when the new value is not "present"' do
+ before { variant.update(discontinue_on: Date.yesterday) }
+
+ it do
+ expect do
+ variant.update(discontinue_on: nil)
+ end.not_to emit_webhook_event('variant.discontinued')
+ end
+ end
+ end
+
+ context 'when variant discontinued_on does not change' do
+ it do
+ expect do
+ variant.update(width: 180)
+ end.not_to emit_webhook_event('variant.discontinued')
+ end
end
end
end