Simplify update_shipment_state based on the fact there's only one shipment per order in OFN

This commit is contained in:
Luis Ramos
2020-07-10 17:45:49 +01:00
parent 2070cfd5bb
commit bdf9c1e405
2 changed files with 22 additions and 60 deletions

View File

@@ -65,36 +65,20 @@ module OrderManagement
# Updates the +shipment_state+ attribute according to the following logic:
#
# - shipped - when all Shipments are in the "shipped" state
# - partial - when 1. at least one Shipment has a state of "shipped"
# and there is another Shipment with a state other than "shipped"
# or 2. there are InventoryUnits associated with the order that
# have a state of "sold" but are not associated with a Shipment
# - ready - when all Shipments are in the "ready" state
# - shipped - when the order shipment is in the "shipped" state
# - ready - when the order shipment is in the "ready" state
# - backorder - when there is backordered inventory associated with an order
# - pending - when all Shipments are in the "pending" state
# - pending - when the shipment is in the "pending" state
#
# The +shipment_state+ value helps with reporting, etc. since it provides a quick and easy way
# to locate Orders needing attention.
def update_shipment_state
if order.backordered?
order.shipment_state = 'backorder'
else
# get all the shipment states for this order
shipment_states = shipments.states
if shipment_states.size > 1
# multiple shiment states means it's most likely partially shipped
order.shipment_state = 'partial'
else
# will return nil if no shipments are found
order.shipment_state = shipment_states.first
# TODO inventory unit states?
# if order.shipment_state && order.inventory_units.where(:shipment_id => nil).exists?
# shipments exist but there are unassigned inventory units
# order.shipment_state = 'partial'
# end
end
end
order.shipment_state = if order.shipment&.backordered?
'backorder'
else
# It returns nil if there is no shipment
order.shipment&.state
end
order.state_changed('shipment')
end

View File

@@ -28,22 +28,21 @@ module OrderManagement
end
context "updating shipment state" do
let(:shipment) { build(:shipment) }
before do
allow(order).to receive_message_chain(:shipments, :shipped, :count).and_return(0)
allow(order).to receive_message_chain(:shipments, :ready, :count).and_return(0)
allow(order).to receive_message_chain(:shipments, :pending, :count).and_return(0)
allow(order).to receive(:shipments).and_return([shipment])
end
it "is backordered" do
allow(order).to receive(:backordered?) { true }
allow(shipment).to receive(:backordered?) { true }
updater.update_shipment_state
expect(order.shipment_state).to eq 'backorder'
end
it "is nil" do
allow(order).to receive_message_chain(:shipments, :states).and_return([])
allow(order).to receive_message_chain(:shipments, :count).and_return(0)
allow(shipment).to receive(:state).and_return(nil)
updater.update_shipment_state
expect(order.shipment_state).to be_nil
@@ -51,18 +50,11 @@ module OrderManagement
["shipped", "ready", "pending"].each do |state|
it "is #{state}" do
allow(order).to receive_message_chain(:shipments, :states).and_return([state])
allow(shipment).to receive(:state).and_return(state)
updater.update_shipment_state
expect(order.shipment_state).to eq state.to_s
end
end
it "is partial" do
allow(order).
to receive_message_chain(:shipments, :states).and_return(["pending", "ready"])
updater.update_shipment_state
expect(order.shipment_state).to eq 'partial'
end
end
it "state change" do
@@ -93,14 +85,9 @@ module OrderManagement
updater.update
end
it "updates each shipment" do
it "updates the order shipment" do
shipment = build(:shipment)
shipments = [shipment]
allow(order).to receive_messages shipments: shipments
allow(shipments).to receive_messages states: []
allow(shipments).to receive_messages ready: []
allow(shipments).to receive_messages pending: []
allow(shipments).to receive_messages shipped: []
allow(order).to receive_messages shipments: [shipment]
expect(shipment).to receive(:update!).with(order)
updater.update
@@ -120,14 +107,9 @@ module OrderManagement
updater.update
end
it "doesnt update each shipment" do
it "doesnt update the order shipment" do
shipment = build(:shipment)
shipments = [shipment]
allow(order).to receive_messages shipments: shipments
allow(shipments).to receive_messages states: []
allow(shipments).to receive_messages ready: []
allow(shipments).to receive_messages pending: []
allow(shipments).to receive_messages shipped: []
allow(order).to receive_messages shipments: [shipment]
expect(shipment).not_to receive(:update!).with(order)
updater.update
@@ -139,13 +121,9 @@ module OrderManagement
updater.update
end
context "update adjustments" do
context "shipments" do
it "updates" do
expect(updater).to receive(:update_all_adjustments)
updater.update
end
end
it "updates all adjustments" do
expect(updater).to receive(:update_all_adjustments)
updater.update
end
it "is failed if no valid payments" do