Skip to content

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

Add available for purchase validation for validate_draft_order method

IKarbowiak32f73ff32b2b
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 909394852a..a56b290b28 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -828,6 +828,42 @@ def test_validate_draft_order_with_unpublished_product(draft_order):
assert error.code == OrderErrorCode.PRODUCT_NOT_PUBLISHED
+def test_validate_draft_order_with_unavailable_for_purchase_product(draft_order):
+ order = draft_order
+ line = order.lines.first()
+ variant = line.variant
+ variant.product.available_for_purchase = None
+ variant.product.save(update_fields=["available_for_purchase"])
+ line.refresh_from_db()
+
+ with pytest.raises(ValidationError) as e:
+ validate_draft_order(order, "US")
+ msg = "Can't finalize draft with product unavailable for purchase."
+ error = e.value.error_dict["lines"][0]
+
+ assert error.message == msg
+ assert error.code == OrderErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE
+
+
+def test_validate_draft_order_with_product_available_for_purchase_in_future(
+ draft_order,
+):
+ order = draft_order
+ line = order.lines.first()
+ variant = line.variant
+ variant.product.available_for_purchase = date.today() + timedelta(days=2)
+ variant.product.save(update_fields=["available_for_purchase"])
+ line.refresh_from_db()
+
+ with pytest.raises(ValidationError) as e:
+ validate_draft_order(order, "US")
+ msg = "Can't finalize draft with product unavailable for purchase."
+ error = e.value.error_dict["lines"][0]
+
+ assert error.message == msg
+ assert error.code == OrderErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE
+
+
def test_validate_draft_order_out_of_stock_variant(draft_order):
order = draft_order
line = order.lines.first()
diff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.py
index 4ce1476f9f..6e7dfb4f66 100644
--- a/saleor/graphql/order/utils.py
+++ b/saleor/graphql/order/utils.py
@@ -1,3 +1,5 @@
+import datetime
+
from django.core.exceptions import ValidationError
from ...core.exceptions import InsufficientStock
@@ -74,6 +76,22 @@ def validate_product_is_published(order):
)
+def validate_product_is_available_for_purchase(order):
+ for line in order:
+ if (
+ not line.variant.product.available_for_purchase
+ or datetime.date.today() < line.variant.product.available_for_purchase
+ ):
+ raise ValidationError(
+ {
+ "lines": ValidationError(
+ "Can't finalize draft with product unavailable for purchase.",
+ code=OrderErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE,
+ )
+ }
+ )
+
+
def validate_draft_order(order, country):
"""Check if the given order contains the proper data.
@@ -90,3 +108,4 @@ def validate_draft_order(order, country):
validate_total_quantity(order)
validate_order_lines(order, country)
validate_product_is_published(order)
+ validate_product_is_available_for_purchase(order)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 4a5b878fbd..8213db8bef 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2990,6 +2990,7 @@ enum OrderErrorCode {
GRAPHQL_ERROR
INVALID
PRODUCT_NOT_PUBLISHED
+ PRODUCT_UNAVAILABLE_FOR_PURCHASE
NOT_FOUND
ORDER_NO_SHIPPING_ADDRESS
PAYMENT_ERROR
diff --git a/saleor/order/error_codes.py b/saleor/order/error_codes.py
index 22bebc1723..e868994428 100644
--- a/saleor/order/error_codes.py
+++ b/saleor/order/error_codes.py
@@ -13,6 +13,7 @@ class OrderErrorCode(Enum):
GRAPHQL_ERROR = "graphql_error"
INVALID = "invalid"
PRODUCT_NOT_PUBLISHED = "product_not_published"
+ PRODUCT_UNAVAILABLE_FOR_PURCHASE = "product_unavailable_for_purchase"
NOT_FOUND = "not_found"
ORDER_NO_SHIPPING_ADDRESS = "order_no_shipping_address"
PAYMENT_ERROR = "payment_error"
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index fdc921cf04..04ec686d5f 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -885,6 +885,7 @@ def variant_without_inventory_tracking(
product_type=product_type_without_variant,
category=category,
is_published=True,
+ available_for_purchase=datetime.date.today(),
)
variant = ProductVariant.objects.create(
product=product,
@@ -1298,6 +1299,7 @@ def order_with_lines(order, product_type, category, shipping_zone, warehouse):
product_type=product_type,
category=category,
is_published=True,
+ available_for_purchase=datetime.date.today(),
)
variant = ProductVariant.objects.create(
product=product,
@@ -1330,6 +1332,7 @@ def order_with_lines(order, product_type, category, shipping_zone, warehouse):
product_type=product_type,
category=category,
is_published=True,
+ available_for_purchase=datetime.date.today(),
)
variant = ProductVariant.objects.create(
product=product, sku="SKU_B", cost_price=Money(2, "USD"), price_amount=20