Rename StandingOrderUpdater to OrderSyncer

This commit is contained in:
Rob Harrington
2018-01-31 16:22:32 +11:00
parent bbc3cad67d
commit 3e68973812
3 changed files with 50 additions and 50 deletions

View File

@@ -1,7 +1,7 @@
# Responsible for ensuring that any updates to a Standing Order are propagated to any
# orders belonging to that Standing Order which have been instantiated
class StandingOrderUpdater
class OrderSyncer
attr_reader :order_update_issues
def initialize(standing_order)
@@ -10,7 +10,7 @@ class StandingOrderUpdater
@line_item_syncer = LineItemSyncer.new(standing_order, order_update_issues)
end
def update!
def sync!
future_and_undated_orders.all? do |order|
order.assign_attributes(customer_id: customer_id, email: customer.andand.email, distributor_id: shop_id)
update_associations_for(order)

View File

@@ -1,17 +1,17 @@
require 'open_food_network/proxy_order_syncer'
class StandingOrderForm
attr_accessor :standing_order, :params, :fee_calculator, :order_update_issues, :validator, :order_updater
attr_accessor :standing_order, :params, :fee_calculator, :order_update_issues, :validator, :order_syncer
delegate :json_errors, :valid?, to: :validator
delegate :order_update_issues, to: :order_updater
delegate :order_update_issues, to: :order_syncer
def initialize(standing_order, params = {}, fee_calculator = nil)
@standing_order = standing_order
@params = params
@fee_calculator = fee_calculator
@validator = StandingOrderValidator.new(standing_order)
@order_updater = StandingOrderUpdater.new(standing_order)
@order_syncer = OrderSyncer.new(standing_order)
end
def save
@@ -20,7 +20,7 @@ class StandingOrderForm
return false unless valid?
standing_order.transaction do
proxy_order_syncer.sync!
order_updater.update!
order_syncer.sync!
standing_order.save!
end
end

View File

@@ -1,10 +1,10 @@
describe StandingOrderUpdater do
describe OrderSyncer do
describe "updating the shipping method" do
let(:standing_order) { create(:standing_order, with_items: true, with_proxy_orders: true) }
let(:order) { standing_order.proxy_orders.first.initialise_order! }
let(:shipping_method) { standing_order.shipping_method }
let(:new_shipping_method) { create(:shipping_method, distributors: [standing_order.shop]) }
let(:updater) { StandingOrderUpdater.new(standing_order) }
let(:syncer) { OrderSyncer.new(standing_order) }
context "when the shipping method on an order is the same as the standing order" do
let(:params) { { shipping_method_id: new_shipping_method.id } }
@@ -12,7 +12,7 @@ describe StandingOrderUpdater do
it "updates the shipping_method on the order and on shipments" do
expect(order.shipments.first.shipping_method_id_was).to eq shipping_method.id
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).to be true
expect(order.reload.shipping_method).to eq new_shipping_method
expect(order.shipments.first.shipping_method).to eq new_shipping_method
end
@@ -29,13 +29,13 @@ describe StandingOrderUpdater do
order.shipments.first.update_attributes(shipping_method_id: new_shipping_method.id)
order.update_attributes(shipping_method_id: new_shipping_method.id)
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).to be true
end
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 new_shipping_method
expect(order.reload.shipments.first.shipping_method).to eq new_shipping_method
expect(updater.order_update_issues[order.id]).to be nil
expect(syncer.order_update_issues[order.id]).to be nil
end
end
@@ -49,13 +49,13 @@ describe StandingOrderUpdater do
order.shipments.first.update_attributes(shipping_method_id: changed_shipping_method.id)
order.update_attributes(shipping_method_id: changed_shipping_method.id)
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).to be true
end
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(updater.order_update_issues[order.id]).to include "Shipping Method"
expect(syncer.order_update_issues[order.id]).to include "Shipping Method"
end
end
end
@@ -68,7 +68,7 @@ describe StandingOrderUpdater do
let(:new_payment_method) { create(:payment_method, distributors: [standing_order.shop]) }
let(:invalid_payment_method) { create(:payment_method, distributors: [create(:enterprise)]) }
let(:bogus_payment_method) { create(:bogus_payment_method, distributors: [standing_order.shop]) }
let(:updater) { StandingOrderUpdater.new(standing_order) }
let(:syncer) { OrderSyncer.new(standing_order) }
context "when the payment method on an order is the same as the standing order" do
let(:params) { { payment_method_id: new_payment_method.id } }
@@ -76,7 +76,7 @@ describe StandingOrderUpdater do
it "voids existing payments and creates a new payment with the relevant payment method" do
expect(order.payments.reload.first.payment_method).to eq payment_method
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).to be true
payments = order.reload.payments
expect(payments.count).to be 2
expect(payments.with_state('void').count).to be 1
@@ -93,14 +93,14 @@ describe StandingOrderUpdater do
before do
order.payments.first.update_attribute(:payment_method_id, new_payment_method.id)
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).to be true
end
it "keeps pre-altered payments and doesn't add an issue to order_update_issues" do
payments = order.reload.payments
expect(payments.count).to be 1
expect(payments.first.payment_method).to eq new_payment_method
expect(updater.order_update_issues[order.id]).to be nil
expect(syncer.order_update_issues[order.id]).to be nil
end
end
@@ -110,14 +110,14 @@ describe StandingOrderUpdater do
before do
order.payments.first.update_attribute(:payment_method_id, changed_payment_method.id)
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).to be true
end
it "keeps pre-altered payments and adds an issue to order_update_issues" do
payments = order.reload.payments
expect(payments.count).to be 1
expect(payments.first.payment_method).to eq changed_payment_method
expect(updater.order_update_issues[order.id]).to include "Payment Method"
expect(syncer.order_update_issues[order.id]).to include "Payment Method"
end
end
end
@@ -130,7 +130,7 @@ describe StandingOrderUpdater do
let!(:bill_address_attrs) { standing_order.bill_address.attributes }
let!(:ship_address_attrs) { standing_order.ship_address.attributes }
let(:params) { { bill_address_attributes: { id: bill_address_attrs["id"], firstname: "Bill", address1: "123 abc st", phone: "1123581321" } } }
let(:updater) { StandingOrderUpdater.new(standing_order) }
let(:syncer) { OrderSyncer.new(standing_order) }
context "when a ship address is not required" do
before { shipping_method.update_attributes(require_ship_address: false) }
@@ -138,8 +138,8 @@ describe StandingOrderUpdater do
context "when the bill_address on the order matches that on the standing order" do
it "updates all bill_address attrs and ship_address names + phone" do
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(updater.order_update_issues.keys).to_not include order.id
expect(syncer.sync!).to be true
expect(syncer.order_update_issues.keys).to_not include order.id
order.reload;
expect(order.bill_address.firstname).to eq "Bill"
expect(order.bill_address.lastname).to eq bill_address_attrs["lastname"]
@@ -156,8 +156,8 @@ describe StandingOrderUpdater do
before { order.bill_address.update_attributes(firstname: "Jane") }
it "does not update bill_address or ship_address on the order" do
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(updater.order_update_issues.keys).to include order.id
expect(syncer.sync!).to be true
expect(syncer.order_update_issues.keys).to include order.id
order.reload;
expect(order.bill_address.firstname).to eq "Jane"
expect(order.bill_address.lastname).to eq bill_address_attrs["lastname"]
@@ -177,8 +177,8 @@ describe StandingOrderUpdater do
context "when the bill_address on the order matches that on the standing order" do
it "only updates bill_address attrs" do
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(updater.order_update_issues.keys).to_not include order.id
expect(syncer.sync!).to be true
expect(syncer.order_update_issues.keys).to_not include order.id
order.reload;
expect(order.bill_address.firstname).to eq "Bill"
expect(order.bill_address.lastname).to eq bill_address_attrs["lastname"]
@@ -196,8 +196,8 @@ describe StandingOrderUpdater do
it "does not update bill_address or ship_address on the order" do
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(updater.order_update_issues.keys).to include order.id
expect(syncer.sync!).to be true
expect(syncer.order_update_issues.keys).to include order.id
order.reload;
expect(order.bill_address.firstname).to eq "Jane"
expect(order.bill_address.lastname).to eq bill_address_attrs["lastname"]
@@ -219,15 +219,15 @@ describe StandingOrderUpdater do
let!(:bill_address_attrs) { standing_order.bill_address.attributes }
let!(:ship_address_attrs) { standing_order.ship_address.attributes }
let(:params) { { ship_address_attributes: { id: ship_address_attrs["id"], firstname: "Ship", address1: "123 abc st", phone: "1123581321" } } }
let(:updater) { StandingOrderUpdater.new(standing_order) }
let(:syncer) { OrderSyncer.new(standing_order) }
context "when a ship address is not required" do
before { shipping_method.update_attributes(require_ship_address: false) }
it "does not change the ship address" do
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(updater.order_update_issues.keys).to_not include order.id
expect(syncer.sync!).to be true
expect(syncer.order_update_issues.keys).to_not include order.id
order.reload;
expect(order.ship_address.firstname).to eq ship_address_attrs["firstname"]
expect(order.ship_address.lastname).to eq ship_address_attrs["lastname"]
@@ -241,8 +241,8 @@ describe StandingOrderUpdater do
it "updates ship_address attrs" do
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(updater.order_update_issues.keys).to_not include order.id
expect(syncer.sync!).to be true
expect(syncer.order_update_issues.keys).to_not include order.id
order.reload;
expect(order.ship_address.firstname).to eq "Ship"
expect(order.ship_address.lastname).to eq ship_address_attrs["lastname"]
@@ -258,8 +258,8 @@ describe StandingOrderUpdater do
context "when the ship address on the order matches that on the standing order" do
it "updates ship_address attrs" do
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(updater.order_update_issues.keys).to_not include order.id
expect(syncer.sync!).to be true
expect(syncer.order_update_issues.keys).to_not include order.id
order.reload;
expect(order.ship_address.firstname).to eq "Ship"
expect(order.ship_address.lastname).to eq ship_address_attrs["lastname"]
@@ -272,8 +272,8 @@ describe StandingOrderUpdater do
before { order.ship_address.update_attributes(firstname: "Jane") }
it "does not update ship_address on the order" do
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(updater.order_update_issues.keys).to include order.id
expect(syncer.sync!).to be true
expect(syncer.order_update_issues.keys).to include order.id
order.reload;
expect(order.ship_address.firstname).to eq "Jane"
expect(order.ship_address.lastname).to eq ship_address_attrs["lastname"]
@@ -294,12 +294,12 @@ describe StandingOrderUpdater do
context "when quantity is within available stock" do
let(:params) { { standing_line_items_attributes: [{ id: sli.id, quantity: 2}] } }
let(:updater) { StandingOrderUpdater.new(standing_order) }
let(:syncer) { OrderSyncer.new(standing_order) }
it "updates the line_item quantities and totals on all orders" do
expect(order.reload.total.to_f).to eq 59.97
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).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
@@ -308,12 +308,12 @@ describe StandingOrderUpdater do
context "when quantity is greater than available stock" do
let(:params) { { standing_line_items_attributes: [{ id: sli.id, quantity: 3}] } }
let(:updater) { StandingOrderUpdater.new(standing_order) }
let(:syncer) { OrderSyncer.new(standing_order) }
it "updates the line_item quantities and totals on all orders" do
expect(order.reload.total.to_f).to eq 59.97
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).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 [3]
expect(order.reload.total.to_f).to eq 99.95
@@ -322,7 +322,7 @@ describe StandingOrderUpdater do
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(:updater) { StandingOrderUpdater.new(standing_order) }
let(:syncer) { OrderSyncer.new(standing_order) }
let(:changed_line_item) { order.line_items.find_by_variant_id(sli.variant_id) }
before { variant.update_attribute(:count_on_hand, 3) }
@@ -333,10 +333,10 @@ describe StandingOrderUpdater do
it "does not change the quantity, and doesn't add the order to order_update_issues" do
expect(order.reload.total.to_f).to eq 99.95
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).to be true
expect(changed_line_item.reload.quantity).to eq 3
expect(order.reload.total.to_f).to eq 99.95
expect(updater.order_update_issues[order.id]).to be nil
expect(syncer.order_update_issues[order.id]).to be nil
end
end
@@ -346,10 +346,10 @@ describe StandingOrderUpdater do
it "does not change the quantity, and adds the order to order_update_issues" do
expect(order.reload.total.to_f).to eq 79.96
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).to be true
expect(changed_line_item.reload.quantity).to eq 2
expect(order.reload.total.to_f).to eq 79.96
expect(updater.order_update_issues[order.id]).to include "#{changed_line_item.product.name} - #{changed_line_item.full_name}"
expect(syncer.order_update_issues[order.id]).to include "#{changed_line_item.product.name} - #{changed_line_item.full_name}"
end
end
end
@@ -360,12 +360,12 @@ describe StandingOrderUpdater do
let(:order) { standing_order.proxy_orders.first.initialise_order! }
let(:variant) { create(:variant) }
let(:params) { { standing_line_items_attributes: [{ id: nil, variant_id: variant.id, quantity: 1}] } }
let(:updater) { StandingOrderUpdater.new(standing_order) }
let(:syncer) { OrderSyncer.new(standing_order) }
it "adds the line item and updates the total on all orders" do
expect(order.reload.total.to_f).to eq 59.97
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).to be true
line_items = Spree::LineItem.where(order_id: standing_order.orders, variant_id: variant.id)
expect(line_items.map(&:quantity)).to eq [1]
expect(order.reload.total.to_f).to eq 79.96
@@ -378,12 +378,12 @@ describe StandingOrderUpdater do
let(:sli) { standing_order.standing_line_items.first }
let(:variant) { sli.variant }
let(:params) { { standing_line_items_attributes: [{ id: sli.id, _destroy: true }] } }
let(:updater) { StandingOrderUpdater.new(standing_order) }
let(:syncer) { OrderSyncer.new(standing_order) }
it "removes the line item and updates totals on all orders" do
expect(order.reload.total.to_f).to eq 59.97
standing_order.assign_attributes(params)
expect(updater.update!).to be true
expect(syncer.sync!).to be true
line_items = Spree::LineItem.where(order_id: standing_order.orders, variant_id: variant.id)
expect(line_items.count).to be 0
expect(order.reload.total.to_f).to eq 39.98