Skip to content

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

Add generating voucher code if an empty string was provided in mutation

Aleksander Spyraefeddef636f8
diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.py
index 44ed7e0baf..968e07342f 100644
--- a/saleor/graphql/discount/mutations.py
+++ b/saleor/graphql/discount/mutations.py
@@ -2,6 +2,7 @@ import graphene
from graphql_jwt.decorators import permission_required
from ...discount import VoucherType, models
+from ...discount.utils import generate_voucher_code
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.scalars import Decimal
from ..product.types import Category, Collection, Product
@@ -121,6 +122,9 @@ class VoucherCreate(ModelMutation):
@classmethod
def clean_input(cls, info, instance, input, errors):
+ code = input.get('code', None)
+ if code == '':
+ input['code'] = generate_voucher_code()
cleaned_input = super().clean_input(info, instance, input, errors)
voucher_errors = validate_voucher(cleaned_input)
for err in voucher_errors:
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index f292185d7b..4fb36cdcc6 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -96,37 +96,39 @@ def test_sale_query(staff_api_client, sale, permission_manage_discounts):
assert data['startDate'] == sale.start_date.isoformat()
-def test_create_voucher(staff_api_client, permission_manage_discounts):
- query = """
- mutation voucherCreate(
- $type: VoucherTypeEnum, $name: String, $code: String,
- $discountValueType: DiscountValueTypeEnum,
- $discountValue: Decimal, $minAmountSpent: Decimal,
- $startDate: Date, $endDate: Date) {
- voucherCreate(input: {
- name: $name, type: $type, code: $code,
- discountValueType: $discountValueType,
- discountValue: $discountValue,
- minAmountSpent: $minAmountSpent,
- startDate: $startDate, endDate: $endDate}) {
- errors {
- field
- message
- }
- voucher {
- type
- minAmountSpent {
- amount
- }
- name
- code
- discountValueType
- startDate
- endDate
+CREATE_VOUCHER_MUTATION = """
+mutation voucherCreate(
+ $type: VoucherTypeEnum, $name: String, $code: String,
+ $discountValueType: DiscountValueTypeEnum,
+ $discountValue: Decimal, $minAmountSpent: Decimal,
+ $startDate: Date, $endDate: Date) {
+ voucherCreate(input: {
+ name: $name, type: $type, code: $code,
+ discountValueType: $discountValueType,
+ discountValue: $discountValue,
+ minAmountSpent: $minAmountSpent,
+ startDate: $startDate, endDate: $endDate}) {
+ errors {
+ field
+ message
+ }
+ voucher {
+ type
+ minAmountSpent {
+ amount
}
+ name
+ code
+ discountValueType
+ startDate
+ endDate
}
}
- """
+ }
+"""
+
+
+def test_create_voucher(staff_api_client, permission_manage_discounts):
start_date = date(day=1, month=1, year=2018)
end_date = date(day=1, month=1, year=2019)
variables = {
@@ -140,7 +142,9 @@ def test_create_voucher(staff_api_client, permission_manage_discounts):
'endDate': end_date.isoformat()}
response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_discounts])
+ CREATE_VOUCHER_MUTATION,
+ variables,
+ permissions=[permission_manage_discounts])
content = get_graphql_content(response)
data = content['data']['voucherCreate']['voucher']
assert data['type'] == VoucherType.VALUE.upper()
@@ -152,6 +156,30 @@ def test_create_voucher(staff_api_client, permission_manage_discounts):
assert data['endDate'] == end_date.isoformat()
+def test_create_voucher_with_empty_code(
+ staff_api_client, permission_manage_discounts):
+ start_date = date(day=1, month=1, year=2018)
+ end_date = date(day=1, month=1, year=2019)
+ variables = {
+ 'name': 'test voucher',
+ 'type': VoucherTypeEnum.VALUE.name,
+ 'code': '',
+ 'discountValueType': DiscountValueTypeEnum.FIXED.name,
+ 'discountValue': 10.12,
+ 'minAmountSpent': 1.12,
+ 'startDate': start_date.isoformat(),
+ 'endDate': end_date.isoformat()}
+
+ response = staff_api_client.post_graphql(
+ CREATE_VOUCHER_MUTATION,
+ variables,
+ permissions=[permission_manage_discounts])
+ content = get_graphql_content(response)
+ data = content['data']['voucherCreate']['voucher']
+ assert data['name'] == variables['name']
+ assert data['code'] != ''
+
+
def test_update_voucher(
staff_api_client, voucher, permission_manage_discounts):
query = """