Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Validate percentade discount value for voucher
Damian W6fe23644f8ac
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8e4e5b1c89..80ba5ef180 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -25,6 +25,7 @@ All notable, unreleased changes to this project will be documented in this file.- Raise an error when the user is trying to sort products by rank without search - #7013 by @IKarbowiak- Fix available shipping methods - return also weight methods without weight limits - #7021 by @IKarbowiak- Remove redundant Opentracing spans - #6994 by @fowczarek+- Validate discount value for percentage vouchers - #7033 by @d-wysocki### Breaking- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysockidiff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.pyindex a215095811..42f7f759c2 100644--- a/saleor/graphql/discount/mutations.py+++ b/saleor/graphql/discount/mutations.py@@ -296,7 +296,7 @@ class VoucherChannelListingInput(graphene.InputObjectType):)remove_channels = graphene.List(graphene.NonNull(graphene.ID),- description=("List of channels from which the voucher should be unassigned."),+ description="List of channels from which the voucher should be unassigned.",required=False,)@@ -318,14 +318,17 @@ class VoucherChannelListingUpdate(BaseChannelListingMutation):error_type_field = "discount_errors"@classmethod- def clean_prices(cls, cleaned_input, voucher, errors):+ def clean_discount_values_per_channel(cls, cleaned_input, voucher, error_lists):+ (+ channels_without_value,+ channels_with_invalid_value_precision,+ channels_with_invalid_percentage_value,+ channels_with_invalid_min_amount_spent_precision,+ ) = error_listschannel_slugs_assigned_to_voucher = voucher.channel_listings.values_list("channel__slug", flat=True)- is_fixed_value_type = voucher.discount_value_type == DiscountValueType.FIXED- channels_without_value = []- channels_with_invalid_value_precision = []- channels_with_invalid_min_amount_spent_precision = []+for cleaned_channel in cleaned_input.get("add_channels", []):channel = cleaned_channel.get("channel", None)if not channel:@@ -339,14 +342,18 @@ class VoucherChannelListingUpdate(BaseChannelListingMutation):if missing_required_value or discount_value is None:channels_without_value.append(cleaned_channel["channel_id"])# Validate value precision if it is fixed amount voucher- if discount_value and is_fixed_value_type:+ if voucher.discount_value_type == DiscountValueType.FIXED:try:validate_price_precision(discount_value, channel.currency_code)except ValidationError:channels_with_invalid_value_precision.append(cleaned_channel["channel_id"])- if discount_value:+ if voucher.discount_value_type == DiscountValueType.PERCENTAGE:+ if discount_value > 100:+ channels_with_invalid_percentage_value.append(+ cleaned_channel["channel_id"]+ )cleaned_channel["discount_value"] = discount_valuemin_amount_spent = cleaned_channel.get("min_amount_spent", None)@@ -358,6 +365,24 @@ class VoucherChannelListingUpdate(BaseChannelListingMutation):cleaned_channel["channel_id"])+ @classmethod+ def clean_discount_values(cls, cleaned_input, voucher, errors):+ channels_without_value = []+ channels_with_invalid_value_precision = []+ channels_with_invalid_percentage_value = []+ channels_with_invalid_min_amount_spent_precision = []+ error_lists = (+ channels_without_value,+ channels_with_invalid_value_precision,+ channels_with_invalid_percentage_value,+ channels_with_invalid_min_amount_spent_precision,+ )+ cls.clean_discount_values_per_channel(+ cleaned_input,+ voucher,+ error_lists,+ )+if channels_without_value:errors["discount_value"].append(ValidationError(@@ -375,6 +400,14 @@ class VoucherChannelListingUpdate(BaseChannelListingMutation):params={"channels": channels_with_invalid_value_precision},))+ if channels_with_invalid_percentage_value:+ errors["discount_value"].append(+ ValidationError(+ "Invalid percentage value.",+ code=DiscountErrorCode.INVALID.value,+ params={"channels": channels_with_invalid_percentage_value},+ )+ )if channels_with_invalid_min_amount_spent_precision:errors["min_amount_spent"].append(ValidationError(@@ -419,7 +452,7 @@ class VoucherChannelListingUpdate(BaseChannelListingMutation):cleaned_input = cls.clean_channels(info, input, errors, DiscountErrorCode.DUPLICATED_INPUT_ITEM.value)- cleaned_input = cls.clean_prices(cleaned_input, voucher, errors)+ cleaned_input = cls.clean_discount_values(cleaned_input, voucher, errors)if errors:raise ValidationError(errors)diff --git a/saleor/graphql/discount/tests/test_voucher_channel_listing.py b/saleor/graphql/discount/tests/test_voucher_channel_listing.pyindex f02f24f939..0ce8d43315 100644--- a/saleor/graphql/discount/tests/test_voucher_channel_listing.py+++ b/saleor/graphql/discount/tests/test_voucher_channel_listing.py@@ -1,5 +1,6 @@import graphene+from ....discount import DiscountValueTypefrom ....discount.error_codes import DiscountErrorCodefrom ...tests.utils import assert_no_permission, get_graphql_content@@ -429,6 +430,46 @@ def test_voucher_channel_listing_create_with_null_as_discount_value(assert errors[0]["channels"] == [channel_id]+def test_voucher_channel_listing_create_with_invalid_percentage_value(+ staff_api_client,+ voucher_without_channel,+ permission_manage_discounts,+ channel_USD,+):+ # given+ voucher = voucher_without_channel+ voucher.discount_value_type = DiscountValueType.PERCENTAGE+ voucher.save()+ voucher_id = graphene.Node.to_global_id("Voucher", voucher.pk)+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)+ variables = {+ "id": voucher_id,+ "input": {+ "addChannels": [+ {+ "channelId": channel_id,+ "discountValue": 101,+ }+ ]+ },+ }++ # when+ response = staff_api_client.post_graphql(+ VOUCHER_CHANNEL_LISTING_UPDATE_MUTATION,+ variables=variables,+ permissions=(permission_manage_discounts,),+ )+ content = get_graphql_content(response)++ # then+ errors = content["data"]["voucherChannelListingUpdate"]["discountErrors"]+ assert len(errors) == 1+ assert errors[0]["field"] == "discountValue"+ assert errors[0]["code"] == DiscountErrorCode.INVALID.name+ assert errors[0]["channels"] == [channel_id]++def test_voucher_channel_listing_create_without_discount_value(staff_api_client,voucher_without_channel,