Skip to content

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

Disallow to change voucher usage limit setting when voucher has been already used (#16459)

Piotr Zabieglik2d7dca5c9e00
diff --git a/saleor/graphql/discount/mutations/voucher/voucher_update.py b/saleor/graphql/discount/mutations/voucher/voucher_update.py
index 3f89741715..551f270787 100644
--- a/saleor/graphql/discount/mutations/voucher/voucher_update.py
+++ b/saleor/graphql/discount/mutations/voucher/voucher_update.py
@@ -60,21 +60,11 @@ class VoucherUpdate(VoucherCreate):
@classmethod
def clean_voucher_usage_setting(cls, instance, data):
- """Ensure single use setting is not changed if voucher was already used."""
+ """Ensure voucher usage settings are not changed if voucher was already used."""
+ is_voucher_used = None
if "single_use" in data and instance.single_use != data["single_use"]:
- voucher_codes = instance.codes.all()
- used_codes = voucher_codes.filter(
- Exists(order_models.Order.objects.filter(voucher_code=OuterRef("code")))
- | Exists(
- order_models.OrderLine.objects.filter(voucher_code=OuterRef("code"))
- )
- | Exists(
- checkout_models.Checkout.objects.filter(
- voucher_code=OuterRef("code")
- )
- )
- )
- if used_codes.exists():
+ is_voucher_used = cls.is_voucher_used(instance)
+ if is_voucher_used:
raise ValidationError(
{
"single_use": ValidationError(
@@ -84,6 +74,33 @@ class VoucherUpdate(VoucherCreate):
)
}
)
+ if "usage_limit" in data and instance.usage_limit != data["usage_limit"]:
+ if is_voucher_used is None:
+ is_voucher_used = cls.is_voucher_used(instance)
+ if is_voucher_used:
+ raise ValidationError(
+ {
+ "usage_limit": ValidationError(
+ "Cannot change usage limit setting when any voucher code has "
+ "already been used.",
+ code=DiscountErrorCode.VOUCHER_ALREADY_USED.value,
+ )
+ }
+ )
+
+ @classmethod
+ def is_voucher_used(cls, instance) -> bool:
+ voucher_codes = instance.codes.all()
+ used_codes = voucher_codes.filter(
+ Exists(order_models.Order.objects.filter(voucher_code=OuterRef("code")))
+ | Exists(
+ order_models.OrderLine.objects.filter(voucher_code=OuterRef("code"))
+ )
+ | Exists(
+ checkout_models.Checkout.objects.filter(voucher_code=OuterRef("code"))
+ )
+ )
+ return used_codes.exists()
@classmethod
def construct_codes_instances(
diff --git a/saleor/graphql/discount/tests/mutations/test_voucher_update.py b/saleor/graphql/discount/tests/mutations/test_voucher_update.py
index 5dcbe6edd2..4e6892330f 100644
--- a/saleor/graphql/discount/tests/mutations/test_voucher_update.py
+++ b/saleor/graphql/discount/tests/mutations/test_voucher_update.py
@@ -415,3 +415,100 @@ def test_update_voucher_single_use_voucher_already_used_in_checkout(
assert errors[0]["field"] == "singleUse"
assert errors[0]["code"] == DiscountErrorCode.VOUCHER_ALREADY_USED.name
assert not errors[0]["voucherCodes"]
+
+
+def test_update_voucher_usage_limit_voucher_already_used(
+ staff_api_client,
+ voucher,
+ permission_manage_discounts,
+ checkout,
+):
+ # given
+ assert voucher.usage_limit is None
+ code_instance = voucher.codes.first()
+ checkout.voucher_code = code_instance.code
+ checkout.save(update_fields=["voucher_code"])
+
+ variables = {
+ "id": graphene.Node.to_global_id("Voucher", voucher.id),
+ "input": {"usageLimit": 10},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_VOUCHER_MUTATION, variables, permissions=[permission_manage_discounts]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ assert not content["data"]["voucherUpdate"]["voucher"]
+ errors = content["data"]["voucherUpdate"]["errors"]
+ assert len(errors) == 1
+
+ voucher.refresh_from_db()
+ assert voucher.usage_limit is None
+ assert errors[0]["field"] == "usageLimit"
+ assert errors[0]["code"] == DiscountErrorCode.VOUCHER_ALREADY_USED.name
+
+
+def test_update_voucher_usage_limit_the_same_value(
+ staff_api_client,
+ voucher,
+ permission_manage_discounts,
+ checkout,
+):
+ # given
+ usage_limit = 10
+ voucher.usage_limit = usage_limit
+ voucher.save(update_fields=["usage_limit"])
+
+ code_instance = voucher.codes.first()
+ checkout.voucher_code = code_instance.code
+ checkout.save(update_fields=["voucher_code"])
+
+ variables = {
+ "id": graphene.Node.to_global_id("Voucher", voucher.id),
+ "input": {"usageLimit": usage_limit},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_VOUCHER_MUTATION, variables, permissions=[permission_manage_discounts]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ assert not content["data"]["voucherUpdate"]["errors"]
+ assert content["data"]["voucherUpdate"]["voucher"]
+
+
+def test_update_voucher_with_deprecated_code_field(
+ staff_api_client,
+ voucher,
+ permission_manage_discounts,
+):
+ # given
+ new_code = "new-code"
+ code_instance = voucher.codes.get()
+ assert code_instance.code != new_code
+ variables = {
+ "id": graphene.Node.to_global_id("Voucher", voucher.id),
+ "input": {
+ "code": new_code,
+ },
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_VOUCHER_MUTATION, variables, permissions=[permission_manage_discounts]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ assert not content["data"]["voucherUpdate"]["errors"]
+ data = content["data"]["voucherUpdate"]["voucher"]
+ assert len(data["codes"]["edges"]) == 1
+ assert data["codes"]["edges"][0]["node"]["code"] == new_code
+
+ code_instance.refresh_from_db()
+ assert code_instance.code == new_code