Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update in-memory shipments of order in order_shipping #4334

Merged
merged 1 commit into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions core/app/models/spree/order_shipping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,15 @@ def ship(inventory_units:, stock_location:, address:, shipping_method:,
end

inventory_units.map(&:shipment).uniq.each do |shipment|
# Temporarily propagate the tracking number to the shipment as well
# TODO: Remove tracking numbers from shipments.
shipment.update!(tracking: tracking_number)

next unless shipment.inventory_units.reload.all? { |iu| iu.shipped? || iu.canceled? }
# 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)
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
if shipment.inventory_units.reload.all? { |iu| iu.shipped? || iu.canceled? }
shipment.update!(state: "shipped", shipped_at: Time.current, tracking: tracking_number)
else
shipment.update!(tracking: tracking_number)
end
end

send_shipment_emails(carton) if stock_location.fulfillable? && !suppress_mailer # e.g. digital gift cards that aren't actually shipped
@order.shipments.reload
@order.recalculate

carton
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