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 Shipment Remove Item endpoint
Damian Legawiecb4b43bb8405a
diff --git a/api/app/controllers/spree/api/v2/platform/shipments_controller.rb b/api/app/controllers/spree/api/v2/platform/shipments_controller.rbindex 8022adb5b0..2038cef059 100644--- a/api/app/controllers/spree/api/v2/platform/shipments_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/shipments_controller.rb@@ -42,45 +42,37 @@ module Spreerender_result(result)end- # 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- # TODO: move this code- # def remove_item- # quantity = params.dig(:shipment, :quantity)&.to_i || resource.inventory_units_for(variant).sum(:quantity)-- # result = Spree::Dependencies.cart_remove_item_service.constantize.call(order: resource.order,- # variant: @variant,- # quantity: quantity,- # options: { shipment: resource })-- # if result.success?- # if resource.inventory_units.any?- # render_serialized_payload { serialize_resource(resource.reload) }- # elsif resource.detroy!- # head 204- # else- # render_error_payload(resource)- # end- # else- # render_error_payload(result.error)- # end- # end+ def remove_item+ result = remove_item_service.call(+ shipment: resource,+ variant_id: params.dig(:shipment, :variant_id),+ quantity: params.dig(:shipment, :quantity)+ )++ if result.success?+ if result.value == :shipment_deleted+ head 204+ else+ render_serialized_payload { serialize_resource(result.value) }+ end+ else+ render_error_payload(result.error)+ end+ end# def transfer_to_location# quantity = params.dig(:shipment, :quantity)&.to_i || 1# unless quantity > 0- # unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occurred, scope: 'api')} \n #{Spree.t(:negative_quantity, scope: 'api')}")+ # render_error_payload("#{Spree.t(:shipment_transfer_errors_occurred, scope: 'api')} \n #{Spree.t(:negative_quantity, scope: 'api')}")# return# end# transfer = resource.transfer_to_location(@variant, quantity, stock_location)- # if transfer.valid?- # transfer.run!+ # if transfer.valid? && transfer.run!# render json: { message: Spree.t(:shipment_transfer_success) }, status: 201# else- # render json: { message: transfer.errors.full_messages.to_sentence }, status: 422+ # render_error_payload(transfer.errors)# end# end@@ -97,14 +89,13 @@ module Spree# end# if error- # unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occurred, scope: 'api')} \n#{error}")+ # render_error_payload("#{Spree.t(:shipment_transfer_errors_occurred, scope: 'api')} \n#{error}")# else# transfer = @original_shipment.transfer_to_shipment(@variant, @quantity, @target_shipment)- # if transfer.valid?- # transfer.run!+ # if transfer.valid? && transfer.run!# render json: { message: Spree.t(:shipment_transfer_success) }, status: 201# else- # render json: { message: transfer.errors.full_messages }, status: 422+# render_error_payload(transfer.errors)# end# end# end@@ -144,6 +135,10 @@ module Spreedef add_item_serviceSpree::Api::Dependencies.platform_shipment_add_item_service.constantizeend++ def remove_item_service+ Spree::Api::Dependencies.platform_shipment_remove_item_service.constantize+ endendendenddiff --git a/api/config/routes.rb b/api/config/routes.rbindex 9eefea0ccf..2e133debc4 100644--- a/api/config/routes.rb+++ b/api/config/routes.rb@@ -265,6 +265,7 @@ Spree::Core::Engine.add_routes dopatch state.to_symendpatch :add_item+ patch :remove_itemendenddiff --git a/api/lib/spree/api/api_dependencies.rb b/api/lib/spree/api/api_dependencies.rbindex b20b345e05..308ea0a527 100644--- a/api/lib/spree/api/api_dependencies.rb+++ b/api/lib/spree/api/api_dependencies.rb@@ -31,7 +31,7 @@ module Spree:platform_line_item_create_service, :platform_line_item_update_service, :platform_line_item_destroy_service,:platform_order_approve_service, :platform_order_cancel_service,:platform_shipment_change_state_service, :platform_shipment_create_service, :platform_shipment_update_service,- :platform_shipment_add_item_service+ :platform_shipment_add_item_service, :platform_shipment_remove_item_service].freezeattr_accessor *INJECTION_POINTS@@ -157,6 +157,7 @@ module Spree@platform_shipment_update_service = Spree::Dependencies.shipment_update_service@platform_shipment_change_state_service = Spree::Dependencies.shipment_change_state_service@platform_shipment_add_item_service = Spree::Dependencies.shipment_add_item_service+ @platform_shipment_remove_item_service = Spree::Dependencies.shipment_remove_item_serviceendendenddiff --git a/api/spec/integration/api/v2/platform/shipments_spec.rb b/api/spec/integration/api/v2/platform/shipments_spec.rbindex 7dcb53b88a..466328f520 100644--- a/api/spec/integration/api/v2/platform/shipments_spec.rb+++ b/api/spec/integration/api/v2/platform/shipments_spec.rb@@ -77,6 +77,66 @@ describe 'Shipments API', swagger: true doendend+ path "/api/v2/platform/#{resource_path}/{id}/remove_item" do+ let(:shipment_record) { order.shipments.find(id) }+ let(:line_item) { shipment_record.line_items.last }++ let(:shipment) do+ {+ shipment: {+ variant_id: line_item.variant_id,+ }+ }+ end++ patch "Removes item (Variant) from Shipment" do+ tags resource_name.pluralize+ security [ bearer_auth: [] ]+ description "If selected Variant is removed completely and Shipment doesn't include any other Line Items, Shipment itself will be deleted"+ operationId "remove-item-shipment"+ consumes 'application/json'+ parameter name: :id, in: :path, type: :string+ parameter name: :shipment, in: :body#, schema: custom_update_params+ json_api_include_parameter(options[:include_example])++ context 'removes all the quantity' do+ it_behaves_like 'record deleted'+ end++ context 'removes only part of the quantity' do+ before do+ line_item.variant.stock_items.update_all(count_on_hand: 100)+ line_item.update_column(:quantity, 3)+ end++ let(:shipment) do+ {+ shipment: {+ variant_id: line_item.variant_id,+ quantity: 1+ }+ }+ end++ it_behaves_like 'record updated'+ end++ context '404' do+ let(:shipment) do+ {+ shipment: {+ variant_id: '1',+ }+ }+ end++ it_behaves_like 'record not found'+ end++ it_behaves_like 'authentication failed'+ end+ end+path "/api/v2/platform/#{resource_path}/{id}/ready" dopatch "Mark Shipment as ready to be shipped" dotags resource_name.pluralizediff --git a/core/app/models/spree/order_inventory.rb b/core/app/models/spree/order_inventory.rbindex f2a4a22a13..e00db54ece 100644--- a/core/app/models/spree/order_inventory.rb+++ b/core/app/models/spree/order_inventory.rb@@ -120,7 +120,7 @@ module Spreeif order.completed?current_on_hand = shipment.stock_location.count_on_hand(variant)- if current_on_hand.negative? && current_on_hand.abs < removed_backordered+ if current_on_hand&.negative? && current_on_hand.abs < removed_backorderedshipment.stock_location.restock_backordered variant, current_on_hand.abs, shipmentelseshipment.stock_location.restock_backordered variant, removed_backordered, shipmentdiff --git a/core/lib/spree/core/app_dependencies.rb b/core/lib/spree/core/app_dependencies.rbindex b211cde969..baeb1be363 100644--- a/core/lib/spree/core/app_dependencies.rb+++ b/core/lib/spree/core/app_dependencies.rb@@ -16,7 +16,7 @@ module Spree:classification_reposition_service, :credit_cards_destroy_service, :cart_associate_service, :cart_change_currency_service,:line_item_create_service, :line_item_update_service, :line_item_destroy_service,:order_approve_service, :order_cancel_service, :shipment_change_state_service, :shipment_update_service,- :shipment_create_service, :shipment_add_item_service,+ :shipment_create_service, :shipment_add_item_service, :shipment_remove_item_service,:payment_create_service, :address_create_service, :address_update_service,:checkout_select_shipping_method_service].freeze@@ -70,6 +70,7 @@ module Spree@shipment_create_service = 'Spree::Shipments::Create'@shipment_update_service = 'Spree::Shipments::Update'@shipment_add_item_service = 'Spree::Shipments::AddItem'+ @shipment_remove_item_service = 'Spree::Shipments::RemoveItem'# sorter@collection_sorter = 'Spree::BaseSorter'diff --git a/core/spec/services/spree/shipments/remove_item_spec.rb b/core/spec/services/spree/shipments/remove_item_spec.rbindex 9009d22270..055592c43b 100644--- a/core/spec/services/spree/shipments/remove_item_spec.rb+++ b/core/spec/services/spree/shipments/remove_item_spec.rb@@ -36,7 +36,7 @@ module Spreeendcontext 'part of the line item qty removed' do- before { line_item.update(quantity: 2) }+ before { line_item.update!(quantity: 2) }let(:params) do{