Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Block zero balance in gift cards amount
Tomasz Szymanskicde3de0851d8
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.pyindex 39fe8a3851..d9160743a2 100644--- a/saleor/graphql/giftcard/mutations.py+++ b/saleor/graphql/giftcard/mutations.py@@ -170,16 +170,36 @@ class GiftCardCreate(ModelMutation):@staticmethoddef clean_balance(cleaned_input, instance):balance = cleaned_input.pop("balance", None)- amount = balance["amount"]- currency = balance["currency"]- try:- validate_price_precision(amount, currency)- except ValidationError as error:- error.code = GiftCardErrorCode.INVALID.value- raise ValidationError({"balance": error})- cleaned_input["currency"] = currency- cleaned_input["current_balance_amount"] = amount- cleaned_input["initial_balance_amount"] = amount+ if balance:+ amount = balance["amount"]+ currency = balance["currency"]+ try:+ validate_price_precision(amount, currency)+ except ValidationError as error:+ error.code = GiftCardErrorCode.INVALID.value+ raise ValidationError({"balance": error})+ if instance.pk:+ if currency != instance.currency:+ raise ValidationError(+ {+ "balance": ValidationError(+ "Cannot change gift card currency.",+ code=GiftCardErrorCode.INVALID.value,+ )+ }+ )+ if not amount > 0:+ raise ValidationError(+ {+ "balance": ValidationError(+ "Balance amount have to be greater than 0.",+ code=GiftCardErrorCode.INVALID.value,+ )+ }+ )+ cleaned_input["currency"] = currency+ cleaned_input["current_balance_amount"] = amount+ cleaned_input["initial_balance_amount"] = amount@classmethoddef post_save_action(cls, info, instance, cleaned_input):diff --git a/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py b/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.pyindex 3de2c03230..4e55147859 100644--- a/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py+++ b/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py@@ -342,6 +342,52 @@ def test_create_gift_card_with_too_many_decimal_places_in_balance_amount(assert errors[0]["code"] == GiftCardErrorCode.INVALID.name+def test_create_gift_card_with_zero_balance_amount(+ staff_api_client,+ customer_user,+ permission_manage_gift_card,+ permission_manage_users,+ permission_manage_apps,+):+ # given+ currency = "USD"+ expiry_type = GiftCardExpiryTypeEnum.NEVER_EXPIRE.name+ tag = "gift-card-tag"+ variables = {+ "balance": {+ "amount": 0,+ "currency": currency,+ },+ "userEmail": customer_user.email,+ "tag": tag,+ "note": "This is gift card note that will be save in gift card event.",+ "expirySettings": {+ "expiryType": expiry_type,+ },+ }++ # 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"] == "balance"+ assert errors[0]["code"] == GiftCardErrorCode.INVALID.name++def test_create_gift_card_with_expiry_date(staff_api_client,customer_user,