Skip to content

Commit

Permalink
Update in-memory shipments of order in order_shipping
Browse files Browse the repository at this point in the history
Since we update the shipments state in the database,
but we read the shipsments from memory in the
OrderUpdater#determine_shipment_state we need to set the new
values here on the object as well.

Co-authored-by: darkswoop <benny@digitalbehr.de>
  • Loading branch information
tvdeyen and DarkSwoop committed Apr 13, 2022
1 parent 9fbcce5 commit 99c496d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/app/models/spree/order_shipping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ def ship(inventory_units:, stock_location:, address:, shipping_method:,
# TODO: make OrderShipping#ship_shipment call Shipment#ship! rather than
# having Shipment#ship! call OrderShipping#ship_shipment. We only really
# need this `update_columns` for the specs, until we make that change.
shipment.update_columns(state: 'shipped', shipped_at: Time.current)
attributes = { state: "shipped", shipped_at: Time.current }
shipment.update_columns(attributes)
# Make sure the in-memory shipments of the order have the correct state for OrderUpdate#determine_shipment_state
@order.shipments.detect { |s| shipment.id == s.id }&.assign_attributes(attributes)
end

send_shipment_emails(carton) if stock_location.fulfillable? && !suppress_mailer # e.g. digital gift cards that aren't actually shipped
Expand Down
15 changes: 15 additions & 0 deletions core/spec/models/spree/order_shipping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ def emails
end
end

context "when a second shipment is shipped" do
let(:order) { create(:order_ready_to_ship) }

it "sets the order to shipped state" do
order.shipping.ship_shipment(order.shipments.first)

unshipped_shipment = create(:shipment, order: order, state: "ready")

order.reload
order.recalculate

expect { order.shipping.ship_shipment(unshipped_shipment) }.to(change { order.shipment_state }.from("partial").to("shipped"))
end
end

context "when told to suppress the mailer" do
subject do
order.shipping.ship_shipment(
Expand Down

0 comments on commit 99c496d

Please sign in to comment.