Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add available for purchase validation in CheckoutLinesAdd mutation
IKarbowiak5fc465b27d02
diff --git a/saleor/checkout/error_codes.py b/saleor/checkout/error_codes.pyindex 4792af1245..98e13e270f 100644--- a/saleor/checkout/error_codes.py+++ b/saleor/checkout/error_codes.py@@ -6,6 +6,7 @@ class CheckoutErrorCode(Enum):CHECKOUT_NOT_FULLY_PAID = "checkout_not_fully_paid"GRAPHQL_ERROR = "graphql_error"PRODUCT_NOT_PUBLISHED = "product_not_published"+ PRODUCT_UNAVAILABLE_FOR_PURCHASE = "product_unavailable_for_purchase"INSUFFICIENT_STOCK = "insufficient_stock"INVALID = "invalid"INVALID_SHIPPING_METHOD = "invalid_shipping_method"diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex 50150ddc8b..c5c9be5fb7 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -1,3 +1,4 @@+import datetimefrom typing import List, Optional, Tupleimport graphene@@ -345,6 +346,29 @@ class CheckoutLinesAdd(BaseMutation):error_type_class = CheckoutErrorerror_type_field = "checkout_errors"+ @classmethod+ def validate_variants_available_for_purchase(cls, variants):+ not_available_variants = [+ variant.pk+ for variant in variants+ if not variant.product.available_for_purchase+ or datetime.date.today() < variant.product.available_for_purchase+ ]+ if not_available_variants:+ variant_ids = [+ graphene.Node.to_global_id("ProductVariant", pk)+ for pk in not_available_variants+ ]+ raise ValidationError(+ {+ "lines": ValidationError(+ "Cannot add lines for unavailable for purchase variants.",+ code=CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE,+ params={"variants": variant_ids},+ )+ }+ )+@classmethoddef perform_mutation(cls, _root, info, checkout_id, lines, replace=False):checkout = cls.get_node_or_error(@@ -356,6 +380,7 @@ class CheckoutLinesAdd(BaseMutation):quantities = [line.get("quantity") for line in lines]check_lines_quantity(variants, quantities, checkout.get_country())+ cls.validate_variants_available_for_purchase(variants)if variants and quantities:for variant, quantity in zip(variants, quantities):diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.pyindex 4c15221098..27da25a23d 100644--- a/saleor/graphql/checkout/tests/test_checkout.py+++ b/saleor/graphql/checkout/tests/test_checkout.py@@ -845,6 +845,7 @@ MUTATION_CHECKOUT_LINES_ADD = """fieldcodemessage+ variants}}}"""@@ -905,6 +906,34 @@ def test_checkout_lines_add_with_unpublished_product(assert error["code"] == "PRODUCT_NOT_PUBLISHED"+def test_checkout_lines_add_with_unavailable_for_purchase_product(+ user_api_client, checkout_with_item, stock+):+ # given+ variant = stock.product_variant+ product = stock.product_variant.product+ product.available_for_purchase = None+ product.save(update_fields=["available_for_purchase"])++ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_item.pk)++ variables = {+ "checkoutId": checkout_id,+ "lines": [{"variantId": variant_id, "quantity": 1}],+ }++ # when+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)++ # then+ content = get_graphql_content(response)+ error = content["data"]["checkoutLinesAdd"]["checkoutErrors"][0]+ assert error["field"] == "lines"+ assert error["code"] == CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE.name+ assert error["variants"] == [variant_id]++def test_checkout_lines_add_too_many(user_api_client, checkout_with_item, stock):variant = stock.product_variantvariant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)@@ -923,6 +952,7 @@ def test_checkout_lines_add_too_many(user_api_client, checkout_with_item, stock)"field": "quantity","message": "Cannot add more than 50 times this item.","code": "QUANTITY_GREATER_THAN_LIMIT",+ "variants": None,}]diff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.pyindex 9df4b731a7..693aef481f 100644--- a/saleor/graphql/core/types/common.py+++ b/saleor/graphql/core/types/common.py@@ -102,6 +102,11 @@ class StaffError(AccountError):class CheckoutError(Error):code = CheckoutErrorCode(description="The error code.", required=True)+ variants = graphene.List(+ graphene.NonNull(graphene.ID),+ description="List of varint IDs which causes the error.",+ required=False,+ )class DiscountError(Error):diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex f71335584e..11e73507b1 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -948,6 +948,7 @@ type CheckoutError {field: Stringmessage: Stringcode: CheckoutErrorCode!+ variants: [ID!]}enum CheckoutErrorCode {@@ -955,6 +956,7 @@ enum CheckoutErrorCode {CHECKOUT_NOT_FULLY_PAIDGRAPHQL_ERRORPRODUCT_NOT_PUBLISHED+ PRODUCT_UNAVAILABLE_FOR_PURCHASEINSUFFICIENT_STOCKINVALIDINVALID_SHIPPING_METHODdiff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex 04ec686d5f..fea1419ed2 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -732,6 +732,7 @@ def product(product_type, category, warehouse):product_type=product_type,category=category,is_published=True,+ available_for_purchase=datetime.date(1999, 1, 1),)associate_attribute_values_to_instance(product, product_attr, product_attr_value)@@ -759,6 +760,7 @@ def product_with_single_variant(product_type, category, warehouse):product_type=product_type,category=category,is_published=True,+ available_for_purchase=datetime.date(1999, 1, 1),)variant = ProductVariant.objects.create(product=product,@@ -778,6 +780,7 @@ def product_with_two_variants(product_type, category, warehouse):product_type=product_type,category=category,is_published=True,+ available_for_purchase=datetime.date(1999, 1, 1),)variants = [@@ -819,6 +822,7 @@ def product_with_variant_with_two_attributes(product_type=product_type,category=category,is_published=True,+ available_for_purchase=datetime.date(1999, 1, 1),)variant = ProductVariant.objects.create(@@ -867,6 +871,7 @@ def product_with_default_variant(product_type_without_variant, category, warehouproduct_type=product_type_without_variant,category=category,is_published=True,+ available_for_purchase=datetime.date(1999, 1, 1),)variant = ProductVariant.objects.create(product=product, sku="1234", track_inventory=True, price_amount=Decimal(10)