Skip to content

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

Use enum types for voucher

Adam Janikcbed6ab2517c
diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.py
index 1d082d2cdf..5408c38a56 100644
--- a/saleor/graphql/discount/mutations.py
+++ b/saleor/graphql/discount/mutations.py
@@ -3,6 +3,7 @@ import graphene
from ...discount import VoucherType, models
from ..core.mutations import ModelDeleteMutation, ModelMutation
from ..core.types import Decimal
+from .types import ApplyToEnum, DiscountValueTypeEnum, VoucherTypeEnum
def validate_voucher(voucher_data):
@@ -18,7 +19,7 @@ def validate_voucher(voucher_data):
class VoucherInput(graphene.InputObjectType):
- type = graphene.String(
+ type = VoucherTypeEnum(
description='Voucher type: product, category shipping or value.')
name = graphene.String(description='Voucher name.')
code = graphene.String(decription='Code to use the voucher.')
@@ -26,13 +27,12 @@ class VoucherInput(graphene.InputObjectType):
description='Start date of the voucher in ISO 8601 format.')
end_date = graphene.types.datetime.DateTime(
description='End date of the voucher in ISO 8601 format.')
- discount_value_type = graphene.String(
+ discount_value_type = DiscountValueTypeEnum(
description='Choices: fixed or percentage.')
discount_value = Decimal(description='Value of the voucher.')
product = graphene.ID(description='Product related to the discount.')
category = graphene.ID(description='Category related to the discount.')
- apply_to = graphene.String(
- description='Single item (one) or all matching products (all).')
+ apply_to = ApplyToEnum(description='Single item or all matching products.')
limit = Decimal(description='Limit value of the discount.')
diff --git a/saleor/graphql/discount/types.py b/saleor/graphql/discount/types.py
index 177b0a7d9a..2782d1db17 100644
--- a/saleor/graphql/discount/types.py
+++ b/saleor/graphql/discount/types.py
@@ -1,6 +1,8 @@
+import graphene
from graphene import relay
-from ...discount import models
+from ...discount import (
+ DiscountValueType, VoucherApplyToProduct, VoucherType, models)
from ..core.types import CountableDjangoObjectType
@@ -29,3 +31,20 @@ class Sale(CountableDjangoObjectType):
'type': ['icontains'],
'value': ['gte', 'lte']}
model = models.Sale
+
+
+class VoucherTypeEnum(graphene.Enum):
+ PRODUCT = VoucherType.PRODUCT
+ CATEGORY = VoucherType.CATEGORY
+ SHIPPING = VoucherType.SHIPPING
+ VALUE = VoucherType.VALUE
+
+
+class DiscountValueTypeEnum(graphene.Enum):
+ FIXED = DiscountValueType.FIXED
+ PERCENTAGE = DiscountValueType.PERCENTAGE
+
+
+class ApplyToEnum(graphene.Enum):
+ ONE_PRODUCT = VoucherApplyToProduct.ONE_PRODUCT
+ ALL_PRODUCTS = VoucherApplyToProduct.ALL_PRODUCTS
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index 818b2581ad..ef2ca583f6 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -7,6 +7,8 @@ from tests.utils import get_graphql_content
from saleor.discount import (
DiscountValueType, VoucherApplyToProduct, VoucherType)
+from saleor.graphql.discount.types import (
+ ApplyToEnum, DiscountValueTypeEnum, VoucherTypeEnum)
from .utils import assert_no_permission
@@ -103,8 +105,9 @@ def test_sale_query(
def test_create_voucher(user_api_client, admin_api_client):
query = """
mutation voucherCreate(
- $type: String, $name: String, $code: String, $applyTo: String
- $discountValueType: String, $discountValue: Decimal, $limit: Decimal) {
+ $type: VoucherTypeEnum, $name: String, $code: String,
+ $applyTo: ApplyToEnum, $discountValueType: DiscountValueTypeEnum,
+ $discountValue: Decimal, $limit: Decimal) {
voucherCreate(input: {
name: $name, type: $type, code: $code, applyTo: $applyTo,
discountValueType: $discountValueType, discountValue: $discountValue,
@@ -128,10 +131,10 @@ def test_create_voucher(user_api_client, admin_api_client):
"""
variables = json.dumps({
'name': 'test voucher',
- 'type': VoucherType.VALUE,
+ 'type': VoucherTypeEnum.VALUE.name,
'code': 'testcode123',
- 'applyTo': VoucherApplyToProduct.ALL_PRODUCTS,
- 'discountValueType': DiscountValueType.FIXED,
+ 'applyTo': ApplyToEnum.ALL_PRODUCTS.name,
+ 'discountValueType': DiscountValueTypeEnum.FIXED.name,
'discountValue': '10.12',
'limit': '1.12'})
response = user_api_client.post(
@@ -153,8 +156,8 @@ def test_create_voucher(user_api_client, admin_api_client):
def test_update_voucher(user_api_client, admin_api_client, voucher):
query = """
- mutation voucherUpdate($code: String, $discountValueType: String,
- $id: ID!) {
+ mutation voucherUpdate($code: String,
+ $discountValueType: DiscountValueTypeEnum, $id: ID!) {
voucherUpdate(id: $id, input: {
code: $code, discountValueType: $discountValueType}) {
errors {
@@ -175,7 +178,7 @@ def test_update_voucher(user_api_client, admin_api_client, voucher):
variables = json.dumps({
'id': graphene.Node.to_global_id('Voucher', voucher.id),
'code': 'testcode123',
- 'discountValueType': DiscountValueType.PERCENTAGE})
+ 'discountValueType': DiscountValueTypeEnum.PERCENTAGE.name})
response = user_api_client.post(
reverse('api'), {'query': query, 'variables': variables})
@@ -325,7 +328,8 @@ def test_sale_delete_mutation(user_api_client, admin_api_client, sale):
def test_validate_voucher(voucher, admin_api_client, product):
query = """
- mutation voucherUpdate($product_id: ID, $id: ID!, $type: String) {
+ mutation voucherUpdate(
+ $product_id: ID, $id: ID!, $type: VoucherTypeEnum) {
voucherUpdate(
id: $id, input: {product: $product_id, type: $type}) {
errors {
@@ -343,7 +347,7 @@ def test_validate_voucher(voucher, admin_api_client, product):
assert not voucher.product
variables = json.dumps({
- 'type': VoucherType.PRODUCT,
+ 'type': VoucherTypeEnum.PRODUCT.name,
'id': graphene.Node.to_global_id('Voucher', voucher.id),
'product': graphene.Node.to_global_id('Product', product.id)})