Skip to content

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

Add draft order validation in DraftOrderComplete mutation (#9118)

Iga Karbowiak7b5c320e674d
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.py
index 8edf32fa6e..161b126482 100644
--- a/saleor/graphql/order/mutations/draft_orders.py
+++ b/saleor/graphql/order/mutations/draft_orders.py
@@ -433,6 +433,17 @@ class DraftOrderComplete(BaseMutation):
except User.DoesNotExist:
order.user = None
+ @classmethod
+ def validate_order(cls, order):
+ if not order.is_draft():
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ "The order is not draft.", code=OrderErrorCode.INVALID.value
+ )
+ }
+ )
+
@classmethod
def perform_mutation(cls, _root, info, id):
manager = info.context.plugins
@@ -442,6 +453,8 @@ class DraftOrderComplete(BaseMutation):
only_type=Order,
qs=models.Order.objects.prefetch_related("lines__variant"),
)
+ cls.validate_order(order)
+
country = get_order_country(order)
validate_draft_order(order, country)
cls.update_user_fields(order)
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 9c7c3cf81b..0b463fadce 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -3861,6 +3861,22 @@ def test_draft_order_complete_insufficient_stock_preorders(
assert error["code"] == OrderErrorCode.INSUFFICIENT_STOCK.name
+def test_draft_order_complete_not_draft_order(
+ staff_api_client,
+ permission_manage_orders,
+ order_with_lines,
+):
+ order_id = graphene.Node.to_global_id("Order", order_with_lines.id)
+ variables = {"id": order_id}
+ response = staff_api_client.post_graphql(
+ DRAFT_ORDER_COMPLETE_MUTATION, variables, permissions=[permission_manage_orders]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["draftOrderComplete"]
+ assert data["errors"][0]["code"] == OrderErrorCode.INVALID.name
+ assert data["errors"][0]["field"] == "id"
+
+
ORDER_LINES_CREATE_MUTATION = """
mutation OrderLinesCreate($orderId: ID!, $variantId: ID!, $quantity: Int!) {
orderLinesCreate(id: $orderId,