From 3b467dbae8a4e7dab1249701a27960c77cb63a5e Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 16 Jul 2019 12:08:08 +0100 Subject: [PATCH] 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 --- app/services/line_item_syncer.rb | 10 ++++++++-- spec/services/order_syncer_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/services/line_item_syncer.rb b/app/services/line_item_syncer.rb index 5c304557e7..7771ebf150 100644 --- a/app/services/line_item_syncer.rb +++ b/app/services/line_item_syncer.rb @@ -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 diff --git a/spec/services/order_syncer_spec.rb b/spec/services/order_syncer_spec.rb index 10c8020162..abb6ee58c5 100644 --- a/spec/services/order_syncer_spec.rb +++ b/spec/services/order_syncer_spec.rb @@ -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