Skip to content

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

Secure waiting fulfillments amount

Tomasz Szymanski7019789cbfdb
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.py
index f22fa0f40e..6d2f871960 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -93,7 +93,19 @@ class OrderFulfill(BaseMutation):
@classmethod
def clean_lines(cls, order_lines, quantities):
for order_line, line_quantities in zip(order_lines, quantities):
- line_quantity_unfulfilled = order_line.quantity_unfulfilled
+ quantity_awaiting = sum(
+ [
+ f_line.quantity
+ if f_line.fulfillment.status
+ == FulfillmentStatus.WAITING_FOR_APPROVAL
+ else 0
+ for f_line in order_line.fulfillment_lines.all()
+ ]
+ )
+
+ line_quantity_unfulfilled = (
+ order_line.quantity_unfulfilled - quantity_awaiting
+ )
if sum(line_quantities) > line_quantity_unfulfilled:
msg = (
@@ -189,7 +201,12 @@ class OrderFulfill(BaseMutation):
lines_ids = [line["order_line_id"] for line in lines]
cls.check_lines_for_duplicates(lines_ids)
order_lines = cls.get_nodes_or_error(
- lines_ids, field="lines", only_type=OrderLine
+ lines_ids,
+ field="lines",
+ only_type=OrderLine,
+ qs=order_models.OrderLine.objects.prefetch_related(
+ "fulfillment_lines__fulfillment"
+ ),
)
cls.clean_lines(order_lines, quantities_for_lines)
diff --git a/saleor/graphql/order/tests/test_fulfillment.py b/saleor/graphql/order/tests/test_fulfillment.py
index bbe8b44e0b..d6983cc88e 100644
--- a/saleor/graphql/order/tests/test_fulfillment.py
+++ b/saleor/graphql/order/tests/test_fulfillment.py
@@ -8,7 +8,7 @@ from ....core.exceptions import InsufficientStock, InsufficientStockData
from ....order import OrderStatus
from ....order.error_codes import OrderErrorCode
from ....order.events import OrderEvents
-from ....order.models import Fulfillment, FulfillmentStatus
+from ....order.models import Fulfillment, FulfillmentLine, FulfillmentStatus
from ....warehouse.models import Allocation, Stock
from ...tests.utils import assert_no_permission, get_graphql_content
@@ -128,6 +128,59 @@ def test_order_fulfill(
)
+@patch("saleor.graphql.order.mutations.fulfillments.create_fulfillments")
+def test_order_fulfill_above_available_quantity(
+ mock_create_fulfillments,
+ staff_api_client,
+ staff_user,
+ order_with_lines,
+ permission_manage_orders,
+ warehouse,
+):
+ order = order_with_lines
+ query = ORDER_FULFILL_QUERY
+ order_id = graphene.Node.to_global_id("Order", order.id)
+ order_line, order_line2 = order.lines.all()
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.id)
+ order_line2_id = graphene.Node.to_global_id("OrderLine", order_line2.id)
+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
+ fulfillment = Fulfillment.objects.create(
+ order=order, status=FulfillmentStatus.WAITING_FOR_APPROVAL
+ )
+ FulfillmentLine.objects.create(
+ order_line=order_line,
+ quantity=1,
+ stock=warehouse.stock_set.first(),
+ fulfillment_id=fulfillment.pk,
+ )
+ variables = {
+ "order": order_id,
+ "input": {
+ "notifyCustomer": True,
+ "lines": [
+ {
+ "orderLineId": order_line_id,
+ "stocks": [{"quantity": 3, "warehouse": warehouse_id}],
+ },
+ {
+ "orderLineId": order_line2_id,
+ "stocks": [{"quantity": 2, "warehouse": warehouse_id}],
+ },
+ ],
+ },
+ }
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_orders]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfill"]
+ error = data["errors"][0]
+ assert error["field"] == "orderLineId"
+ assert error["code"] == OrderErrorCode.FULFILL_ORDER_LINE.name
+
+ mock_create_fulfillments.assert_not_called()
+
+
@patch("saleor.graphql.order.mutations.fulfillments.create_fulfillments")
def test_order_fulfill_as_app(
mock_create_fulfillments,