Skip to content

Commit

Permalink
Call empty only on incomplete orders
Browse files Browse the repository at this point in the history
Code fails with a 500 on admin because of the order shipping status.
The change triggers @order.empty! only when the order is incomplete.

Co-authored-by: An Stewart <[email protected]>
  • Loading branch information
2 people authored and nvandoorn committed Sep 12, 2024
1 parent a791a5f commit e96f358
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
9 changes: 7 additions & 2 deletions api/app/controllers/spree/api/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ def create

def empty
authorize! :update, @order, order_token
@order.empty!
respond_with(@order, default_template: :show)

if @order.complete?
invalid_resource!(@order)
else
@order.empty!
respond_with(@order, default_template: :show)
end
end

def index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Spree.Views.Cart.EmptyCartButton = Backbone.View.extend({

render: function() {
var isNew = function (item) { return item.isNew() };
this.$el.prop("disabled", !this.collection.length || this.collection.some(isNew));
this.$el.prop("disabled", !this.collection.length || this.collection.some(isNew) || this.model.get("completed_at"));
}
});
57 changes: 26 additions & 31 deletions backend/spec/features/admin/orders/order_details_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,39 +137,11 @@
expect(page).to have_field('quantity')
end

it "can remove all items with empty cart" do
it "cannot remove all items with a completed order" do
# Wait for the cart contents to be loaded.
expect(page).to have_content("spree t-shirt")

within("#item_total") do
expect(page).to have_content("$40.00")
end

within("#order_total") do
expect(page).to have_content("$40.00")
end

within("#order-total", text: 'Order Total') do
expect(page).to have_content("$40.00")
end

accept_confirm "Are you sure you want to delete this record?" do
click_on 'Empty Cart'
end

expect(page).not_to have_content("spree t-shirt")

# Should have a new item row
expect(page).to have_field('quantity')

within("#item_total") do
expect(page).to have_content("$0.00")
end

within("#order_total") do
expect(page).to have_content("$0.00")
end

expect(page).to have_css('#order-total', visible: false)
expect(page).to have_button("Empty Cart", disabled: true)
end

# Regression test for https://github.com/spree/spree/issues/3862
Expand Down Expand Up @@ -384,6 +356,29 @@
expect(page).not_to have_selector('.fa-arrows-h')
expect(page).not_to have_selector('.fa-trash')
end

context 'order has shipped' do
it 'disables empty cart' do
order = create(:order_ready_to_ship)

visit spree.cart_admin_order_path(order)
order.fulfill!

## simulate shipping order in another tab
shipment = order.shipments.first
order.shipping.ship(
inventory_units: shipment.inventory_units,
stock_location: shipment.stock_location,
address: order.ship_address,
shipping_method: shipment.shipping_method
)

# Make an assertion here about page content to give the JS time
# to load before the next expectation.
expect(page).to have_content("Cart")
expect(page).to have_button("Empty Cart", disabled: true)
end
end
end
end

Expand Down

0 comments on commit e96f358

Please sign in to comment.