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
Use new AddItem service in Shipments API
Damian Legawiec5acc8b4c4899
diff --git a/api/app/controllers/spree/api/v2/platform/shipments_controller.rb b/api/app/controllers/spree/api/v2/platform/shipments_controller.rbindex 6e4d526bec..8022adb5b0 100644--- a/api/app/controllers/spree/api/v2/platform/shipments_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/shipments_controller.rb@@ -5,12 +5,13 @@ module Spreeclass ShipmentsController < ResourceControllerinclude NumberResource- before_action :load_variant, only: %i[add_item remove_item transfer_to_location transfer_to_shipment]+ before_action :load_variant, only: %i[remove_item transfer_to_location transfer_to_shipment]before_action :load_stock_location, only: %i[transfer_to_location]SHIPMENT_STATES = %w[ready ship cancel resume pend]def create+ # FIXME: support saving metadataresult = create_service.call(store: current_store,shipment_attributes: params.require(:shipment).permit(@@ -32,19 +33,14 @@ module Spreeendend- # adds item to an existing shipment- # if this variant is already added to the cart it will increase the quantity- # if not it will create new line item- # def add_item- # quantity = params.dig(:shipment, :quantity)&.to_i || 1-- # Spree::Dependencies.cart_add_item_service.constantize.call(order: resource.order,- # variant: @variant,- # quantity: quantity,- # options: { shipment: resource })-- # render_serialized_payload { serialize_resource(resource) }- # end+ def add_item+ result = add_item_service.call(+ shipment: resource,+ variant_id: params.dig(:shipment, :variant_id),+ quantity: params.dig(:shipment, :quantity)+ )+ render_result(result)+ end# removes an item (variant) from shipment# quantity can be passed@@ -144,6 +140,10 @@ module Spreedef change_state_serviceSpree::Api::Dependencies.platform_shipment_change_state_service.constantizeend++ def add_item_service+ Spree::Api::Dependencies.platform_shipment_add_item_service.constantize+ endendendenddiff --git a/api/config/routes.rb b/api/config/routes.rbindex 1e4c5c2423..9eefea0ccf 100644--- a/api/config/routes.rb+++ b/api/config/routes.rb@@ -264,6 +264,7 @@ Spree::Core::Engine.add_routes do%w[ready ship cancel resume pend].each do |state|patch state.to_symend+ patch :add_itemendenddiff --git a/api/lib/spree/api/api_dependencies.rb b/api/lib/spree/api/api_dependencies.rbindex ba109b7c38..b20b345e05 100644--- a/api/lib/spree/api/api_dependencies.rb+++ b/api/lib/spree/api/api_dependencies.rb@@ -30,7 +30,8 @@ module Spree:platform_order_next_service, :platform_order_advance_service,: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_change_state_service, :platform_shipment_create_service, :platform_shipment_update_service,+ :platform_shipment_add_item_service].freezeattr_accessor *INJECTION_POINTS@@ -155,6 +156,7 @@ module Spree@platform_shipment_create_service = Spree::Dependencies.shipment_create_service@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_serviceendendenddiff --git a/api/spec/integration/api/v2/platform/shipments_spec.rb b/api/spec/integration/api/v2/platform/shipments_spec.rbindex dcd9425878..7dcb53b88a 100644--- a/api/spec/integration/api/v2/platform/shipments_spec.rb+++ b/api/spec/integration/api/v2/platform/shipments_spec.rb@@ -51,6 +51,32 @@ describe 'Shipments API', swagger: true doinclude_examples 'CRUD examples', resource_name, options+ path "/api/v2/platform/#{resource_path}/{id}/add_item" do+ let(:shipment) do+ {+ shipment: {+ variant_id: product.master.id,+ quantity: 1+ }+ }+ end++ patch "Adds item (Variant) to an existing Shipment" do+ tags resource_name.pluralize+ security [ bearer_auth: [] ]+ description "If selected Variant was already added to Order it will increase the quantity of existing Line Item, if not it will create a new Line Item"+ operationId "add-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])++ it_behaves_like 'record updated'+ it_behaves_like 'record not found'+ 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/lib/spree/core/app_dependencies.rb b/core/lib/spree/core/app_dependencies.rbindex 6740085b53..b211cde969 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_create_service, :shipment_add_item_service,:payment_create_service, :address_create_service, :address_update_service,:checkout_select_shipping_method_service].freeze@@ -69,6 +69,7 @@ module Spree@shipment_change_state_service = 'Spree::Shipments::ChangeState'@shipment_create_service = 'Spree::Shipments::Create'@shipment_update_service = 'Spree::Shipments::Update'+ @shipment_add_item_service = 'Spree::Shipments::AddItem'# sorter@collection_sorter = 'Spree::BaseSorter'