Skip to content
This repository has been 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 50407db
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 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
10 changes: 8 additions & 2 deletions app/serializers/spree/wombat/shipment_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ 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|
line = LineItem.new do |line|
line.quantity = units.count
line.variant = line_item.variant
line.price = line_item.price
end

i << InventoryUnitSerializer.new(line, root: false)
end
i
end
Expand Down
24 changes: 24 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,30 @@ 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

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

0 comments on commit 50407db

Please sign in to comment.