Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

Commit

Permalink
Make update shipment handler accept inventory units
Browse files Browse the repository at this point in the history
In
a1c78bb,
we updated the shipment serializer to push inventory units, so if we are
updating, we should use them as well in case there is a variant mismatch
as there is with exchanges

If a shipment happens to be created using the add_shipment_handler
instead of pushed with the serializer, even though that builds inventory
units from the line items matching the shipment items skus, we should
still be ok because it should match the inventory units similarly to how
it previously matched line items.

Change update shipment handler to expect flattened inventory items, since that is what is generated in the serializer

Make sure to cast quantity to an integer

Handle multiple inventory units of the same sku and quantity in update shipment handler

Fixes #39
  • Loading branch information
Andrew Thal authored and Jeff Dutil committed Sep 16, 2014
1 parent 2431c56 commit 918f215
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
25 changes: 16 additions & 9 deletions lib/spree/wombat/handler/update_shipment_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def process
shipment_attributes["address_attributes"] = address_attributes if address_attributes

missing_variants = []
missing_line_items = []
missing_inventory_units = []

shipping_items = shipment_hsh.delete(:items)
if shipping_items
Expand All @@ -65,24 +65,31 @@ def process
next
end

line_item_id = shipment.order.line_items.where(variant_id: variant.id).pluck(:id).first
unless line_item_id
missing_line_items << sku
inventory_unit_id = shipment.inventory_units.where(variant_id: variant.id).pluck(:id).first
unless inventory_unit_id
missing_inventory_units << sku
next
end
end

# check on items sku and quantity
shipment_lines = shipment.line_items.map { |li| {sku: li.variant.sku, quantity: li.quantity} }
received_shipping_items = shipping_items.map { |item| {sku: item[:product_id], quantity: item[:quantity]} }
shipment_lines = shipment.inventory_units.map do |inventory_unit|
quantity = inventory_unit.respond_to?(:quantity) ? inventory_unit.quantity : 1
{ sku: inventory_unit.variant.sku, quantity: quantity }
end

received_shipping_items = shipping_items.map { |item| {sku: item[:product_id], quantity: item[:quantity].to_i} }

shipping_items_diff = received_shipping_items - shipment_lines
shipping_items_diff = received_shipping_items.reject do |item|
# using Array#delete deletes all of the instances of the item, we just want to delete the first
index_of_item = shipment_lines.index(item)
index_of_item && shipment_lines.delete_at(index_of_item)
end

return response("The received shipment items do not match with the shipment, diff: #{shipping_items_diff}", 500) unless shipping_items_diff.empty?

return response("Can't find variants with the following skus: #{missing_variants.join(', ')}", 500) unless missing_variants.empty?
return response("Can't find line_items with the following skus: #{missing_line_items.join(', ')} in the order.", 500) unless missing_line_items.empty?

return response("Can't find inventory_units with the following skus: #{missing_inventory_units.join(', ')} in the order.", 500) unless missing_inventory_units.empty?
end

# check if a state transition is required, and search for correct event to fire
Expand Down
20 changes: 18 additions & 2 deletions spec/lib/spree/wombat/handler/update_shipment_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ module Wombat
context "with mismatching items in shipment" do

before do
line_item.quantity = 2
line_item.save
original_item = message['shipment']['items'].first
message['shipment']['items'] << original_item
end

it "will return an error message with the mismatch diff" do
Expand All @@ -47,6 +47,22 @@ module Wombat
end
end

context "with multiple of the same items in a shipment" do
before do
new_inventory_unit = shipment.inventory_units.last.dup
new_inventory_unit.save!
original_item = message['shipment']['items'].first
message['shipment']['items'] << original_item
end

it "will return a proper message" do
responder = handler.process
expect(responder.summary).to eql "Updated shipment #{shipment.number}"
expect(responder.code).to eql 200
end

end

context "including a valid state transition" do
before do
message['shipment']['status'] = 'canceled'
Expand Down

0 comments on commit 918f215

Please sign in to comment.