Skip to content

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

Add ProductSetAvailabilityForPurchase mutation

IKarbowiak823b899757ab
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 14f3c3f295..ca4f887f85 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -1,3 +1,4 @@
+import datetime
from collections import defaultdict
from typing import Iterable, List, Tuple, Union
@@ -1817,3 +1818,59 @@ class VariantImageUnassign(BaseMutation):
variant_image.delete()
return VariantImageUnassign(product_variant=variant, image=image)
+
+
+class ProductSetAvailabilityForPurchase(BaseMutation):
+ product = graphene.Field(Product)
+
+ class Arguments:
+ product_id = graphene.ID(
+ required=True,
+ description=(
+ "Id of product that availability for purchase should be changed."
+ ),
+ )
+ is_available = graphene.Boolean(
+ description="Determine if product should be available for purchase.",
+ required=True,
+ )
+ start_date = graphene.Date(
+ description=(
+ "A start date from which a product will be available for purchase. "
+ "When not set and is_available is set to True, "
+ "the current day is assumed."
+ ),
+ required=False,
+ )
+
+ class Meta:
+ description = "Set product availability for purchase date."
+ permissions = (ProductPermissions.MANAGE_PRODUCTS,)
+ error_type_class = ProductError
+ error_type_field = "product_errors"
+
+ @classmethod
+ def perform_mutation(cls, _root, info, **data):
+ product = cls.get_node_or_error(info, data.get("product_id"), only_type=Product)
+ is_available = data.get("is_available")
+ start_date = data.get("start_date")
+
+ if start_date and not is_available:
+ raise ValidationError(
+ {
+ "start_date": ValidationError(
+ "Cannot set start date when is_available is false.",
+ code=ProductErrorCode.INVALID,
+ )
+ }
+ )
+
+ if not is_available:
+ product.available_for_purchase = None
+ elif is_available and not start_date:
+ product.available_for_purchase = datetime.date.today()
+ else:
+ product.available_for_purchase = start_date
+
+ product.save(update_fields=["available_for_purchase", "updated_at"])
+ return ProductSetAvailabilityForPurchase(product=product)
diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.py
index 7c3312f1e2..0f9e5ce76b 100644
--- a/saleor/graphql/product/schema.py
+++ b/saleor/graphql/product/schema.py
@@ -84,6 +84,7 @@ from .mutations.products import (
ProductImageDelete,
ProductImageReorder,
ProductImageUpdate,
+ ProductSetAvailabilityForPurchase,
ProductTypeClearMeta,
ProductTypeClearPrivateMeta,
ProductTypeCreate,
@@ -439,6 +440,8 @@ class ProductMutations(graphene.ObjectType):
)
)
+ product_set_availability_for_purchase = ProductSetAvailabilityForPurchase.Field()
+
product_image_create = ProductImageCreate.Field()
product_image_delete = ProductImageDelete.Field()
product_image_bulk_delete = ProductImageBulkDelete.Field()
diff --git a/saleor/graphql/product/tests/test_product_set_availability_for_purchase.py b/saleor/graphql/product/tests/test_product_set_availability_for_purchase.py
new file mode 100644
index 0000000000..1e2a70f953
--- /dev/null
+++ b/saleor/graphql/product/tests/test_product_set_availability_for_purchase.py
@@ -0,0 +1,177 @@
+import datetime
+
+import graphene
+
+from ....product.error_codes import ProductErrorCode
+from ...tests.utils import assert_no_permission, get_graphql_content
+
+MUTATION_PRODUCT_SET_AVAILABILITY_FOR_PURCHASE = """
+ mutation ProductSetAvailabilityForPurchase(
+ $id: ID!, $is_available: Boolean!, $start_date: Date
+ ) {
+ productSetAvailabilityForPurchase(
+ productId: $id, isAvailable: $is_available, startDate: $start_date
+ ) {
+ product {
+ isAvailableForPurchase
+ availableForPurchase
+ }
+ productErrors {
+ code
+ field
+ }
+ }
+ }
+"""
+
+
+def test_product_set_availability_for_purchase_by_staff(
+ staff_api_client, permission_manage_products, product
+):
+ # given
+ query = MUTATION_PRODUCT_SET_AVAILABILITY_FOR_PURCHASE
+
+ staff_user = staff_api_client.user
+ staff_user.user_permissions.add(permission_manage_products)
+
+ start_date = datetime.date(1999, 1, 1)
+
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ "is_available": True,
+ "start_date": start_date,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productSetAvailabilityForPurchase"]
+ product_data = data["product"]
+
+ assert not data["productErrors"]
+ assert product_data["isAvailableForPurchase"] is True
+ assert product_data["availableForPurchase"] == start_date.strftime("%Y-%m-%d")
+
+
+def test_product_set_availability_for_purchase_by_staff_no_permissions(
+ staff_api_client, product
+):
+ # given
+ query = MUTATION_PRODUCT_SET_AVAILABILITY_FOR_PURCHASE
+
+ start_date = datetime.date(1999, 1, 1)
+
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ "is_available": True,
+ "start_date": start_date,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_product_set_availability_for_purchase_by_app(
+ app_api_client, permission_manage_products, product
+):
+ # given
+ query = MUTATION_PRODUCT_SET_AVAILABILITY_FOR_PURCHASE
+
+ app = app_api_client.app
+ app.permissions.add(permission_manage_products)
+
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ "is_available": True,
+ }
+
+ # when
+ response = app_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productSetAvailabilityForPurchase"]
+ product_data = data["product"]
+
+ assert not data["productErrors"]
+ assert product_data["isAvailableForPurchase"] is True
+ assert product_data["availableForPurchase"] == datetime.date.today().strftime(
+ "%Y-%m-%d"
+ )
+
+
+def test_product_set_availability_for_purchase_by_app_no_permissions(
+ app_api_client, product
+):
+ # given
+ query = MUTATION_PRODUCT_SET_AVAILABILITY_FOR_PURCHASE
+
+ start_date = datetime.date(1999, 1, 1)
+
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ "is_available": True,
+ "start_date": start_date,
+ }
+
+ # when
+ response = app_api_client.post_graphql(query, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_product_set_availability_for_purchase_by_customer(user_api_client, product):
+ # given
+ query = MUTATION_PRODUCT_SET_AVAILABILITY_FOR_PURCHASE
+
+ start_date = datetime.date(1999, 1, 1)
+
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ "is_available": True,
+ "start_date": start_date,
+ }
+
+ # when
+ response = user_api_client.post_graphql(query, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_product_set_availability_for_purchase_is_available_false_and_start_date_given(
+ staff_api_client, permission_manage_products, product
+):
+ # given
+ query = MUTATION_PRODUCT_SET_AVAILABILITY_FOR_PURCHASE
+
+ staff_user = staff_api_client.user
+ staff_user.user_permissions.add(permission_manage_products)
+
+ start_date = datetime.date(1999, 1, 1)
+
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ "is_available": False,
+ "start_date": start_date,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productSetAvailabilityForPurchase"]
+ product_data = data["product"]
+ errors = data["productErrors"]
+
+ assert not product_data
+ assert len(errors) == 1
+ assert errors[0]["code"] == ProductErrorCode.INVALID.name
+ assert errors[0]["field"] == "startDate"
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 8213db8bef..f71335584e 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2638,6 +2638,7 @@ type Mutation {
productClearMetadata(id: ID!, input: MetaPath!): ProductClearMeta @deprecated(reason: "Use the `deleteMetadata` mutation instead. This field will be removed after 2020-07-31.")
productUpdatePrivateMetadata(id: ID!, input: MetaInput!): ProductUpdatePrivateMeta @deprecated(reason: "Use the `updatePrivateMetadata` mutation instead. This field will be removed after 2020-07-31.")
productClearPrivateMetadata(id: ID!, input: MetaPath!): ProductClearPrivateMeta @deprecated(reason: "Use the `deletePrivateMetadata` mutation instead. This field will be removed after 2020-07-31.")
+ productSetAvailabilityForPurchase(isAvailable: Boolean!, productId: ID!, startDate: Date): ProductSetAvailabilityForPurchase
productImageCreate(input: ProductImageCreateInput!): ProductImageCreate
productImageDelete(id: ID!): ProductImageDelete
productImageBulkDelete(ids: [ID]!): ProductImageBulkDelete
@@ -3878,6 +3879,12 @@ type ProductPricingInfo {
priceRangeLocalCurrency: TaxedMoneyRange
}
+type ProductSetAvailabilityForPurchase {
+ errors: [Error!]! @deprecated(reason: "Use typed errors with error codes. This field will be removed after 2020-07-31.")
+ product: Product
+ productErrors: [ProductError!]!
+}
+
input ProductStockFilterInput {
warehouseIds: [ID!]
quantity: IntRangeInput