Extract order-updating logic to Order::Updater

This commit is contained in:
Matt-Yorkley
2021-04-26 08:55:14 +01:00
parent 8e10f7af0e
commit 00c4a28d22
3 changed files with 27 additions and 15 deletions

View File

@@ -201,19 +201,7 @@ module Spree
end
def update_order
if completed? || void?
order.updater.update_payment_total
end
if order.completed?
order.updater.update_payment_state
order.updater.update_shipments
order.updater.update_shipment_state
end
if self.completed? || order.completed?
order.updater.persist_totals
end
OrderManagement::Order::Updater.new(order).after_payment_update(self)
end
# Necessary because some payment gateways will refuse payments with

View File

@@ -141,6 +141,22 @@ module OrderManagement
order.ship_address = order.address_from_distributor
end
def after_payment_update(payment)
if payment.completed? || payment.void?
update_payment_total
end
if order.completed?
update_payment_state
update_shipments
update_shipment_state
end
if payment.completed? || order.completed?
persist_totals
end
end
private
def round_money(value)

View File

@@ -556,11 +556,19 @@ describe Spree::Payment do
end
context "completed orders" do
let(:order_updater) { OrderManagement::Order::Updater.new(order) }
before { allow(order).to receive(:completed?) { true } }
it "updates payment_state and shipments" do
expect(order.updater).to receive(:update_payment_state)
expect(order.updater).to receive(:update_shipment_state)
expect(OrderManagement::Order::Updater).to receive(:new).with(order).
and_return(order_updater)
expect(order_updater).to receive(:after_payment_update).with(kind_of(Spree::Payment)).
and_call_original
expect(order_updater).to receive(:update_payment_state)
expect(order_updater).to receive(:update_shipment_state)
create(:payment, amount: 100, order: order)
end
end