Skip to content

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

Fix fulfillment cancel (#18137) (#18139)

Iga Karbowiak0f16ed78b02b
diff --git a/saleor/graphql/order/mutations/fulfillment_cancel.py b/saleor/graphql/order/mutations/fulfillment_cancel.py
index a5fae6bcf8..c8794ba598 100644
--- a/saleor/graphql/order/mutations/fulfillment_cancel.py
+++ b/saleor/graphql/order/mutations/fulfillment_cancel.py
@@ -6,7 +6,7 @@ from django.core.exceptions import ValidationError
from ....account.models import User
from ....giftcard.utils import order_has_gift_card_lines
from ....order import FulfillmentStatus
-from ....order.actions import cancel_fulfillment, cancel_waiting_fulfillment
+from ....order.actions import cancel_fulfillment
from ....order.error_codes import OrderErrorCode
from ....permission.enums import OrderPermissions
from ...app.dataloaders import get_app_promise
@@ -112,12 +112,8 @@ class FulfillmentCancel(BaseMutation):
app = get_app_promise(info.context).get()
manager = get_plugin_manager_promise(info.context).get()
- if fulfillment.status == FulfillmentStatus.WAITING_FOR_APPROVAL:
- cancel_waiting_fulfillment(fulfillment, user, app, manager)
- fulfillment_response = None
- else:
- fulfillment = cancel_fulfillment(fulfillment, user, app, warehouse, manager)
- fulfillment_response = SyncWebhookControlContext(node=fulfillment)
+ fulfillment = cancel_fulfillment(fulfillment, user, app, warehouse, manager)
+ fulfillment_response = SyncWebhookControlContext(node=fulfillment)
order.refresh_from_db(fields=["status"])
return FulfillmentCancel(
fulfillment=fulfillment_response, order=SyncWebhookControlContext(order)
diff --git a/saleor/graphql/order/tests/mutations/test_fulfillment_cancel.py b/saleor/graphql/order/tests/mutations/test_fulfillment_cancel.py
index 966f7cfcba..de5f4053ca 100644
--- a/saleor/graphql/order/tests/mutations/test_fulfillment_cancel.py
+++ b/saleor/graphql/order/tests/mutations/test_fulfillment_cancel.py
@@ -187,22 +187,29 @@ def test_cancel_fulfillment_no_warehouse_id(
def test_cancel_fulfillment_awaiting_approval(
mock_restock_lines, staff_api_client, fulfillment, permission_group_manage_orders
):
+ # given
permission_group_manage_orders.user_set.add(staff_api_client.user)
fulfillment.status = FulfillmentStatus.WAITING_FOR_APPROVAL
fulfillment.save(update_fields=["status"])
+
query = CANCEL_FULFILLMENT_MUTATION
fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.id)
variables = {"id": fulfillment_id}
+
+ # when
response = staff_api_client.post_graphql(query, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["orderFulfillmentCancel"]
- assert data["fulfillment"] is None
- mock_restock_lines.assert_not_called()
+ assert data["fulfillment"]
+ assert data["fulfillment"]["status"] == FulfillmentStatus.CANCELED.upper()
+
event_cancelled = fulfillment.order.events.get()
- assert event_cancelled.type == (OrderEvents.FULFILLMENT_CANCELED)
- assert event_cancelled.parameters == {}
+ assert event_cancelled.type == OrderEvents.FULFILLMENT_CANCELED
+ assert event_cancelled.parameters == {"composed_id": fulfillment.composed_id}
assert event_cancelled.user == staff_api_client.user
- assert not Fulfillment.objects.filter(pk=fulfillment.pk).exists()
+ mock_restock_lines.assert_not_called()
@patch("saleor.order.actions.restock_fulfillment_lines")
@@ -213,6 +220,7 @@ def test_cancel_fulfillment_awaiting_approval_warehouse_specified(
permission_group_manage_orders,
warehouse,
):
+ # given
permission_group_manage_orders.user_set.add(staff_api_client.user)
fulfillment.status = FulfillmentStatus.WAITING_FOR_APPROVAL
fulfillment.save(update_fields=["status"])
@@ -220,16 +228,21 @@ def test_cancel_fulfillment_awaiting_approval_warehouse_specified(
fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.id)
warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.id)
variables = {"id": fulfillment_id, "warehouseId": warehouse_id}
+
+ # when
response = staff_api_client.post_graphql(query, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["orderFulfillmentCancel"]
- assert data["fulfillment"] is None
- mock_restock_lines.assert_not_called()
+ assert data["fulfillment"]
+ assert data["fulfillment"]["status"] == FulfillmentStatus.CANCELED.upper()
+
event_cancelled = fulfillment.order.events.get()
- assert event_cancelled.type == (OrderEvents.FULFILLMENT_CANCELED)
- assert event_cancelled.parameters == {}
+ assert event_cancelled.type == OrderEvents.FULFILLMENT_CANCELED
+ assert event_cancelled.parameters == {"composed_id": fulfillment.composed_id}
assert event_cancelled.user == staff_api_client.user
- assert not Fulfillment.objects.filter(pk=fulfillment.pk).exists()
+ mock_restock_lines.assert_not_called()
def test_cancel_fulfillment_canceled_state(
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 13cd327380..10314391df 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -800,6 +800,9 @@ def cancel_fulfillment(
fulfillment=fulfillment,
warehouse_pk=warehouse.pk,
)
+ else:
+ decrease_fulfilled_quantity(fulfillment)
+
fulfillment.status = FulfillmentStatus.CANCELED
fulfillment.save(update_fields=["status"])
update_order_status(fulfillment.order)
@@ -812,35 +815,14 @@ def cancel_fulfillment(
return fulfillment
-def cancel_waiting_fulfillment(
- fulfillment: Fulfillment,
- user: User,
- app: Optional["App"],
- manager: "PluginsManager",
-):
- """Cancel fulfillment which is in waiting for approval state."""
- fulfillment = Fulfillment.objects.get(pk=fulfillment.pk)
- # transaction ensures sending webhooks after order line is updated and events are
- # successfully created
- with traced_atomic_transaction():
- events.fulfillment_canceled_event(
- order=fulfillment.order, user=user, app=app, fulfillment=None
- )
-
- order_lines = []
- for line in fulfillment:
- order_line = line.order_line
- order_line.quantity_fulfilled -= line.quantity
- order_lines.append(order_line)
- OrderLine.objects.bulk_update(order_lines, ["quantity_fulfilled"])
-
- fulfillment.delete()
- call_event(manager.fulfillment_canceled, fulfillment)
- call_order_event(
- manager,
- WebhookEventAsyncType.ORDER_UPDATED,
- fulfillment.order,
- )
+def decrease_fulfilled_quantity(fulfillment: Fulfillment) -> None:
+ """Decrease the fulfilled quantity for order lines in the given fulfillment."""
+ order_lines = []
+ for line in fulfillment:
+ order_line = line.order_line
+ order_line.quantity_fulfilled -= line.quantity
+ order_lines.append(order_line)
+ OrderLine.objects.bulk_update(order_lines, ["quantity_fulfilled"])
def approve_fulfillment(
diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.py
index 9422fc223d..19d1e5c623 100644
--- a/saleor/order/tests/test_order_actions.py
+++ b/saleor/order/tests/test_order_actions.py
@@ -37,7 +37,6 @@ from ..actions import (
call_order_events,
cancel_fulfillment,
cancel_order,
- cancel_waiting_fulfillment,
clean_mark_order_as_paid,
fulfill_order_lines,
handle_fully_paid_order,
@@ -569,21 +568,33 @@ def test_cancel_fulfillment(fulfilled_order, warehouse):
assert line_2.order_line.quantity_fulfilled == 0
-def test_cancel_waiting_fulfillment_for_unconfirmed_order(fulfilled_order):
- for fulfillment in fulfilled_order.fulfillments.all():
- fulfillment.status = FulfillmentStatus.WAITING_FOR_APPROVAL
- fulfillment.save()
+def test_cancel_fulfillment_waiting_for_approval(fulfilled_order):
+ # given
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment.status = FulfillmentStatus.WAITING_FOR_APPROVAL
+ fulfillment.save(update_fields=["status"])
+
fulfilled_order.status = OrderStatus.UNCONFIRMED
- fulfilled_order.save()
+ fulfilled_order.save(update_fields=["status"])
+ warehouse = None
- cancel_waiting_fulfillment(
- fulfilled_order.fulfillments.first(),
+ # when
+ cancel_fulfillment(
+ fulfillment,
None,
None,
- get_plugins_manager(allow_replica=False),
+ warehouse,
+ manager=get_plugins_manager(allow_replica=False),
)
- assert fulfilled_order.status == OrderStatus.UNCONFIRMED
+ # then
+ fulfillment.refresh_from_db()
+ fulfilled_order.refresh_from_db()
+ line_1, line_2 = fulfillment.lines.all()
+ assert fulfillment.status == FulfillmentStatus.CANCELED
+ assert fulfilled_order.status == OrderStatus.UNFULFILLED
+ assert line_1.order_line.quantity_fulfilled == 0
+ assert line_2.order_line.quantity_fulfilled == 0
def test_cancel_fulfillment_variant_without_inventory_tracking(