Skip to content

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

Fix INSUFFICIENT_STOCK error not returning order lines ids (#15194)

SzymJ7c23bbd2fbb0
diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_complete.py b/saleor/graphql/order/tests/mutations/test_draft_order_complete.py
index 5038048d0e..c812e5d4d5 100644
--- a/saleor/graphql/order/tests/mutations/test_draft_order_complete.py
+++ b/saleor/graphql/order/tests/mutations/test_draft_order_complete.py
@@ -27,6 +27,7 @@ DRAFT_ORDER_COMPLETE_MUTATION = """
code
message
variants
+ orderLines
}
order {
status
@@ -748,6 +749,7 @@ def test_draft_order_complete_with_not_excluded_shipping_method(
def test_draft_order_complete_out_of_stock_variant(
staff_api_client, permission_group_manage_orders, staff_user, draft_order
):
+ # given
permission_group_manage_orders.user_set.add(staff_api_client.user)
order = draft_order
@@ -761,15 +763,19 @@ def test_draft_order_complete_out_of_stock_variant(
order_id = graphene.Node.to_global_id("Order", order.id)
variables = {"id": order_id}
+
+ # when
response = staff_api_client.post_graphql(DRAFT_ORDER_COMPLETE_MUTATION, variables)
content = get_graphql_content(response)
error = content["data"]["draftOrderComplete"]["errors"][0]
order.refresh_from_db()
+
+ # then
assert order.status == OrderStatus.DRAFT
assert order.origin == OrderOrigin.DRAFT
-
assert error["field"] == "lines"
assert error["code"] == OrderErrorCode.INSUFFICIENT_STOCK.name
+ assert error["orderLines"] == [graphene.Node.to_global_id("OrderLine", line_1.id)]
def test_draft_order_complete_existing_user_email_updates_user_field(
diff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.py
index 91cb2a9bf2..6faa54e767 100644
--- a/saleor/graphql/order/utils.py
+++ b/saleor/graphql/order/utils.py
@@ -148,7 +148,11 @@ def validate_order_lines(order: "Order", country: str, errors: T_ERRORS):
elif line.variant.track_inventory:
try:
check_stock_and_preorder_quantity(
- line.variant, country, order.channel.slug, line.quantity
+ line.variant,
+ country,
+ order.channel.slug,
+ line.quantity,
+ order_line=line,
)
except InsufficientStock as exc:
errors["lines"].extend(
@@ -326,7 +330,6 @@ def prepare_insufficient_stock_order_validation_errors(exc):
if item.warehouse_pk
else None
)
-
errors.append(
ValidationError(
"Insufficient product stock.",
diff --git a/saleor/warehouse/availability.py b/saleor/warehouse/availability.py
index d48d0e81fd..3683415bb8 100644
--- a/saleor/warehouse/availability.py
+++ b/saleor/warehouse/availability.py
@@ -22,6 +22,7 @@ from .reservations import get_listings_reservations
if TYPE_CHECKING:
from ..checkout.fetch import CheckoutLineInfo
from ..checkout.models import CheckoutLine
+ from ..order.models import OrderLine
from ..product.models import Product, ProductVariant
@@ -65,6 +66,7 @@ def check_stock_and_preorder_quantity(
quantity: int,
checkout_lines: Optional[list["CheckoutLine"]] = None,
check_reservations: bool = False,
+ order_line: Optional["OrderLine"] = None,
):
"""Validate if there is stock/preorder available for given variant.
@@ -83,6 +85,7 @@ def check_stock_and_preorder_quantity(
quantity,
checkout_lines,
check_reservations,
+ order_line,
)
@@ -93,6 +96,7 @@ def check_stock_quantity(
quantity: int,
checkout_lines: Optional[list["CheckoutLine"]] = None,
check_reservations: bool = False,
+ order_line: Optional["OrderLine"] = None,
):
"""Validate if there is stock available for given variant in given country.
@@ -105,7 +109,11 @@ def check_stock_quantity(
)
if not stocks:
raise InsufficientStock(
- [InsufficientStockData(variant=variant, available_quantity=0)]
+ [
+ InsufficientStockData(
+ variant=variant, available_quantity=0, order_line=order_line
+ )
+ ]
)
available_quantity = _get_available_quantity(
@@ -113,7 +121,11 @@ def check_stock_quantity(
)
if quantity > available_quantity:
raise InsufficientStock(
- [InsufficientStockData(variant=variant, available_quantity=0)]
+ [
+ InsufficientStockData(
+ variant=variant, available_quantity=0, order_line=order_line
+ )
+ ]
)