Make line_item_syncer delete new line items if stock is insufficient

This commit is contained in:
luisramos0
2019-07-13 22:57:13 +01:00
parent 4f2bc33ec3
commit 6aed9ba549
2 changed files with 41 additions and 7 deletions

View File

@@ -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

View File

@@ -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