Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add constraints on order line quantity value
Marcin Gębalab0b9e437b26c
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.pyindex fbf961e212..49c6fab41e 100644--- a/saleor/graphql/order/mutations/draft_orders.py+++ b/saleor/graphql/order/mutations/draft_orders.py@@ -242,10 +242,16 @@ class DraftOrderLineCreate(BaseMutation):cls.add_error(errors, 'order_id', 'Only draft orders can be edited.')+ quantity = input['quantity']+ if quantity <= 0:+ cls.add_error(+ errors, 'quantity',+ 'Ensure this value is greater than or equal to 1.')+line = Noneif not errors:line = add_variant_to_order(- order, variant, input['quantity'], allow_overselling=True)+ order, variant, quantity, allow_overselling=True)recalculate_order(order)return DraftOrderLineCreate(order=order, order_line=line, errors=errors)@@ -311,11 +317,17 @@ class DraftOrderLineUpdate(ModelMutation):if instance.order.status != OrderStatus.DRAFT:cls.add_error(errors, 'id', 'Only draft orders can be edited.')++ quantity = input['quantity']+ if quantity <= 0:+ cls.add_error(+ errors, 'quantity',+ 'Ensure this value is greater than or equal to 1.')return cleaned_input@classmethoddef save(cls, info, instance, cleaned_input):- instance.save()+ instance.save(update_fields=['quantity'])recalculate_order(instance.order)@classmethoddiff --git a/tests/api/test_order.py b/tests/api/test_order.pyindex 42b4a34fae..6c8246b856 100644--- a/tests/api/test_order.py+++ b/tests/api/test_order.py@@ -407,6 +407,17 @@ def test_draft_order_line_create(assert data['orderLine']['productSku'] == variant.skuassert data['orderLine']['quantity'] == old_quantity + quantity+ # mutation should fail when quantity is lower than 1+ variables = json.dumps({+ 'orderId': order_id, 'variantId': variant_id, 'quantity': 0})+ response = staff_api_client.post(+ reverse('api'),+ {'query': DRAFT_ORDER_LINE_CREATE_MUTATION, 'variables': variables})+ content = get_graphql_content(response)+ data = content['data']['draftOrderLineCreate']+ assert 'errors' in data+ assert data['errors'][0]['field'] == 'quantity'+def test_require_draft_order_when_creating_lines(order_with_lines, admin_api_client):@@ -471,6 +482,16 @@ def test_draft_order_line_update(data = content['data']['draftOrderLineUpdate']assert data['orderLine']['quantity'] == new_quantity+ # mutation should fail when quantity is lower than 1+ variables = json.dumps({'lineId': line_id, 'quantity': 0})+ response = staff_api_client.post(+ reverse('api'),+ {'query': DRAFT_ORDER_LINE_UPDATE_MUTATION, 'variables': variables})+ content = get_graphql_content(response)+ data = content['data']['draftOrderLineUpdate']+ assert 'errors' in data+ assert data['errors'][0]['field'] == 'quantity'+def test_require_draft_order_when_updating_lines(order_with_lines, admin_api_client):