diff --git a/engines/order_management/app/services/order_management/order/updater.rb b/engines/order_management/app/services/order_management/order/updater.rb index d3c496d4e2..2e4f1c6c4b 100644 --- a/engines/order_management/app/services/order_management/order/updater.rb +++ b/engines/order_management/app/services/order_management/order/updater.rb @@ -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 diff --git a/engines/order_management/spec/services/order_management/order/updater_spec.rb b/engines/order_management/spec/services/order_management/order/updater_spec.rb index 009176eed2..1bd901d37c 100644 --- a/engines/order_management/spec/services/order_management/order/updater_spec.rb +++ b/engines/order_management/spec/services/order_management/order/updater_spec.rb @@ -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