Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use discount code if provided (#11015)
Michał Ociepka9b96c1602b1f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 78e33ff7f4..813b5b353a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -30,6 +30,7 @@ All notable, unreleased changes to this project will be documented in this file.- Remove `CUSTOMER_UPDATED` webhook trigger from address mutations - #11395 by @jakubkuc- Drop `Django.Auth` - #11305 by @fowczarek- Propagate voucher discount between checkout lines when charge_taxes is disabled - #11632 by @maarcingebala+- Accept the gift card code provided in the input - by @mociepka# 3.9.0diff --git a/saleor/giftcard/migrations/0017_alter_giftcard_code.py b/saleor/giftcard/migrations/0017_alter_giftcard_code.pynew file mode 100644index 0000000000..6c0069ac40--- /dev/null+++ b/saleor/giftcard/migrations/0017_alter_giftcard_code.py@@ -0,0 +1,24 @@+# Generated by Django 3.2.15 on 2023-01-04 14:38++import django.core.validators+from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ("giftcard", "0016_alter_giftcard_created_at"),+ ]++ operations = [+ migrations.AlterField(+ model_name="giftcard",+ name="code",+ field=models.CharField(+ db_index=True,+ max_length=16,+ unique=True,+ validators=[django.core.validators.MinLengthValidator(8)],+ ),+ ),+ ]diff --git a/saleor/giftcard/models.py b/saleor/giftcard/models.pyindex 9ebb221933..42a3d67d5c 100644--- a/saleor/giftcard/models.py+++ b/saleor/giftcard/models.py@@ -2,6 +2,7 @@ import osfrom django.conf import settingsfrom django.contrib.postgres.indexes import GinIndex+from django.core.validators import MinLengthValidatorfrom django.db import modelsfrom django.db.models import JSONField, Qfrom django.utils import timezone@@ -41,7 +42,9 @@ GiftCardManager = models.Manager.from_queryset(GiftCardQueryset)class GiftCard(ModelWithMetadata):- code = models.CharField(max_length=16, unique=True, db_index=True)+ code = models.CharField(+ max_length=16, unique=True, validators=[MinLengthValidator(8)], db_index=True+ )is_active = models.BooleanField(default=True)created_by = models.ForeignKey(settings.AUTH_USER_MODEL,diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.pyindex 724e38485b..0d153857d4 100644--- a/saleor/graphql/giftcard/mutations.py+++ b/saleor/graphql/giftcard/mutations.py@@ -8,7 +8,7 @@ from django.core.validators import validate_emailfrom ...account.models import Userfrom ...core.permissions import GiftcardPermissionsfrom ...core.tracing import traced_atomic_transaction-from ...core.utils.promo_code import generate_promo_code+from ...core.utils.promo_code import generate_promo_code, is_available_promo_codefrom ...core.utils.validators import is_date_in_futurefrom ...giftcard import events, modelsfrom ...giftcard.error_codes import GiftCardErrorCode@@ -132,7 +132,18 @@ class GiftCardCreate(ModelMutation):# perform only when gift card is createdif instance.pk is None:- cleaned_input["code"] = generate_promo_code()+ code = cleaned_input.get("code")+ if code and not is_available_promo_code(code):+ raise ValidationError(+ {+ "code": ValidationError(+ "Promo code already exists.",+ code=GiftCardErrorCode.ALREADY_EXISTS.value,+ )+ }+ )++ cleaned_input["code"] = code or generate_promo_code()cls.set_created_by_user(cleaned_input, info)cls.clean_expiry_date(cleaned_input, instance)diff --git a/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py b/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.pyindex cc7de90b4b..ac1af32c47 100644--- a/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py+++ b/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py@@ -693,3 +693,83 @@ def test_create_gift_card_trigger_webhook(gift_card,SimpleLazyObject(lambda: app_api_client.app),)+++def test_create_gift_card_with_code(+ staff_api_client,+ permission_manage_gift_card,+ permission_manage_users,+ permission_manage_apps,+):+ # given+ code = "custom-code"+ variables = {+ "input": {+ "balance": {+ "amount": 1,+ "currency": "USD",+ },+ "code": code,+ "isActive": True,+ }+ }++ # when+ response = staff_api_client.post_graphql(+ CREATE_GIFT_CARD_MUTATION,+ variables,+ permissions=[+ permission_manage_gift_card,+ permission_manage_users,+ permission_manage_apps,+ ],+ )++ # then+ content = get_graphql_content(response)+ errors = content["data"]["giftCardCreate"]["errors"]+ data = content["data"]["giftCardCreate"]["giftCard"]++ assert not errors+ assert data["code"] == code+++def test_create_gift_card_with_to_short_code(+ staff_api_client,+ permission_manage_gift_card,+ permission_manage_users,+ permission_manage_apps,+):+ # given+ code = "short"+ variables = {+ "input": {+ "balance": {+ "amount": 1,+ "currency": "USD",+ },+ "code": code,+ "isActive": True,+ }+ }++ # when+ response = staff_api_client.post_graphql(+ CREATE_GIFT_CARD_MUTATION,+ variables,+ permissions=[+ permission_manage_gift_card,+ permission_manage_users,+ permission_manage_apps,+ ],+ )++ # then+ content = get_graphql_content(response)+ errors = content["data"]["giftCardCreate"]["errors"]+ data = content["data"]["giftCardCreate"]["giftCard"]++ assert not data+ assert len(errors) == 1+ assert errors[0]["field"] == "code"+ assert errors[0]["code"] == "INVALID"