mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-29 21:17:17 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user