Skip to content

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

Add discount error codes

IKarbowiakfadf1a7d6aad
diff --git a/saleor/discount/error_codes.py b/saleor/discount/error_codes.py
new file mode 100644
index 0000000000..ca845d9090
--- /dev/null
+++ b/saleor/discount/error_codes.py
@@ -0,0 +1,9 @@
+from enum import Enum
+
+
+class DiscountErrorCode(Enum):
+ ALREADY_EXISTS = "already_exists"
+ INVALID = "invalid"
+ NOT_FOUND = "not_found"
+ REQUIRED = "required"
+ UNIQUE = "unique"
diff --git a/saleor/graphql/core/enums.py b/saleor/graphql/core/enums.py
index 2057e49366..ebc72fe22c 100644
--- a/saleor/graphql/core/enums.py
+++ b/saleor/graphql/core/enums.py
@@ -5,6 +5,7 @@ from ...checkout import error_codes as checkout_error_codes
from ...core import error_codes as core_error_codes
from ...core.permissions import get_permissions_enum_list
from ...core.weight import WeightUnits
+from ...discount import error_codes as discount_error_codes
from ...extensions import error_codes as extensions_error_codes
from ...extensions.plugins.vatlayer import TaxRateType as CoreTaxRateType
from ...giftcard import error_codes as giftcard_error_codes
@@ -85,6 +86,7 @@ WeightUnitsEnum = graphene.Enum(
AccountErrorCode = graphene.Enum.from_enum(account_error_codes.AccountErrorCode)
CheckoutErrorCode = graphene.Enum.from_enum(checkout_error_codes.CheckoutErrorCode)
+DiscountErrorCode = graphene.Enum.from_enum(discount_error_codes.DiscountErrorCode)
ExtensionsErrorCode = graphene.Enum.from_enum(
extensions_error_codes.ExtensionsErrorCode
)
diff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.py
index 082d53fc46..5d3777157f 100644
--- a/saleor/graphql/core/types/common.py
+++ b/saleor/graphql/core/types/common.py
@@ -5,6 +5,7 @@ from ...translations.enums import LanguageCodeEnum
from ..enums import (
AccountErrorCode,
CheckoutErrorCode,
+ DiscountErrorCode,
ExtensionsErrorCode,
GiftCardErrorCode,
MenuErrorCode,
@@ -52,6 +53,10 @@ class CheckoutError(Error):
code = CheckoutErrorCode(description="The error code.", required=True)
+class DiscountError(Error):
+ code = DiscountErrorCode(description="The error code.", required=True)
+
+
class MenuError(Error):
code = MenuErrorCode(description="The error code.", required=True)
diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.py
index e9d63bf092..8fc8dd3a02 100644
--- a/saleor/graphql/discount/mutations.py
+++ b/saleor/graphql/discount/mutations.py
@@ -1,18 +1,17 @@
import graphene
+from django.core.exceptions import ValidationError
from ...core.permissions import DiscountPermissions
-from ...core.utils.promo_code import (
- PromoCodeAlreadyExists,
- generate_promo_code,
- is_available_promo_code,
-)
+from ...core.utils.promo_code import generate_promo_code, is_available_promo_code
from ...discount import models
+from ...discount.error_codes import DiscountErrorCode
from ...product.tasks import (
update_products_minimal_variant_prices_of_catalogues_task,
update_products_minimal_variant_prices_of_discount_task,
)
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.scalars import Decimal
+from ..core.types.common import DiscountError
from ..product.types import Category, Collection, Product
from .enums import DiscountValueTypeEnum, VoucherTypeEnum
from .types import Sale, Voucher
@@ -86,7 +85,7 @@ class VoucherInput(graphene.InputObjectType):
description=("Voucher type: PRODUCT, CATEGORY SHIPPING or ENTIRE_ORDER.")
)
name = graphene.String(description="Voucher name.")
- code = graphene.String(decription="Code to use the voucher.")
+ code = graphene.String(description="Code to use the voucher.")
start_date = graphene.types.datetime.DateTime(
description="Start date of the voucher in ISO 8601 format."
)
@@ -141,6 +140,8 @@ class VoucherCreate(ModelMutation):
description = "Creates a new voucher."
model = models.Voucher
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
@classmethod
def clean_input(cls, info, instance, data):
@@ -148,7 +149,14 @@ class VoucherCreate(ModelMutation):
if code == "":
data["code"] = generate_promo_code()
elif not is_available_promo_code(code):
- raise PromoCodeAlreadyExists()
+ raise ValidationError(
+ {
+ "code": ValidationError(
+ "Promo code already exists.",
+ code=DiscountErrorCode.ALREADY_EXISTS,
+ )
+ }
+ )
cleaned_input = super().clean_input(info, instance, data)
min_spent_amount = cleaned_input.pop("min_amount_spent", None)
@@ -168,6 +176,8 @@ class VoucherUpdate(VoucherCreate):
description = "Updates a voucher."
model = models.Voucher
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
class VoucherDelete(ModelDeleteMutation):
@@ -178,6 +188,8 @@ class VoucherDelete(ModelDeleteMutation):
description = "Deletes a voucher."
model = models.Voucher
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
class VoucherBaseCatalogueMutation(BaseDiscountCatalogueMutation):
@@ -200,6 +212,8 @@ class VoucherAddCatalogues(VoucherBaseCatalogueMutation):
class Meta:
description = "Adds products, categories, collections to a voucher."
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
@classmethod
def perform_mutation(cls, _root, info, **data):
@@ -214,6 +228,8 @@ class VoucherRemoveCatalogues(VoucherBaseCatalogueMutation):
class Meta:
description = "Removes products, categories, collections from a voucher."
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
@classmethod
def perform_mutation(cls, _root, info, **data):
@@ -268,6 +284,8 @@ class SaleCreate(SaleUpdateMinimalVariantPriceMixin, ModelMutation):
description = "Creates a new sale."
model = models.Sale
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
class SaleUpdate(SaleUpdateMinimalVariantPriceMixin, ModelMutation):
@@ -281,6 +299,8 @@ class SaleUpdate(SaleUpdateMinimalVariantPriceMixin, ModelMutation):
description = "Updates a sale."
model = models.Sale
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
class SaleDelete(SaleUpdateMinimalVariantPriceMixin, ModelDeleteMutation):
@@ -291,6 +311,8 @@ class SaleDelete(SaleUpdateMinimalVariantPriceMixin, ModelDeleteMutation):
description = "Deletes a sale."
model = models.Sale
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
class SaleBaseCatalogueMutation(BaseDiscountCatalogueMutation):
@@ -313,6 +335,8 @@ class SaleAddCatalogues(SaleBaseCatalogueMutation):
class Meta:
description = "Adds products, categories, collections to a voucher."
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
@classmethod
def perform_mutation(cls, _root, info, **data):
@@ -327,6 +351,8 @@ class SaleRemoveCatalogues(SaleBaseCatalogueMutation):
class Meta:
description = "Removes products, categories, collections from a sale."
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
@classmethod
def perform_mutation(cls, _root, info, **data):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 7279ceecac..49c6bdb445 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1509,6 +1509,20 @@ input DigitalContentUrlCreateInput {
content: ID!
}
+type DiscountError {
+ field: String
+ message: String
+ code: DiscountErrorCode!
+}
+
+enum DiscountErrorCode {
+ ALREADY_EXISTS
+ INVALID
+ NOT_FOUND
+ REQUIRED
+ UNIQUE
+}
+
enum DiscountStatusEnum {
ACTIVE
EXPIRED
@@ -3816,6 +3830,7 @@ type Sale implements Node {
type SaleAddCatalogues {
errors: [Error!]!
sale: Sale
+ discountErrors: [DiscountError!]!
}
type SaleBulkDelete {
@@ -3836,11 +3851,13 @@ type SaleCountableEdge {
type SaleCreate {
errors: [Error!]!
+ discountErrors: [DiscountError!]!
sale: Sale
}
type SaleDelete {
errors: [Error!]!
+ discountErrors: [DiscountError!]!
sale: Sale
}
@@ -3865,6 +3882,7 @@ input SaleInput {
type SaleRemoveCatalogues {
errors: [Error!]!
sale: Sale
+ discountErrors: [DiscountError!]!
}
enum SaleSortField {
@@ -3905,6 +3923,7 @@ enum SaleType {
type SaleUpdate {
errors: [Error!]!
+ discountErrors: [DiscountError!]!
sale: Sale
}
@@ -4701,6 +4720,7 @@ type Voucher implements Node {
type VoucherAddCatalogues {
errors: [Error!]!
voucher: Voucher
+ discountErrors: [DiscountError!]!
}
type VoucherBulkDelete {
@@ -4721,11 +4741,13 @@ type VoucherCountableEdge {
type VoucherCreate {
errors: [Error!]!
+ discountErrors: [DiscountError!]!
voucher: Voucher
}
type VoucherDelete {
errors: [Error!]!
+ discountErrors: [DiscountError!]!
voucher: Voucher
}
@@ -4765,6 +4787,7 @@ input VoucherInput {
type VoucherRemoveCatalogues {
errors: [Error!]!
voucher: Voucher
+ discountErrors: [DiscountError!]!
}
enum VoucherSortField {
@@ -4808,6 +4831,7 @@ enum VoucherTypeEnum {
type VoucherUpdate {
errors: [Error!]!
+ discountErrors: [DiscountError!]!
voucher: Voucher
}
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index e51f360dca..55f53381ec 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -7,6 +7,7 @@ from django.utils import timezone
from django_countries import countries
from saleor.discount import DiscountValueType, VoucherType
+from saleor.discount.error_codes import DiscountErrorCode
from saleor.discount.models import Sale, Voucher
from saleor.graphql.discount.enums import DiscountValueTypeEnum, VoucherTypeEnum
from tests.api.utils import get_graphql_content
@@ -153,8 +154,9 @@ mutation voucherCreate(
startDate: $startDate, endDate: $endDate, usageLimit: $usageLimit
applyOncePerOrder: $applyOncePerOrder,
applyOncePerCustomer: $applyOncePerCustomer}) {
- errors {
+ discountErrors {
field
+ code
message
}
voucher {
@@ -256,10 +258,11 @@ def test_create_voucher_with_existing_gift_card_code(
CREATE_VOUCHER_MUTATION, variables, permissions=[permission_manage_discounts]
)
content = get_graphql_content(response)
- assert content["data"]["voucherCreate"]["errors"]
- errors = content["data"]["voucherCreate"]["errors"]
+ assert content["data"]["voucherCreate"]["discountErrors"]
+ errors = content["data"]["voucherCreate"]["discountErrors"]
assert len(errors) == 1
- assert errors[0]["field"] == "promoCode"
+ assert errors[0]["field"] == "code"
+ assert errors[0]["code"] == DiscountErrorCode.ALREADY_EXISTS.name
def test_create_voucher_with_existing_voucher_code(
@@ -282,10 +285,11 @@ def test_create_voucher_with_existing_voucher_code(
CREATE_VOUCHER_MUTATION, variables, permissions=[permission_manage_discounts]
)
content = get_graphql_content(response)
- assert content["data"]["voucherCreate"]["errors"]
- errors = content["data"]["voucherCreate"]["errors"]
+ assert content["data"]["voucherCreate"]["discountErrors"]
+ errors = content["data"]["voucherCreate"]["discountErrors"]
assert len(errors) == 1
- assert errors[0]["field"] == "promoCode"
+ assert errors[0]["field"] == "code"
+ assert errors
def test_update_voucher(staff_api_client, voucher, permission_manage_discounts):
@@ -298,8 +302,9 @@ def test_update_voucher(staff_api_client, voucher, permission_manage_discounts):
applyOncePerOrder: $applyOncePerOrder,
minCheckoutItemsQuantity: $minCheckoutItemsQuantity
}) {
- errors {
+ discountErrors {
field
+ code
message
}
voucher {
@@ -345,8 +350,9 @@ def test_voucher_delete_mutation(
name
id
}
- errors {
+ discountErrors {
field
+ code
message
}
}
@@ -375,8 +381,9 @@ def test_voucher_add_catalogues(
query = """
mutation voucherCataloguesAdd($id: ID!, $input: CatalogueInput!) {
voucherCataloguesAdd(id: $id, input: $input) {
- errors {
+ discountErrors {
field
+ code
message
}
}
@@ -400,7 +407,7 @@ def test_voucher_add_catalogues(
content = get_graphql_content(response)
data = content["data"]["voucherCataloguesAdd"]
- assert not data["errors"]
+ assert not data["discountErrors"]
assert product in voucher.products.all()
assert category in voucher.categories.all()
assert collection in voucher.collections.all()
@@ -421,8 +428,9 @@ def test_voucher_remove_catalogues(
query = """
mutation voucherCataloguesRemove($id: ID!, $input: CatalogueInput!) {
voucherCataloguesRemove(id: $id, input: $input) {
- errors {
+ discountErrors {
field
+ code
message
}
}
@@ -446,7 +454,7 @@ def test_voucher_remove_catalogues(
content = get_graphql_content(response)
data = content["data"]["voucherCataloguesRemove"]
- assert not data["errors"]
+ assert not data["discountErrors"]
assert product not in voucher.products.all()
assert category not in voucher.categories.all()
assert collection not in voucher.collections.all()
@@ -458,8 +466,9 @@ def test_voucher_add_no_catalogues(
query = """
mutation voucherCataloguesAdd($id: ID!, $input: CatalogueInput!) {
voucherCataloguesAdd(id: $id, input: $input) {
- errors {
+ discountErrors {
field
+ code
message
}
}
@@ -475,7 +484,7 @@ def test_voucher_add_no_catalogues(
content = get_graphql_content(response)
data = content["data"]["voucherCataloguesAdd"]
- assert not data["errors"]
+ assert not data["discountErrors"]
assert not voucher.products.exists()
assert not voucher.categories.exists()
assert not voucher.collections.exists()
@@ -496,8 +505,9 @@ def test_voucher_remove_no_catalogues(
query = """
mutation voucherCataloguesAdd($id: ID!, $input: CatalogueInput!) {
voucherCataloguesAdd(id: $id, input: $input) {
- errors {
+ discountErrors {
field
+ code
message
}
}
@@ -513,7 +523,7 @@ def test_voucher_remove_no_catalogues(
content = get_graphql_content(response)
data = content["data"]["voucherCataloguesAdd"]
- assert not data["errors"]
+ assert not data["discountErrors"]
assert voucher.products.exists()
assert voucher.categories.exists()
assert voucher.collections.exists()
@@ -534,8 +544,9 @@ def test_create_sale(staff_api_client, permission_manage_discounts):
startDate
endDate
}
- errors {
+ discountErrors {
field
+ code
message
}
}
@@ -568,8 +579,9 @@ def test_update_sale(staff_api_client, sale, permission_manage_discounts):
query = """
mutation saleUpdate($type: DiscountValueTypeEnum, $id: ID!) {
saleUpdate(id: $id, input: {type: $type}) {
- errors {
+ discountErrors {
field
+ code
message
}
sale {
@@ -602,8 +614,9 @@ def test_sale_delete_mutation(staff_api_client, sale, permission_manage_discount
name
id
}
- errors {
+ discountErrors {
field
+ code
message
}
}
@@ -627,8 +640,9 @@ def test_sale_add_catalogues(
query = """
mutation saleCataloguesAdd($id: ID!, $input: CatalogueInput!) {
saleCataloguesAdd(id: $id, input: $input) {
- errors {
+ discountErrors {
field
+ code
message
}
}
@@ -652,7 +666,7 @@ def test_sale_add_catalogues(
content = get_graphql_content(response)
data = content["data"]["saleCataloguesAdd"]
- assert not data["errors"]
+ assert not data["discountErrors"]
assert product in sale.products.all()
assert category in sale.categories.all()
assert collection in sale.collections.all()
@@ -668,8 +682,9 @@ def test_sale_remove_catalogues(
query = """
mutation saleCataloguesRemove($id: ID!, $input: CatalogueInput!) {
saleCataloguesRemove(id: $id, input: $input) {
- errors {
+ discountErrors {
field
+ code
message
}
}
@@ -693,7 +708,7 @@ def test_sale_remove_catalogues(
content = get_graphql_content(response)
data = content["data"]["saleCataloguesRemove"]
- assert not data["errors"]
+ assert not data["discountErrors"]
assert product not in sale.products.all()
assert category not in sale.categories.all()
assert collection not in sale.collections.all()
@@ -703,8 +718,9 @@ def test_sale_add_no_catalogues(staff_api_client, sale, permission_manage_discou
query = """
mutation saleCataloguesAdd($id: ID!, $input: CatalogueInput!) {
saleCataloguesAdd(id: $id, input: $input) {
- errors {
+ discountErrors {
field
+ code
message
}
}
@@ -720,7 +736,7 @@ def test_sale_add_no_catalogues(staff_api_client, sale, permission_manage_discou
content = get_graphql_content(response)
data = content["data"]["saleCataloguesAdd"]
- assert not data["errors"]
+ assert not data["discountErrors"]
assert not sale.products.exists()
assert not sale.categories.exists()
assert not sale.collections.exists()
@@ -736,8 +752,9 @@ def test_sale_remove_no_catalogues(
query = """
mutation saleCataloguesAdd($id: ID!, $input: CatalogueInput!) {
saleCataloguesAdd(id: $id, input: $input) {
- errors {
+ discountErrors {
field
+ code
message
}
}
@@ -753,7 +770,7 @@ def test_sale_remove_no_catalogues(
content = get_graphql_content(response)
data = content["data"]["saleCataloguesAdd"]
- assert not data["errors"]
+ assert not data["discountErrors"]
assert sale.products.exists()
assert sale.categories.exists()
assert sale.collections.exists()