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.

Improve `Variants::RemoveLineItems` service

Damian Legawiec710f076b7c1e
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 22fb5bdea0..84ce771497 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -9,6 +9,7 @@ module Spree
class Order < Spree::Base
PAYMENT_STATES = %w(balance_due credit_owed failed paid void)
SHIPMENT_STATES = %w(backorder canceled partial pending ready shipped)
+ LINE_ITEM_REMOVABLE_STATES = %w(cart address delivery payment confirm resumed)
include Spree::Order::Checkout
include Spree::Order::CurrencyUpdater
diff --git a/core/app/services/spree/variants/remove_line_items.rb b/core/app/services/spree/variants/remove_line_items.rb
index d7e00a884e..fbc5039807 100644
--- a/core/app/services/spree/variants/remove_line_items.rb
+++ b/core/app/services/spree/variants/remove_line_items.rb
@@ -4,7 +4,7 @@ module Spree
prepend Spree::ServiceModule::Base
def call(variant:)
- variant.line_items.joins(:order).where.not(spree_orders: { state: 'complete' }).find_each do |line_item|
+ variant.line_items.joins(:order).where(spree_orders: { state: Spree::Order::LINE_ITEM_REMOVABLE_STATES }).find_each do |line_item|
Spree::Variants::RemoveLineItemJob.perform_later(line_item: line_item)
end
diff --git a/core/spec/services/spree/variants/remove_line_items_spec.rb b/core/spec/services/spree/variants/remove_line_items_spec.rb
index 6a4220db23..57da7cb15e 100644
--- a/core/spec/services/spree/variants/remove_line_items_spec.rb
+++ b/core/spec/services/spree/variants/remove_line_items_spec.rb
@@ -6,20 +6,60 @@ module Spree
describe '#call' do
let(:variant) { create(:variant) }
- let(:orders) { create_list(:order_with_line_items, 3) }
- before do
- orders.each { |order| order.line_items.take.update(variant: variant) }
+ context 'when all order states allows to remove line items' do
+ let!(:orders) { create_list(:order_with_line_items, 3) }
+
+ before do
+ orders.each { |order| order.line_items.take.update(variant: variant) }
+ end
+
+ it 'schedules a Spree::LineItems::RemoveFromOrderJob for each order' do
+ expect(Spree::Variants::RemoveLineItemJob).to receive(:perform_later).with(line_item: orders.first.line_items.take).once
+ expect(Spree::Variants::RemoveLineItemJob).to receive(:perform_later).with(line_item: orders.second.line_items.take).once
+ expect(Spree::Variants::RemoveLineItemJob).to receive(:perform_later).with(line_item: orders.third.line_items.take).once
+
+ subject.call(variant: variant)
+ end
+ end
+
+ context 'when none of order states allow to remove line items' do
+ let!(:orders) { create_list(:order_with_line_items, 3, state: 'complete') }
+
+ before do
+ orders.each { |order| order.line_items.take.update(variant: variant) }
+ end
+
+ it 'does not schedule a Spree::LineItems::RemoveFromOrderJob for each order' do
+ expect(Spree::Variants::RemoveLineItemJob).not_to receive(:perform_later)
+ end
end
- it 'schedules a Spree::LineItems::RemoveFromOrderJob for each order' do
- expect(Spree::Variants::RemoveLineItemJob).to receive(:perform_later).with(line_item: orders.first.line_items.take).once
- expect(Spree::Variants::RemoveLineItemJob).to receive(:perform_later).with(line_item: orders.second.line_items.take).once
- expect(Spree::Variants::RemoveLineItemJob).to receive(:perform_later).with(line_item: orders.third.line_items.take).once
+ context 'when some of order states allow to remove line items' do
+ let!(:pending_orders) { create_list(:order_with_line_items, 3, state: 'address') }
+ let!(:cancelled_orders) { create_list(:order_with_line_items, 3, state: 'canceled') }
+
+ before do
+ pending_orders.each { |order| order.line_items.take.update(variant: variant) }
+ cancelled_orders.each { |order| order.line_items.take.update(variant: variant) }
+ end
+
+ it 'schedules a Spree::LineItems::RemoveFromOrderJob for each pending order' do
+ expect(Spree::Variants::RemoveLineItemJob).to receive(:perform_later).with(line_item: pending_orders.first.line_items.take).once
+ expect(Spree::Variants::RemoveLineItemJob).to receive(:perform_later).with(line_item: pending_orders.second.line_items.take).once
+ expect(Spree::Variants::RemoveLineItemJob).to receive(:perform_later).with(line_item: pending_orders.third.line_items.take).once
- subject.call(variant: variant)
+ subject.call(variant: variant)
+ end
+
+ it 'does not schedule a Spree::LineItems::RemoveFromOrderJob for each cancelled order' do
+ expect(Spree::Variants::RemoveLineItemJob).not_to receive(:perform_later).with(line_item: cancelled_orders.first.line_items.take)
+ expect(Spree::Variants::RemoveLineItemJob).not_to receive(:perform_later).with(line_item: cancelled_orders.second.line_items.take)
+ expect(Spree::Variants::RemoveLineItemJob).not_to receive(:perform_later).with(line_item: cancelled_orders.third.line_items.take)
+
+ subject.call(variant: variant)
+ end
end
end
end
end
-