Repository: saleor/saleor

Issue:
Bug: INSUFFICIENT_STOCK error does not return order lines ids in draftOrderComplete
### What are you trying to achieve?

Return order lines ids when `INSUFFICIENT_STOCK` occurs in `draftOrderComplete` mutation

### Steps to reproduce the problem

1. Create a draft order.
2. Add a line with a variant and quantity greater than stock availability.
3. Use `draftOrderComplete` mutation for this draft order.
```graphql
mutation OrderDraftFinalize($id: ID!) {
  draftOrderComplete(id: $id) {
    errors {
      field
      message
      code
      warehouse
      orderLines
      variants
      __typename
    }
  }
}
```

### What did you expect to happen?

An error will be returned correctly but `orderLines` field will be empty. So user is not able to identify an order line that has insufficient stock.

```graphql
{
  "data": {
    "draftOrderComplete": {
      "errors": [
        {
          "field": "lines",
          "message": "Insufficient product stock.",
          "code": "INSUFFICIENT_STOCK",
          "warehouse": null,
          "orderLines": [],
          "variants": null,
          "__typename": "OrderError"
        }
      ]
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 0,
      "maximumAvailable": 50000
    }
  }
}
```

### Logs

_No response_

### Environment

Saleor version: …
OS and version: …



Discussion:

Resolving patch:
diff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.py
--- a/saleor/graphql/order/utils.py
+++ b/saleor/graphql/order/utils.py
@@ -142,7 +142,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(
@@ -304,7 +308,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
--- a/saleor/warehouse/availability.py
+++ b/saleor/warehouse/availability.py
@@ -25,6 +25,7 @@
 if TYPE_CHECKING:
     from ..checkout.fetch import CheckoutLineInfo
     from ..checkout.models import CheckoutLine
+    from ..order.models import OrderLine
     from ..product.models import Product, ProductVariant
 
 
@@ -68,6 +69,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.
 
@@ -86,6 +88,7 @@ def check_stock_and_preorder_quantity(
             quantity,
             checkout_lines,
             check_reservations,
+            order_line,
         )
 
 
@@ -96,6 +99,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.
 
@@ -108,7 +112,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(
@@ -116,7 +124,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
+                    )
+                ]
             )
 
 


Test patch:
diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_complete.py b/saleor/graphql/order/tests/mutations/test_draft_order_complete.py
--- a/saleor/graphql/order/tests/mutations/test_draft_order_complete.py
+++ b/saleor/graphql/order/tests/mutations/test_draft_order_complete.py
@@ -25,6 +25,7 @@
                 code
                 message
                 variants
+                orderLines
             }
             order {
                 status
@@ -546,6 +547,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
 
@@ -559,15 +561,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(