diff --git a/app/services/line_item_syncer.rb b/app/services/line_item_syncer.rb index 8be0b46822..0db9743edf 100644 --- a/app/services/line_item_syncer.rb +++ b/app/services/line_item_syncer.rb @@ -30,7 +30,8 @@ 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: skip_stock_check?(order)) + new_line_item = order.line_items.create(variant_id: sli.variant_id, quantity: sli.quantity, skip_stock_check: skip_stock_check?(order)) + new_line_item.destroy if !skip_stock_check?(order) && new_line_item.insufficient_stock? end end diff --git a/spec/services/order_syncer_spec.rb b/spec/services/order_syncer_spec.rb index 661f15eb74..fdb8d62a99 100644 --- a/spec/services/order_syncer_spec.rb +++ b/spec/services/order_syncer_spec.rb @@ -458,16 +458,49 @@ describe OrderSyncer do let(:subscription) { create(:subscription, with_items: true, with_proxy_orders: true) } let(:order) { subscription.proxy_orders.first.initialise_order! } let(:variant) { create(:variant) } - let(:params) { { subscription_line_items_attributes: [{ id: nil, variant_id: variant.id, quantity: 1 }] } } let(:syncer) { OrderSyncer.new(subscription) } - it "adds the line item and updates the total 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: variant.id) - expect(line_items.map(&:quantity)).to eq [1] - expect(order.reload.total.to_f).to eq 79.96 + end + + context "when quantity is within available stock" do + let(:params) { { subscription_line_items_attributes: [{ id: nil, variant_id: variant.id, quantity: 1 }] } } + + it "adds the line item and updates the total on all orders" 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 [1] + expect(order.reload.total.to_f).to eq 79.96 + end + end + + context "when quantity is greater than available stock" do + let(:params) { { subscription_line_items_attributes: [{ id: nil, variant_id: variant.id, quantity: 7 }] } } + + context "when order is not complete" do + it "adds the line_item and updates totals on all orders" 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 [7] + expect(order.reload.total.to_f).to eq 199.9 + end + end + + context "when order is complete" do + before { AdvanceOrderService.new(order).call } + + it "does not add line_item and keeps totals on all orders" 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 [] + expect(order.reload.total.to_f).to eq 59.97 + end + end end end