StandingOrderForm: Storing problematic orders for reporting to user

This commit is contained in:
Rob Harrington
2016-12-16 10:38:57 +11:00
parent 9f50253537
commit e357a7b401
2 changed files with 31 additions and 3 deletions

View File

@@ -3,7 +3,7 @@ class StandingOrderForm
include ActiveModel::Conversion
include ActiveModel::Validations
attr_accessor :standing_order, :params, :fee_calculator
attr_accessor :standing_order, :params, :fee_calculator, :problematic_orders
delegate :orders, :order_cycles, :bill_address, :ship_address, :standing_line_items, to: :standing_order
delegate :shop, :shop_id, :customer, :customer_id, :begins_at, :ends_at, :proxy_orders, to: :standing_order
@@ -14,6 +14,7 @@ class StandingOrderForm
@standing_order = standing_order
@params = params
@fee_calculator = fee_calculator
@problematic_orders = []
end
def save
@@ -51,8 +52,12 @@ class StandingOrderForm
update_payment_for(order) if payment_method_id_changed?
changed_standing_line_items.each do |sli|
order.line_items.find_by_variant_id(sli.variant_id)
.update_attributes(quantity: sli.quantity, skip_stock_check: true)
line_item = order.line_items.find_by_variant_id(sli.variant_id)
if line_item.quantity == sli.quantity_was
line_item.update_attributes(quantity: sli.quantity, skip_stock_check: true)
else
@problematic_orders |= [order]
end
end
new_standing_line_items.each do |sli|
@@ -75,6 +80,8 @@ class StandingOrderForm
if payment
payment.andand.void_transaction!
order.payments.create(payment_method_id: payment_method_id, amount: order.reload.total)
else
@problematic_orders |= [order]
end
end
@@ -83,6 +90,8 @@ class StandingOrderForm
if shipment
shipment.update_attributes(shipping_method_id: shipping_method_id)
order.update_attribute(:shipping_method_id, shipping_method_id)
else
@problematic_orders |= [order]
end
end

View File

@@ -136,6 +136,7 @@ describe StandingOrderForm do
it "does not update the shipping_method on the standing order or on the pre-altered shipment" do
expect(order.reload.shipping_method).to eq changed_shipping_method
expect(order.reload.shipments.first.shipping_method).to eq changed_shipping_method
expect(form.problematic_orders).to include order
end
end
end
@@ -173,6 +174,7 @@ describe StandingOrderForm do
payments = order.reload.payments
expect(payments.count).to be 1
expect(payments.first.payment_method).to eq changed_payment_method
expect(form.problematic_orders).to include order
end
end
end
@@ -230,6 +232,23 @@ describe StandingOrderForm do
expect(order.reload.total.to_f).to eq 99.95
end
end
context "where the quantity of the item on an initialised order has already been changed" do
let(:params) { { standing_line_items_attributes: [ { id: sli.id, quantity: 3} ] } }
let(:form) { StandingOrderForm.new(standing_order, params) }
let(:changed_line_item) { order.line_items.find_by_variant_id(sli.variant_id) }
before { changed_line_item.update_attributes(quantity: 2) }
it "does not change the quantity, and adds the order to the problematic_orders list" do
expect(order.reload.total.to_f).to eq 79.96
expect(form.save).to be true
line_items = Spree::LineItem.where(order_id: standing_order.orders, variant_id: sli.variant_id)
expect(line_items.map(&:quantity)).to eq [2]
expect(order.reload.total.to_f).to eq 79.96
expect(form.problematic_orders).to include order
end
end
end
describe "adding a new line item" do