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
VEN-225 Add ability to delete variants attached to line items of incomplete orders (#11578)
Aleksandar Petrushev4f9bbe8b3357
diff --git a/core/app/jobs/spree/variants/remove_from_incomplete_orders_job.rb b/core/app/jobs/spree/variants/remove_from_incomplete_orders_job.rbnew file mode 100644index 0000000000..8aa9f38bea--- /dev/null+++ b/core/app/jobs/spree/variants/remove_from_incomplete_orders_job.rb@@ -0,0 +1,9 @@+module Spree+ module Variants+ class RemoveFromIncompleteOrdersJob < Spree::BaseJob+ def perform(variant)+ Spree::Variants::RemoveLineItems.call(variant: variant)+ end+ end+ end+enddiff --git a/core/app/jobs/spree/variants/remove_line_item_job.rb b/core/app/jobs/spree/variants/remove_line_item_job.rbnew file mode 100644index 0000000000..c7118154ac--- /dev/null+++ b/core/app/jobs/spree/variants/remove_line_item_job.rb@@ -0,0 +1,9 @@+module Spree+ module Variants+ class RemoveLineItemJob < Spree::BaseJob+ def perform(line_item:)+ Spree::Dependencies.cart_remove_line_item_service.constantize.call(order: line_item.order, line_item: line_item)+ end+ end+ end+enddiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex a0e5c11928..49b4333cbf 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -42,7 +42,7 @@ module Spree# we need to have this callback before any dependent: :destroy associations# https://github.com/rails/rails/issues/3458- before_destroy :ensure_no_line_items+ before_destroy :ensure_not_in_complete_ordershas_many :product_option_types, dependent: :destroy, inverse_of: :producthas_many :option_types, through: :product_option_types@@ -485,8 +485,8 @@ module SpreeSpree::Taxonomy.where(id: taxonomy_ids).update_all(updated_at: Time.current)end- def ensure_no_line_items- if line_items.any?+ def ensure_not_in_complete_orders+ if orders.complete.any?errors.add(:base, :cannot_destroy_if_attached_to_line_items)throw(:abort)enddiff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 65525d0252..f137f4f514 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -19,9 +19,10 @@ module Spree# we need to have this callback before any dependent: :destroy associations# https://github.com/rails/rails/issues/3458- before_destroy :ensure_no_line_items+ before_destroy :ensure_not_in_complete_orders+ after_destroy :remove_line_items_from_incomplete_orders- # must include this after ensure_no_line_items to make sure price won't be deleted before validation+ # must include this after ensure_not_in_complete_orders to make sure price won't be deleted before validationinclude Spree::DefaultPricewith_options inverse_of: :variant do@@ -328,13 +329,17 @@ module Spreeprivate- def ensure_no_line_items- if line_items.any?+ def ensure_not_in_complete_orders+ if orders.complete.any?errors.add(:base, :cannot_destroy_if_attached_to_line_items)throw(:abort)endend+ def remove_line_items_from_incomplete_orders+ Spree::Variants::RemoveFromIncompleteOrdersJob.perform_later(self)+ end+def quantifierSpree::Stock::Quantifier.new(self)enddiff --git a/core/app/services/spree/variants/remove_line_items.rb b/core/app/services/spree/variants/remove_line_items.rbnew file mode 100644index 0000000000..d7e00a884e--- /dev/null+++ b/core/app/services/spree/variants/remove_line_items.rb@@ -0,0 +1,15 @@+module Spree+ module Variants+ class RemoveLineItems+ prepend Spree::ServiceModule::Base++ def call(variant:)+ variant.line_items.joins(:order).where.not(spree_orders: { state: 'complete' }).find_each do |line_item|+ Spree::Variants::RemoveLineItemJob.perform_later(line_item: line_item)+ end++ success(true)+ end+ end+ end+enddiff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex a235391b18..77ed520de6 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -361,7 +361,7 @@ en:discontinue_on:invalid_date_range: must be later than available datebase:- cannot_destroy_if_attached_to_line_items: Cannot delete products once they are attached to line items.+ cannot_destroy_if_attached_to_line_items: Cannot delete Products that are added to placed Orders. In such cases, please discontinue them.spree/refund:attributes:amount:@@ -398,7 +398,7 @@ en:spree/variant:attributes:base:- cannot_destroy_if_attached_to_line_items: Cannot delete variants once they are attached to line items.+ cannot_destroy_if_attached_to_line_items: Cannot delete Variants that are added to placed Orders. In such cases, please discontinue them.no_master_variant_found_to_infer_price: No master variant found to infer pricemust_supply_price_for_variant_or_master: Must supply price for variant or master price for product.spree/image:diff --git a/core/spec/jobs/spree/variants/remove_from_incomplete_orders_job_spec.rb b/core/spec/jobs/spree/variants/remove_from_incomplete_orders_job_spec.rbnew file mode 100644index 0000000000..f4181875e4--- /dev/null+++ b/core/spec/jobs/spree/variants/remove_from_incomplete_orders_job_spec.rb@@ -0,0 +1,14 @@+require 'spec_helper'++module Spree+ describe Variants::RemoveFromIncompleteOrdersJob, :job do+ let!(:variant) { orders.first.line_items.first.variant }+ let!(:orders) { [create(:order_with_line_items)] }++ it 'enqueues the removal of variants line items' do+ expect { described_class.perform_later(variant) }.to(+ have_enqueued_job.on_queue('default')+ )+ end+ end+enddiff --git a/core/spec/jobs/spree/variants/remove_line_item_job_spec.rb b/core/spec/jobs/spree/variants/remove_line_item_job_spec.rbnew file mode 100644index 0000000000..36c917dad0--- /dev/null+++ b/core/spec/jobs/spree/variants/remove_line_item_job_spec.rb@@ -0,0 +1,17 @@+require 'spec_helper'++module Spree+ describe Variants::RemoveLineItemJob, :job do+ let!(:order) { create(:order_with_line_items) }+ let!(:line_item) { order.line_items.take }+ let(:remove_line_item_service_double) { double('RemoveLineItemService') }++ it 'calls the cart_remove_item_service service' do+ expect(Spree::Dependencies.cart_remove_line_item_service).to receive(:constantize).and_return(remove_line_item_service_double)+ expect(remove_line_item_service_double).to receive(:call).with(order: order, line_item: line_item)++ described_class.perform_now(line_item: line_item)+ end+ end+end+diff --git a/core/spec/models/spree/order_merger_spec.rb b/core/spec/models/spree/order_merger_spec.rbindex 20eddff2d4..13d104dfce 100644--- a/core/spec/models/spree/order_merger_spec.rb+++ b/core/spec/models/spree/order_merger_spec.rb@@ -124,9 +124,7 @@ module Spreeendit 'creates errors with invalid line items' do- # we cannot use .destroy here as it will be halted by- # :ensure_no_line_items callback- variant_2.really_destroy!+ variant_2.destroy!subject.merge!(order_2)expect(order_1.errors.full_messages).not_to be_emptyenddiff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rbindex 3abd7317d3..fe2df34740 100644--- a/core/spec/models/spree/product_spec.rb+++ b/core/spec/models/spree/product_spec.rb@@ -311,7 +311,7 @@ describe Spree::Product, type: :model docontext 'hard deletion' doit 'doesnt raise ActiveRecordError error' do- expect { product.really_destroy! }.not_to raise_error+ expect { product.really_destroy! }.not_to raise_error(ActiveRecord::ActiveRecordError)endend@@ -659,9 +659,10 @@ describe Spree::Product, type: :model doendend- describe '#ensure_no_line_items' do+ describe '#ensure_not_in_complete_orders' do+ let!(:order) { create(:completed_order_with_totals) }let(:product) { create(:product, stores: [store]) }- let!(:line_item) { create(:line_item, variant: product.master, product: product) }+ let!(:line_item) { create(:line_item, order: order, variant: product.master, product: product) }it 'adds error on product destroy' doexpect(product.destroy).to eq falsediff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex ec5da032d0..b2d7a87491 100644--- a/core/spec/models/spree/variant_spec.rb+++ b/core/spec/models/spree/variant_spec.rb@@ -1021,12 +1021,32 @@ describe Spree::Variant, type: :model doendend- describe '#ensure_no_line_items' do- let!(:line_item) { create(:line_item, variant: variant) }+ describe '#ensure_not_in_complete_orders' do+ let!(:order) { create(:completed_order_with_totals) }+ let!(:line_item) { create(:line_item, order: order, variant: variant) }- it 'adds error on product destroy' do+ it 'adds error on variant destroy' doexpect(variant.destroy).to eq falseexpect(variant.errors[:base]).to include I18n.t('activerecord.errors.models.spree/variant.attributes.base.cannot_destroy_if_attached_to_line_items')endend++ describe '#remove_line_items_from_incomplete_orders' do+ let!(:order) { create(:order) }+ let!(:line_item) { create(:line_item, order: order, variant: variant, quantity: 2) }+ let!(:line_item_2) { create(:line_item, order: order, variant: variant, quantity: 3) }++ before { variant.update(track_inventory: false) }++ it 'schedules RemoveFromIncompleteOrdersJob' do+ expect(Spree::Variants::RemoveFromIncompleteOrdersJob).to receive(:perform_later).with(variant)+ variant.destroy+ end++ it 'deletes the line items from the order' do+ variant.destroy+ expect(order.reload.line_items).to be_empty+ expect(order.total).to eq(0)+ end+ endenddiff --git a/core/spec/services/spree/variants/remove_line_items_spec.rb b/core/spec/services/spree/variants/remove_line_items_spec.rbnew file mode 100644index 0000000000..6a4220db23--- /dev/null+++ b/core/spec/services/spree/variants/remove_line_items_spec.rb@@ -0,0 +1,25 @@+require 'spec_helper'++module Spree+ describe Variants::RemoveLineItems do+ subject { described_class }++ 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) }+ 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+ end+end+