Adapt line_item_syncer to the case where item is not added to the completed order because of insufficient stock and the subscription quantity is updated for that item afterwards

This commit is contained in:
luisramos0
2019-07-16 12:08:08 +01:00
parent 306390440a
commit 3b467dbae8
2 changed files with 28 additions and 2 deletions

View File

@@ -22,9 +22,15 @@ class LineItemSyncer
def update_item_quantities(order)
changed_subscription_line_items.each do |sli|
line_item = order.line_items.find_by_variant_id(sli.variant_id)
next if update_quantity(line_item, sli)
add_order_update_issue(order, line_item)
if line_item.blank?
order_update_issues.add(order, sli.variant.product_and_full_name)
next
end
unless update_quantity(line_item, sli)
add_order_update_issue(order, line_item)
end
end
end

View File

@@ -457,8 +457,10 @@ describe OrderSyncer do
it "does not change the quantity, and adds the order to order_update_issues" do
expect(order.reload.total.to_f).to eq 79.96
subscription.assign_attributes(params)
expect(syncer.sync!).to be true
expect(changed_line_item.reload.quantity).to eq 2
expect(order.reload.total.to_f).to eq 79.96
expect(syncer.order_update_issues[order.id]).to include "#{changed_line_item.product.name} - #{changed_line_item.variant.full_name}"
@@ -514,6 +516,24 @@ describe OrderSyncer do
expect(order.reload.total.to_f).to eq 59.97
expect(syncer.order_update_issues[order.id]).to include "#{variant.product.name} - #{variant.full_name} - Insufficient stock available"
end
context "and then updating the quantity of that subscription line item that was not added to the completed order" do
it "does nothing to the order and adds the order to order_update_issues" do
expect(syncer.sync!).to be true
line_items = Spree::LineItem.where(order_id: subscription.orders, variant_id: variant.id)
expect(line_items.map(&:quantity)).to eq []
subscription.save # this is necessary to get an id on the subscription_line_items
params = { subscription_line_items_attributes: [{ id: subscription.subscription_line_items.last.id, quantity: 2 }] }
subscription.assign_attributes(params)
expect(syncer.sync!).to be true
line_items = Spree::LineItem.where(order_id: subscription.orders, variant_id: variant.id)
expect(line_items.map(&:quantity)).to eq []
expect(syncer.order_update_issues[order.id]).to include "#{variant.product.name} - #{variant.full_name}"
end
end
end
end
end