Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Apply code review suggestion - refator cancel_fulfillment action

IKarbowiak4727505f8b58
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.py
index 9f259bb28c..f6611c0cce 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -320,4 +320,6 @@ class FulfillmentCancel(BaseMutation):
order = fulfillment.order
cancel_fulfillment(fulfillment, info.context.user, warehouse)
+ fulfillment.refresh_from_db(fields=["status"])
+ order.refresh_from_db(fields=["status"])
return FulfillmentCancel(fulfillment=fulfillment, order=order)
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 01fafa26a7..7b9f00d989 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -152,6 +152,7 @@ def fulfillment_tracking_updated(
get_plugins_manager().order_updated(fulfillment.order)
def cancel_fulfillment(
fulfillment: "Fulfillment", user: "User", warehouse: "Warehouse"
):
@@ -159,6 +160,8 @@ def cancel_fulfillment(
Return products to corresponding stocks.
"""
+ fulfillment = Fulfillment.objects.select_for_update().get(pk=fulfillment.pk)
+ restock_fulfillment_lines(fulfillment, warehouse)
events.fulfillment_canceled_event(
order=fulfillment.order, user=user, fulfillment=fulfillment
)
@@ -168,7 +171,6 @@ def cancel_fulfillment(
fulfillment=fulfillment,
warehouse_pk=warehouse.pk,
)
- restock_fulfillment_lines(fulfillment, warehouse)
fulfillment.status = FulfillmentStatus.CANCELED
fulfillment.save(update_fields=["status"])
update_order_status(fulfillment.order)
diff --git a/tests/api/test_fulfillment.py b/tests/api/test_fulfillment.py
index 41af2a7a50..7e467904f4 100644
--- a/tests/api/test_fulfillment.py
+++ b/tests/api/test_fulfillment.py
@@ -490,6 +490,9 @@ CANCEL_FULFILLMENT_MUTATION = """
fulfillment {
status
}
+ order {
+ status
+ }
}
}
"""
@@ -506,8 +509,9 @@ def test_cancel_fulfillment(
query, variables, permissions=[permission_manage_orders]
)
content = get_graphql_content(response)
- data = content["data"]["orderFulfillmentCancel"]["fulfillment"]
- assert data["status"] == FulfillmentStatus.CANCELED.upper()
+ data = content["data"]["orderFulfillmentCancel"]
+ assert data["fulfillment"]["status"] == FulfillmentStatus.CANCELED.upper()
+ assert data["order"]["status"] == OrderStatus.UNFULFILLED.upper()
event_cancelled, event_restocked_items = fulfillment.order.events.all()
assert event_cancelled.type == (OrderEvents.FULFILLMENT_CANCELED)
assert event_cancelled.parameters == {"composed_id": fulfillment.composed_id}
diff --git a/tests/test_order_actions.py b/tests/test_order_actions.py
index bf08441833..8030a03a9d 100644
--- a/tests/test_order_actions.py
+++ b/tests/test_order_actions.py
@@ -149,6 +149,8 @@ def test_cancel_fulfillment(fulfilled_order, warehouse):
cancel_fulfillment(fulfillment, None, warehouse)
+ fulfillment.refresh_from_db()
+ fulfilled_order.refresh_from_db()
assert fulfillment.status == FulfillmentStatus.CANCELED
assert fulfilled_order.status == OrderStatus.UNFULFILLED
assert line_1.order_line.quantity_fulfilled == 0