Skip to content

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

Errors handling fix for orderFulfillApprove mutation (#9491)

SzymJc6220233ccb1
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.py
index 50cbb12598..b42fd404ea 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -466,15 +466,21 @@ class FulfillmentApprove(BaseMutation):
cls.clean_input(info, fulfillment)
order = fulfillment.order
- fulfillment = approve_fulfillment(
- fulfillment,
- info.context.user,
- info.context.app,
- info.context.plugins,
- info.context.site.settings,
- notify_customer=data["notify_customer"],
- allow_stock_to_be_exceeded=data.get("allow_stock_to_be_exceeded"),
- )
+
+ try:
+ fulfillment = approve_fulfillment(
+ fulfillment,
+ info.context.user,
+ info.context.app,
+ info.context.plugins,
+ info.context.site.settings,
+ notify_customer=data["notify_customer"],
+ allow_stock_to_be_exceeded=data.get("allow_stock_to_be_exceeded"),
+ )
+ except InsufficientStock as exc:
+ errors = prepare_insufficient_stock_order_validation_errors(exc)
+ raise ValidationError({"stocks": errors})
+
order.refresh_from_db(fields=["status"])
return FulfillmentApprove(fulfillment=fulfillment, order=order)
diff --git a/saleor/graphql/order/tests/test_fulfillment.py b/saleor/graphql/order/tests/test_fulfillment.py
index 8e2aba32e1..ff1f1b2914 100644
--- a/saleor/graphql/order/tests/test_fulfillment.py
+++ b/saleor/graphql/order/tests/test_fulfillment.py
@@ -188,10 +188,10 @@ def test_order_fulfill_with_stock_exceeded_with_flag_disabled(
errors = data["errors"]
assert errors[0]["code"] == "INSUFFICIENT_STOCK"
- assert errors[0]["message"] == "Insufficient product stock: SKU_AA"
+ assert errors[0]["message"] == f"Insufficient product stock: {order_line}"
assert errors[1]["code"] == "INSUFFICIENT_STOCK"
- assert errors[1]["message"] == "Insufficient product stock: SKU_B"
+ assert errors[1]["message"] == f"Insufficient product stock: {order_line2}"
def test_order_fulfill_with_stock_exceeded_with_flag_enabled(
@@ -1714,6 +1714,7 @@ APPROVE_FULFILLMENT_MUTATION = """
errors {
field
code
+ message
}
}
}
@@ -1811,13 +1812,26 @@ def test_fulfillment_approve_delete_products_before_approval_allow_stock_exceede
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_orders]
)
- content = get_graphql_content(response, ignore_errors=True)
- errors = content["errors"]
- assert len(errors) == 1
- assert (
- errors[0]["message"]
- == "Insufficient stock for Test product (SKU_AA), Test product 2 (SKU_B)"
- )
+
+ content = get_graphql_content(response)
+ errors = content["data"]["orderFulfillmentApprove"]["errors"]
+
+ assert len(errors) == 2
+
+ error_field_and_code = {
+ "field": "stocks",
+ "code": "INSUFFICIENT_STOCK",
+ }
+ expected_errors = [
+ {
+ **error_field_and_code,
+ "message": f"Insufficient product stock: {line.order_line}",
+ }
+ for line in fulfillment.lines.all()
+ ]
+
+ for expected_error in expected_errors:
+ assert expected_error in errors
fulfillment.refresh_from_db()
assert fulfillment.status == FulfillmentStatus.WAITING_FOR_APPROVAL
@@ -1965,8 +1979,25 @@ def test_fulfillment_approve_when_stock_is_exceeded_and_flag_disabled(
query, variables, permissions=[permission_manage_orders]
)
content = get_graphql_content(response, ignore_errors=True)
- assert content["errors"]
- assert content["errors"][0]["message"] == "Insufficient stock for SKU_AA, SKU_B"
+ errors = content["data"]["orderFulfillmentApprove"]["errors"]
+
+ assert len(errors) == 2
+
+ error_field_and_code = {
+ "field": "stocks",
+ "code": "INSUFFICIENT_STOCK",
+ }
+
+ expected_errors = [
+ {
+ **error_field_and_code,
+ "message": f"Insufficient product stock: {line.order_line}",
+ }
+ for line in fulfillment.lines.all()
+ ]
+
+ for expected_error in expected_errors:
+ assert expected_error in errors
@patch("saleor.order.actions.send_fulfillment_confirmation_to_customer", autospec=True)
diff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.py
index 632b757ab3..58cfc76c00 100644
--- a/saleor/graphql/order/utils.py
+++ b/saleor/graphql/order/utils.py
@@ -289,7 +289,7 @@ def prepare_insufficient_stock_order_validation_errors(exc):
)
errors.append(
ValidationError(
- f"Insufficient product stock: {item.variant}",
+ f"Insufficient product stock: {item.order_line or item.variant}",
code=OrderErrorCode.INSUFFICIENT_STOCK,
params={
"order_lines": [order_line_global_id]