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
Added Shipments::RemoveItem service
Damian Legawiecd4c61c7a5ff5
diff --git a/core/app/services/spree/shipments/add_item.rb b/core/app/services/spree/shipments/add_item.rbindex b4b3bf08b7..44db493a57 100644--- a/core/app/services/spree/shipments/add_item.rb+++ b/core/app/services/spree/shipments/add_item.rb@@ -7,7 +7,7 @@ module Spreeprepend Spree::ServiceModule::Baseinclude Helper- def call(shipment:, variant_id:, quantity:)+ def call(shipment:, variant_id:, quantity: nil)ActiveRecord::Base.transaction dorun :prepare_argumentsrun :add_or_update_line_item@@ -16,7 +16,7 @@ module Spreeprotected- def prepare_arguments(shipment:, variant_id:, quantity:)+ def prepare_arguments(shipment:, variant_id:, quantity: nil)order = shipment.orderstore = order.storediff --git a/core/app/services/spree/shipments/helper.rb b/core/app/services/spree/shipments/helper.rbindex 44786472e1..8e9c126b8d 100644--- a/core/app/services/spree/shipments/helper.rb+++ b/core/app/services/spree/shipments/helper.rb@@ -14,6 +14,10 @@ module Spreedef add_item_serviceSpree::Dependencies.cart_add_item_service.constantizeend++ def remove_item_service+ Spree::Dependencies.cart_remove_item_service.constantize+ endendendenddiff --git a/core/app/services/spree/shipments/remove_item.rb b/core/app/services/spree/shipments/remove_item.rbnew file mode 100644index 0000000000..bdb142d54e--- /dev/null+++ b/core/app/services/spree/shipments/remove_item.rb@@ -0,0 +1,55 @@+# removes an item (variant) from shipment+# quantity can be passed+# if there is no quantity left for this item for this shipent, shipment itself will be removed+module Spree+ module Shipments+ class RemoveItem+ prepend Spree::ServiceModule::Base+ include Helper++ def call(shipment:, variant_id:, quantity: nil)+ ActiveRecord::Base.transaction do+ run :prepare_arguments+ run :remove_item+ end+ end++ protected++ def prepare_arguments(shipment:, variant_id:, quantity: nil)+ variant = Spree::Variant.find_by(id: variant_id)+ return failure(nil, :variant_not_found) if variant.nil? || !shipment.include?(variant)++ # if quantity isn't passed let's remove all added qty of this variant+ quantity = quantity&.to_i || shipment.inventory_units_for(variant).sum(:quantity)++ success(shipment: shipment, variant: variant, quantity: quantity)+ end++ # if the removed variant was the only one shipped via this shipment+ # we need to remove the shipment itself+ def remove_item(shipment:, variant:, quantity:)+ result = remove_item_service.call(order: shipment.order,+ variant: variant,+ quantity: quantity,+ options: { shipment: shipment })++ if result.success?+ line_item = result.value+ line_item.destroy if line_item.quantity.zero?++ # OrderInventory#remove_from_shipment is caleed in `remove_item_service`+ # which will delete the shipment if all inventory units were removed+ if shipment.inventory_units.any?+ success(shipment)+ else+ shipment.destroy!+ success(:shipment_deleted)+ end+ else+ failure(result.value, result.error)+ end+ end+ end+ end+enddiff --git a/core/spec/services/spree/shipments/add_item_spec.rb b/core/spec/services/spree/shipments/add_item_spec.rbindex 209f02e9a9..f43e3f141f 100644--- a/core/spec/services/spree/shipments/add_item_spec.rb+++ b/core/spec/services/spree/shipments/add_item_spec.rb@@ -25,6 +25,8 @@ module Spreecontext 'valid attributes' doshared_context 'creates a line item' do+ it { expect(execute.success?).to eq(true) }+it 'creates new line item record' doexpect { execute }.to change(order.line_items, :count).by(1)expect(line_item.variant).to eq(variant)@@ -36,6 +38,23 @@ module Spreeit_behaves_like 'creates a line item'end+ context 'without quantity passed' do+ let(:params) do+ {+ shipment: shipment,+ variant_id: variant.id+ }+ end++ it { expect(execute.success?).to eq(true) }++ it 'creates new line item record' do+ expect { execute }.to change(order.line_items, :count).by(1)+ expect(line_item.variant).to eq(variant)+ expect(line_item.quantity).to eq(1)+ end+ end+context 'with existing line item' dolet!(:old_line_item) { create(:line_item, order: order, variant: variant, quantity: 2) }diff --git a/core/spec/services/spree/shipments/remove_item_spec.rb b/core/spec/services/spree/shipments/remove_item_spec.rbnew file mode 100644index 0000000000..9009d22270--- /dev/null+++ b/core/spec/services/spree/shipments/remove_item_spec.rb@@ -0,0 +1,97 @@+require 'spec_helper'++module Spree+ describe Shipments::RemoveItem do+ subject { described_class }++ let(:store) { create(:store) }+ let(:order) { create(:order_ready_to_ship, store: store, user: nil, email: '[email protected]') }+ let(:variant) { shipment.line_items.first.variant }+ let(:shipment) { order.shipments.first }+ let(:line_item) { shipment.line_items.last }++ let(:execute) { subject.call(params) }+ let(:value) { execute.value }++ let(:params) do+ {+ shipment: shipment,+ variant_id: variant.id,+ quantity: line_item.quantity+ }+ end++ context 'valid attributes' do+ shared_examples 'successful' do+ it { expect(execute.success?).to eq(true) }+ end++ shared_examples 'removes line item' do+ it { expect { execute }.to change(order.line_items, :count).by(-1) }+ end++ shared_examples 'removes shipment' do+ it { expect { execute }.to change(order.shipments, :count).by(-1) }+ it { expect(execute.value).to eq(:shipment_deleted) }+ end++ context 'part of the line item qty removed' do+ before { line_item.update(quantity: 2) }++ let(:params) do+ {+ shipment: shipment,+ variant_id: variant.id,+ quantity: 1+ }+ end++ it_behaves_like 'successful'++ it { expect { execute }.not_to change(order.shipments, :count) }+ it { expect { execute }.not_to change(order.line_items, :count) }++ it 'decreases line item quantity' do+ expect(line_item.quantity).to eq(2)+ execute+ expect(line_item.reload.quantity).to eq(1)+ end++ it { expect(execute.value).to be_kind_of(Spree::Shipment) }+ it { expect(execute.value.id).to eq(shipment.id) }+ end++ context 'entire shipment & line item qty removed' do+ it_behaves_like 'successful'+ it_behaves_like 'removes line item'+ it_behaves_like 'removes shipment'+ end++ context 'no quantity is passed' do+ let(:params) do+ {+ shipment: shipment,+ variant_id: variant.id+ }+ end++ it_behaves_like 'successful'+ it_behaves_like 'removes line item'+ it_behaves_like 'removes shipment'+ end+ end++ context 'missing variant' do+ let(:params) do+ {+ shipment: shipment,+ variant_id: 141223453,+ quantity: 2+ }+ end++ it { expect(execute.success?).to eq(false) }+ it { expect(execute.error.to_s).to eq('variant_not_found') }+ end+ end+end