Skip to content

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

Add informations to errors in OrderFulfill mutation

fowczrek319f68c7cacc
diff --git a/saleor/core/exceptions.py b/saleor/core/exceptions.py
index 03829f737c..540ef9e091 100644
--- a/saleor/core/exceptions.py
+++ b/saleor/core/exceptions.py
@@ -2,9 +2,11 @@ from ..checkout.error_codes import CheckoutErrorCode
class InsufficientStock(Exception):
- def __init__(self, item):
+ def __init__(self, item, order_line=None, warehouse_pk=None):
super().__init__("Insufficient stock for %r" % (item,))
self.item = item
+ self.order_line = order_line
+ self.warehouse_pk = warehouse_pk
self.code = CheckoutErrorCode.INSUFFICIENT_STOCK
diff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.py
index fd09845d86..5c3a37b5f6 100644
--- a/saleor/graphql/core/types/common.py
+++ b/saleor/graphql/core/types/common.py
@@ -115,6 +115,12 @@ class MetadataError(Error):
class OrderError(Error):
code = OrderErrorCode(description="The error code.", required=True)
+ warehouse = graphene.ID(
+ description="Warehouse ID which causes the error.", required=False,
+ )
+ order_line = graphene.ID(
+ description="Order line ID which causes the error.", required=False,
+ )
class PermissionGroupError(Error):
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.py
index a8ad1e4eb0..8e9282202f 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -133,10 +133,15 @@ class OrderFulfill(BaseMutation):
"item_pluralize": pluralize(line_quantity_unfulfilled),
"order_line": order_line,
}
+ order_line_global_id = graphene.Node.to_global_id(
+ "OrderLine", order_line.pk
+ )
raise ValidationError(
{
"order_line_id": ValidationError(
- msg, code=OrderErrorCode.FULFILL_ORDER_LINE
+ msg,
+ code=OrderErrorCode.FULFILL_ORDER_LINE,
+ params={"order_line": order_line_global_id},
)
}
)
@@ -151,6 +156,7 @@ class OrderFulfill(BaseMutation):
"warehouse": ValidationError(
"Duplicated warehouse ID.",
code=OrderErrorCode.DUPLICATED_INPUT_ITEM,
+ params={"warehouse": duplicates.pop()},
)
}
)
@@ -164,6 +170,7 @@ class OrderFulfill(BaseMutation):
"orderLineId": ValidationError(
"Duplicated order line ID.",
code=OrderErrorCode.DUPLICATED_INPUT_ITEM,
+ params={"order_line": duplicates.pop()},
)
}
)
@@ -235,11 +242,21 @@ class OrderFulfill(BaseMutation):
requester, order, dict(lines_for_warehouses), notify_customer
)
except InsufficientStock as exc:
+ order_line_global_id = graphene.Node.to_global_id(
+ "OrderLine", exc.order_line.pk
+ )
+ warehouse_global_id = graphene.Node.to_global_id(
+ "Warehouse", exc.warehouse_pk
+ )
raise ValidationError(
{
"stocks": ValidationError(
f"Insufficient product stock: {exc.item}",
code=OrderErrorCode.INSUFFICIENT_STOCK,
+ params={
+ "order_line": order_line_global_id,
+ "warehouse": warehouse_global_id,
+ },
)
}
)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index dc3214043b..a33a7428ba 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2654,6 +2654,8 @@ type OrderError {
field: String
message: String
code: OrderErrorCode!
+ warehouse: ID
+ orderLine: ID
}
enum OrderErrorCode {
diff --git a/saleor/warehouse/management.py b/saleor/warehouse/management.py
index e89c7e85b9..8dad4b1f68 100644
--- a/saleor/warehouse/management.py
+++ b/saleor/warehouse/management.py
@@ -161,14 +161,14 @@ def decrease_stock(order_line: "OrderLine", quantity: int, warehouse_pk: str):
.get(warehouse__pk=warehouse_pk)
)
except Stock.DoesNotExist:
- raise InsufficientStock(order_line.variant)
+ raise InsufficientStock(order_line.variant, order_line, warehouse_pk)
quantity_allocated = stock.allocations.aggregate(
quantity_allocated=Coalesce(Sum("quantity_allocated"), 0)
)["quantity_allocated"]
if stock.quantity - quantity_allocated < quantity:
- raise InsufficientStock(order_line.variant)
+ raise InsufficientStock(order_line.variant, order_line, warehouse_pk)
stock.quantity = F("quantity") - quantity
stock.save(update_fields=["quantity"])
diff --git a/tests/api/test_fulfillment.py b/tests/api/test_fulfillment.py
index f17e703676..242c8b17cc 100644
--- a/tests/api/test_fulfillment.py
+++ b/tests/api/test_fulfillment.py
@@ -23,6 +23,8 @@ mutation fulfillOrder(
field
code
message
+ warehouse
+ orderLine
}
}
}
@@ -231,22 +233,12 @@ def test_order_fulfill_with_one_line_empty_quantity(
)
- "quantity, error_code, error_field",
- (
- (0, OrderErrorCode.ZERO_QUANTITY.name, "lines"),
- (100, OrderErrorCode.FULFILL_ORDER_LINE.name, "orderLineId"),
- ),
-)
@patch("saleor.graphql.order.mutations.fulfillments.create_fulfillments")
-def test_order_fulfill_not_sufficient_quantity(
+def test_order_fulfill_zero_quantity(
mock_create_fulfillments,
staff_api_client,
staff_user,
order_with_lines,
- quantity,
- error_code,
- error_field,
permission_manage_orders,
warehouse,
):
@@ -261,7 +253,7 @@ def test_order_fulfill_not_sufficient_quantity(
"lines": [
{
"orderLineId": order_line_id,
- "stocks": [{"quantity": quantity, "warehouse": warehouse_id}],
+ "stocks": [{"quantity": 0, "warehouse": warehouse_id}],
}
]
},
@@ -272,8 +264,51 @@ def test_order_fulfill_not_sufficient_quantity(
content = get_graphql_content(response)
data = content["data"]["orderFulfill"]
assert data["orderErrors"]
- assert data["orderErrors"][0]["field"] == error_field
- assert data["orderErrors"][0]["code"] == error_code
+ error = data["orderErrors"][0]
+ assert error["field"] == "lines"
+ assert error["code"] == OrderErrorCode.ZERO_QUANTITY.name
+ assert not error["orderLine"]
+ assert not error["warehouse"]
+
+ mock_create_fulfillments.assert_not_called()
+
+
+@patch("saleor.graphql.order.mutations.fulfillments.create_fulfillments")
+def test_order_fulfill_fulfilled_order(
+ mock_create_fulfillments,
+ staff_api_client,
+ staff_user,
+ order_with_lines,
+ permission_manage_orders,
+ warehouse,
+):
+ query = ORDER_FULFILL_QUERY
+ order_id = graphene.Node.to_global_id("Order", order_with_lines.id)
+ order_line = order_with_lines.lines.first()
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.id)
+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
+ variables = {
+ "order": order_id,
+ "input": {
+ "lines": [
+ {
+ "orderLineId": order_line_id,
+ "stocks": [{"quantity": 100, "warehouse": warehouse_id}],
+ }
+ ]
+ },
+ }
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_orders]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfill"]
+ assert data["orderErrors"]
+ error = data["orderErrors"][0]
+ assert error["field"] == "orderLineId"
+ assert error["code"] == OrderErrorCode.FULFILL_ORDER_LINE.name
+ assert error["orderLine"] == order_line_id
+ assert not error["warehouse"]
mock_create_fulfillments.assert_not_called()
@@ -306,7 +341,9 @@ def test_order_fulfill_warehouse_with_insufficient_stock_exception(
},
}
- mock_create_fulfillments.side_effect = InsufficientStock(order_line.variant)
+ mock_create_fulfillments.side_effect = InsufficientStock(
+ order_line.variant, order_line, warehouse_no_shipping_zone.pk
+ )
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_orders]
@@ -314,8 +351,11 @@ def test_order_fulfill_warehouse_with_insufficient_stock_exception(
content = get_graphql_content(response)
data = content["data"]["orderFulfill"]
assert data["orderErrors"]
- assert data["orderErrors"][0]["field"] == "stocks"
- assert data["orderErrors"][0]["code"] == OrderErrorCode.INSUFFICIENT_STOCK.name
+ error = data["orderErrors"][0]
+ assert error["field"] == "stocks"
+ assert error["code"] == OrderErrorCode.INSUFFICIENT_STOCK.name
+ assert error["orderLine"] == order_line_id
+ assert error["warehouse"] == warehouse_id
@patch("saleor.graphql.order.mutations.fulfillments.create_fulfillments", autospec=True)
@@ -352,8 +392,11 @@ def test_order_fulfill_warehouse_duplicated_warehouse_id(
content = get_graphql_content(response)
data = content["data"]["orderFulfill"]
assert data["orderErrors"]
- assert data["orderErrors"][0]["field"] == "warehouse"
- assert data["orderErrors"][0]["code"] == OrderErrorCode.DUPLICATED_INPUT_ITEM.name
+ error = data["orderErrors"][0]
+ assert error["field"] == "warehouse"
+ assert error["code"] == OrderErrorCode.DUPLICATED_INPUT_ITEM.name
+ assert not error["orderLine"]
+ assert error["warehouse"] == warehouse_id
mock_create_fulfillments.assert_not_called()
@@ -392,8 +435,11 @@ def test_order_fulfill_warehouse_duplicated_order_line_id(
content = get_graphql_content(response)
data = content["data"]["orderFulfill"]
assert data["orderErrors"]
- assert data["orderErrors"][0]["field"] == "orderLineId"
- assert data["orderErrors"][0]["code"] == OrderErrorCode.DUPLICATED_INPUT_ITEM.name
+ error = data["orderErrors"][0]
+ assert error["field"] == "orderLineId"
+ assert error["code"] == OrderErrorCode.DUPLICATED_INPUT_ITEM.name
+ assert error["orderLine"] == order_line_id
+ assert not error["warehouse"]
mock_create_fulfillments.assert_not_called()
diff --git a/tests/test_fulfullments_actions.py b/tests/test_fulfullments_actions.py
index 6865e2867a..49a31e89ee 100644
--- a/tests/test_fulfullments_actions.py
+++ b/tests/test_fulfullments_actions.py
@@ -54,7 +54,7 @@ def test_create_fulfillments(
@patch("saleor.order.actions.send_fulfillment_confirmation_to_customer", autospec=True)
-def test_create_fulfillments_without_email(
+def test_create_fulfillments_without_notification(
mock_email_fulfillment, staff_user, order_with_lines, warehouse,
):
order = order_with_lines
@@ -258,9 +258,13 @@ def test_create_fulfillments_warehouse_with_out_of_stock(
]
}
- with pytest.raises(InsufficientStock):
+ with pytest.raises(InsufficientStock) as exc:
create_fulfillments(staff_user, order, fulfillment_lines_for_warehouses, True)
+ assert exc.value.item == order_line1.variant
+ assert exc.value.order_line == order_line1
+ assert exc.value.warehouse_pk == str(warehouse.pk)
+
order.refresh_from_db()
assert FulfillmentLine.objects.filter(fulfillment__order=order).count() == 0
@@ -294,9 +298,13 @@ def test_create_fulfillments_warehouse_without_stock(
]
}
- with pytest.raises(InsufficientStock):
+ with pytest.raises(InsufficientStock) as exc:
create_fulfillments(staff_user, order, fulfillment_lines_for_warehouses, True)
+ assert exc.value.item == order_line1.variant
+ assert exc.value.order_line == order_line1
+ assert exc.value.warehouse_pk == str(warehouse_no_shipping_zone.pk)
+
order.refresh_from_db()
assert FulfillmentLine.objects.filter(fulfillment__order=order).count() == 0