Change line_item_syncer to verify stock if order is already complete, this will happen for orders in the current OC when a subscription is changed

This commit is contained in:
luisramos0
2019-07-12 22:21:20 +01:00
parent 0f3404ca27
commit 4f2bc33ec3
2 changed files with 29 additions and 7 deletions

View File

@@ -30,7 +30,7 @@ class LineItemSyncer
def create_new_items(order)
new_subscription_line_items.each do |sli|
order.line_items.create(variant_id: sli.variant_id, quantity: sli.quantity, skip_stock_check: true)
order.line_items.create(variant_id: sli.variant_id, quantity: sli.quantity, skip_stock_check: skip_stock_check?(order))
end
end
@@ -48,8 +48,12 @@ class LineItemSyncer
def update_quantity(line_item, sli)
if line_item.quantity == sli.quantity_was
return line_item.update_attributes(quantity: sli.quantity, skip_stock_check: true)
return line_item.update_attributes(quantity: sli.quantity, skip_stock_check: skip_stock_check?(line_item.order))
end
line_item.quantity == sli.quantity
end
def skip_stock_check?(order)
!order.complete?
end
end

View File

@@ -391,13 +391,31 @@ describe OrderSyncer do
let(:params) { { subscription_line_items_attributes: [{ id: sli.id, quantity: 3 }] } }
let(:syncer) { OrderSyncer.new(subscription) }
it "updates the line_item quantities and totals on all orders" do
before do
expect(order.reload.total.to_f).to eq 59.97
subscription.assign_attributes(params)
expect(syncer.sync!).to be true
line_items = Spree::LineItem.where(order_id: subscription.orders, variant_id: sli.variant_id)
expect(line_items.map(&:quantity)).to eq [3]
expect(order.reload.total.to_f).to eq 99.95
end
context "when order is not complete" do
it "updates the line_item quantities and totals on all orders" do
expect(syncer.sync!).to be true
line_items = Spree::LineItem.where(order_id: subscription.orders, variant_id: sli.variant_id)
expect(line_items.map(&:quantity)).to eq [3]
expect(order.reload.total.to_f).to eq 99.95
end
end
context "when order is complete" do
before { AdvanceOrderService.new(order).call }
it "does not update the line_item quantities and totals on all orders" do
expect(syncer.sync!).to be true
line_items = Spree::LineItem.where(order_id: subscription.orders, variant_id: sli.variant_id)
expect(line_items.map(&:quantity)).to eq [1]
expect(order.reload.total.to_f).to eq 59.97
end
end
end