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

Commit

Permalink
Merge quantity of units when serializing shipment.items
Browse files Browse the repository at this point in the history
Currently when receiving that shipment back via a webhook from wombat
the extention would complain about a diff in the items.

e.g. The received shipment items do not match with the shipment, diff: [{:sku=>"123", :quantity=>1}, {:sku=>"123", :quantity=>1}]

Before:

        items: [{ product_id: 1, quantity: 1 }, { product_id: 1, quantity: 1 }]

Now:

        items: [{ product_id: 1, quantity: 2 }]
  • Loading branch information
huoxito committed Oct 8, 2014
1 parent 465bbee commit d41dbef
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/serializers/spree/wombat/inventory_unit_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def quantity
end

def price
object.line_item.price.round(2).to_f
price = object.respond_to?(:price) ? object.price : object.line_item.price
price.round(2).to_f
end

def product_id
Expand Down
21 changes: 18 additions & 3 deletions app/serializers/spree/wombat/shipment_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,29 @@ def updated_at

def items
i = []
object.inventory_units.each do |inventory_unit|
i << InventoryUnitSerializer.new(inventory_unit, root: false)
object.inventory_units.group_by(&:line_item).each do |line_item, units|
if units.map(&:variant).uniq.count > 1

units.group_by(&:variant).each do |variant, units|
line = build_custom_line line_item, units.count, variant
i << InventoryUnitSerializer.new(line, root: false)
end
else
line = build_custom_line line_item, units.count, units.first.variant
i << InventoryUnitSerializer.new(line, root: false)
end
end
i
end

private
def build_custom_line(line_item, quantity, variant)
LineItem.new do |line|
line.quantity = quantity
line.variant = variant
line.price = line_item.price
end
end

def adjustment_total
object.order.adjustment_total.to_f
Expand All @@ -90,7 +106,6 @@ def shipping_total
def tax_total
object.order.tax_total.to_f
end

end
end
end
54 changes: 54 additions & 0 deletions spec/serializers/spree/wombat/shipment_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,60 @@ module Wombat
let(:shipment) { create(:shipment, address: create(:address), order: create(:order_with_line_items)) }
let(:serialized_shipment) { JSON.parse (ShipmentSerializer.new(shipment, root: false).to_json) }

it "merge inventory into a single shipment item when needed" do
variant = Variant.new sku: "wom"
line = LineItem.new variant: variant
inventories = 2.times.map {
InventoryUnit.new line_item: line, variant: variant
}

shipment = Shipment.new
shipment.inventory_units = inventories

serialized = ShipmentSerializer.new(shipment, root: false)
expect(serialized.items.first.quantity).to eq 2

other_variant = Variant.new sku: "other wom"
other_line = LineItem.new variant: other_variant
inventories.push InventoryUnit.new(line_item: other_line, variant: other_variant)

shipment = Shipment.new
shipment.inventory_units = inventories

serialized = ShipmentSerializer.new(shipment, root: false)
expect(serialized.items.last.quantity).to eq 1
end

context "inventory unit's variant doesnt match line_item variant" do
it "still merge inventory quantity properly" do
variant = Variant.new sku: "wom"
line = LineItem.new variant: variant, price: 33

other_variant = Variant.new sku: "other wom"
other_line = LineItem.new variant: other_variant, price: 11

inventories = [
InventoryUnit.new(line_item: line, variant: variant),
InventoryUnit.new(line_item: other_line, variant: variant),
InventoryUnit.new(line_item: line, variant: other_variant)
]

shipment = Shipment.new
shipment.inventory_units = inventories

serialized = ShipmentSerializer.new(shipment, root: false)
expect(serialized.items.count).to eq 3

expect(serialized.items.map(&:quantity)).to match_array [1, 1, 1]

expected = [variant.sku, variant.sku, other_variant.sku]
expect(serialized.items.map(&:product_id)).to match_array expected

expected = [line.price.to_f, other_line.price.to_f, line.price.to_f]
expect(serialized.items.map(&:price)).to match_array expected
end
end

it "serializes the number as id" do
expect(serialized_shipment["id"]).to eql shipment.number
end
Expand Down

0 comments on commit d41dbef

Please sign in to comment.