From e6fbdb17e821ee4a02e69bbcfe008383649c6d93 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Wed, 14 Dec 2016 07:25:56 +1100 Subject: [PATCH] Only initialise a proxy order once --- app/models/proxy_order.rb | 1 + spec/models/proxy_order_spec.rb | 71 +++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/app/models/proxy_order.rb b/app/models/proxy_order.rb index 0b92419743..e1a7acd8a8 100644 --- a/app/models/proxy_order.rb +++ b/app/models/proxy_order.rb @@ -37,6 +37,7 @@ class ProxyOrder < ActiveRecord::Base end def initialise_order! + return order if order.present? create_order!({ customer_id: standing_order.customer_id, email: standing_order.customer.email, diff --git a/spec/models/proxy_order_spec.rb b/spec/models/proxy_order_spec.rb index daccc4ca3f..32cd8f78d6 100644 --- a/spec/models/proxy_order_spec.rb +++ b/spec/models/proxy_order_spec.rb @@ -139,39 +139,52 @@ describe ProxyOrder, type: :model do end end - describe "initialising an the order" do - let(:standing_order) { create(:standing_order, with_items: true) } - let!(:proxy_order) { create(:proxy_order, standing_order: standing_order) } + describe "initialise_order!" do + context "when the order has not already been initialised" do + let(:standing_order) { create(:standing_order, with_items: true) } + let!(:proxy_order) { create(:proxy_order, standing_order: standing_order) } - it "builds a new order based the standing order" do - expect{ proxy_order.initialise_order! }.to change{Spree::Order.count}.by(1) - expect(proxy_order.reload.order).to be_a Spree::Order - order = proxy_order.order - expect(order.line_items.count).to eq standing_order.standing_line_items.count - expect(order.distributor).to eq standing_order.shop - expect(order.order_cycle).to eq proxy_order.order_cycle - expect(order.shipping_method).to eq standing_order.shipping_method - expect(order.shipments.first.shipping_method).to eq standing_order.shipping_method - expect(order.payments.first.payment_method).to eq standing_order.payment_method - expect(order.bill_address).to eq standing_order.bill_address - expect(order.ship_address).to eq standing_order.ship_address - expect(order.complete?).to be false - end - - context "when a requested quantity is greater than available stock" do - let(:sli) { standing_order.standing_line_items.first } - let(:variant) { sli.variant } - - before do - variant.update_attribute(:count_on_hand, 2) - sli.update_attribute(:quantity, 5) - end - - it "initialises the order with the requested quantity regardless" do + it "builds a new order based the standing order" do expect{ proxy_order.initialise_order! }.to change{Spree::Order.count}.by(1) expect(proxy_order.reload.order).to be_a Spree::Order order = proxy_order.order - expect(order.line_items.find_by_variant_id(variant.id).quantity).to eq 5 + expect(order.line_items.count).to eq standing_order.standing_line_items.count + expect(order.distributor).to eq standing_order.shop + expect(order.order_cycle).to eq proxy_order.order_cycle + expect(order.shipping_method).to eq standing_order.shipping_method + expect(order.shipments.first.shipping_method).to eq standing_order.shipping_method + expect(order.payments.first.payment_method).to eq standing_order.payment_method + expect(order.bill_address).to eq standing_order.bill_address + expect(order.ship_address).to eq standing_order.ship_address + expect(order.complete?).to be false + end + + context "when a requested quantity is greater than available stock" do + let(:sli) { standing_order.standing_line_items.first } + let(:variant) { sli.variant } + + before do + variant.update_attribute(:count_on_hand, 2) + sli.update_attribute(:quantity, 5) + end + + it "initialises the order with the requested quantity regardless" do + expect{ proxy_order.initialise_order! }.to change{Spree::Order.count}.by(1) + expect(proxy_order.reload.order).to be_a Spree::Order + order = proxy_order.order + expect(order.line_items.find_by_variant_id(variant.id).quantity).to eq 5 + end + end + end + + context "when the order has already been initialised" do + let(:existing_order) { create(:order) } + let!(:proxy_order) { create(:proxy_order, order: existing_order) } + + it "returns the existing order" do + expect do + expect(proxy_order.initialise_order!).to eq existing_order + end.to_not change{Spree::Order.count} end end end