app/controllers/spree/api/shipments_controller.rb in solidus_api-2.3.1 vs app/controllers/spree/api/shipments_controller.rb in solidus_api-2.4.0.beta1
- old
+ new
@@ -79,18 +79,29 @@
invalid_resource!(@shipment)
end
end
def transfer_to_location
- @stock_location = Spree::StockLocation.find(params[:stock_location_id])
- @original_shipment.transfer_to_location(@variant, @quantity, @stock_location)
- render json: { success: true, message: Spree.t(:shipment_transfer_success) }, status: 201
+ @desired_stock_location = Spree::StockLocation.find(params[:stock_location_id])
+ @desired_shipment = @original_shipment.order.shipments.build(stock_location: @desired_stock_location)
+ transfer_to_shipment
end
def transfer_to_shipment
- @target_shipment = Spree::Shipment.find_by!(number: params[:target_shipment_number])
- @original_shipment.transfer_to_shipment(@variant, @quantity, @target_shipment)
- render json: { success: true, message: Spree.t(:shipment_transfer_success) }, status: 201
+ @desired_shipment ||= Spree::Shipment.find_by!(number: params[:target_shipment_number])
+
+ fulfilment_changer = Spree::FulfilmentChanger.new(
+ current_shipment: @original_shipment,
+ desired_shipment: @desired_shipment,
+ variant: @variant,
+ quantity: @quantity
+ )
+
+ if fulfilment_changer.run!
+ render json: { success: true, message: Spree.t(:shipment_transfer_success) }, status: :accepted
+ else
+ render json: { success: false, message: fulfilment_changer.errors.full_messages.to_sentence }, status: :accepted
+ end
end
private
def load_transfer_params