Skip to content

Commit

Permalink
Added legacy transfer_to endpoints
Browse files Browse the repository at this point in the history
We need them for admin UI, however we should re-think these endpoints and until then not document them
  • Loading branch information
damianlegawiec committed Nov 15, 2021
1 parent c67c088 commit ce3b464
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
79 changes: 40 additions & 39 deletions api/app/controllers/spree/api/v2/platform/shipments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,46 @@ def remove_item
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("#{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.dig(:shipment, :target_shipment_number))
quantity = params.dig(:shipment, :quantity)&.to_i || 1

error =
if quantity < 0 && target_shipment == resource
"#{Spree.t(:negative_quantity, scope: 'api')}, \n#{Spree.t('wrong_shipment_target', scope: 'api')}"
elsif target_shipment == resource
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 = resource.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

private

Expand Down
2 changes: 2 additions & 0 deletions api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@
end
patch :add_item
patch :remove_item
patch :transfer_to_location
patch :transfer_to_shipment
end
end

Expand Down
2 changes: 1 addition & 1 deletion core/app/services/spree/shipments/remove_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def remove_item(shipment:, variant:, quantity:)
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)
Expand Down

0 comments on commit ce3b464

Please sign in to comment.