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.

Added legacy transfer_to endpoints

Damian Legawiec0f3e583c15ae
diff --git a/api/app/controllers/spree/api/v2/platform/shipments_controller.rb b/api/app/controllers/spree/api/v2/platform/shipments_controller.rb
index 2038cef059..19e4d43d77 100644
--- a/api/app/controllers/spree/api/v2/platform/shipments_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/shipments_controller.rb
@@ -60,45 +60,46 @@ module Spree
end
end
- # def transfer_to_location
- # quantity = params.dig(:shipment, :quantity)&.to_i || 1
-
- # unless quantity > 0
- # 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!
- # render json: { message: Spree.t(:shipment_transfer_success) }, status: 201
- # else
- # render_error_payload(transfer.errors)
- # end
- # end
-
- # def transfer_to_shipment
- # @target_shipment = Spree::Shipment.find_by!(number: params[:target_shipment_number])
-
- # error =
- # if @quantity < 0 && @target_shipment == @original_shipment
- # "#{Spree.t(:negative_quantity, scope: 'api')}, \n#{Spree.t('wrong_shipment_target', scope: 'api')}"
- # elsif @target_shipment == @original_shipment
- # Spree.t(:wrong_shipment_target, scope: 'api')
- # elsif @quantity < 0
- # Spree.t(:negative_quantity, scope: 'api')
- # end
-
- # if 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!
- # render json: { message: Spree.t(:shipment_transfer_success) }, status: 201
- # else
-# render_error_payload(transfer.errors)
- # end
- # end
- # end
+ def transfer_to_location
+ quantity = params.dig(:shipment, :quantity)&.to_i || 1
+
+ unless quantity > 0
+ render_error_payload("#{I18n.t('spree.api.shipment_transfer_errors_occurred')} \n #{I18n.t('spree.api.negative_quantity')}")
+ return
+ end
+
+ transfer = resource.transfer_to_location(@variant, quantity, @stock_location)
+ if transfer.valid? && transfer.run!
+ render json: { message: I18n.t('spree.api.shipment_transfer_success') }, status: 201
+ else
+ render_error_payload(transfer.errors)
+ end
+ end
+
+ def transfer_to_shipment
+ target_shipment = Spree::Shipment.find_by!(number: params.dig(:shipment, :target_shipment_number))
+ quantity = params.dig(:shipment, :quantity)&.to_i || 1
+
+ error =
+ if quantity < 0 && target_shipment == resource
+ "#{I18n.t('spree.api.negative_quantity')}, \n#{I18n.t('spree.api.wrong_shipment_target')}"
+ elsif target_shipment == resource
+ I18n.t('spree.api.wrong_shipment_target')
+ elsif quantity < 0
+ I18n.t('spree.api.negative_quantity')
+ end
+
+ if error
+ render_error_payload("#{I18n.t('spree.api.shipment_transfer_errors_occurred')} \n#{error}")
+ else
+ transfer = resource.transfer_to_shipment(@variant, quantity, target_shipment)
+ if transfer.valid? && transfer.run!
+ render json: { message: I18n.t('spree.api.shipment_transfer_success') }, status: 201
+ else
+ render_error_payload(transfer.errors)
+ end
+ end
+ end
private
diff --git a/api/config/routes.rb b/api/config/routes.rb
index 2e133debc4..235c9a801b 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -266,6 +266,8 @@ Spree::Core::Engine.add_routes do
end
patch :add_item
patch :remove_item
+ patch :transfer_to_location
+ patch :transfer_to_shipment
end
end
diff --git a/core/app/services/spree/shipments/remove_item.rb b/core/app/services/spree/shipments/remove_item.rb
index bdb142d54e..959b5c3dcc 100644
--- a/core/app/services/spree/shipments/remove_item.rb
+++ b/core/app/services/spree/shipments/remove_item.rb
@@ -38,7 +38,7 @@ module Spree
line_item = result.value
line_item.destroy if line_item.quantity.zero?
- # OrderInventory#remove_from_shipment is caleed in `remove_item_service`
+ # `OrderInventory#remove_from_shipment` is called in `remove_item_service`
# which will delete the shipment if all inventory units were removed
if shipment.inventory_units.any?
success(shipment)