Skip to content

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

Fix creating promotion rule without catalogue predicate (#15287)

SzymJbe430445a2c0
diff --git a/saleor/graphql/discount/mutations/promotion/validators.py b/saleor/graphql/discount/mutations/promotion/validators.py
index 75d71da11a..0292f5b63d 100644
--- a/saleor/graphql/discount/mutations/promotion/validators.py
+++ b/saleor/graphql/discount/mutations/promotion/validators.py
@@ -117,6 +117,9 @@ def clean_predicate(predicate, error_class, index=None):
Operators cannot be mixed with other filter inputs. There could be only
one operator on each level.
"""
+ if not predicate:
+ return {}
+
if isinstance(predicate, list):
return [
clean_predicate(item, error_class, index)
diff --git a/saleor/graphql/discount/tests/mutations/test_promotion_create.py b/saleor/graphql/discount/tests/mutations/test_promotion_create.py
index 538f3b4479..844912458b 100644
--- a/saleor/graphql/discount/tests/mutations/test_promotion_create.py
+++ b/saleor/graphql/discount/tests/mutations/test_promotion_create.py
@@ -1408,3 +1408,53 @@ def test_promotion_create_events_by_app(
rule_ids = [event["ruleId"] for event in events if event.get("ruleId")]
rules = data["promotion"]["rules"]
assert all([rule["id"] in rule_ids for rule in rules])
+
+
+def test_promotion_create_without_catalogue_predicate(
+ staff_api_client,
+ permission_manage_discounts,
+ description_json,
+ channel_USD,
+ channel_PLN,
+ variant,
+ product,
+):
+ # given
+ start_date = timezone.now() - timedelta(days=30)
+ end_date = timezone.now() + timedelta(days=30)
+ channel_ids = [
+ graphene.Node.to_global_id("Channel", channel.pk)
+ for channel in [channel_USD, channel_PLN]
+ ]
+ promotion_name = "test promotion"
+ variables = {
+ "input": {
+ "name": promotion_name,
+ "description": description_json,
+ "startDate": start_date.isoformat(),
+ "endDate": end_date.isoformat(),
+ "rules": [
+ {
+ "name": "test promotion rule",
+ "description": description_json,
+ "channels": channel_ids,
+ "rewardValueType": RewardValueTypeEnum.PERCENTAGE.name,
+ "rewardValue": Decimal("50"),
+ "cataloguePredicate": None,
+ }
+ ],
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ PROMOTION_CREATE_MUTATION, variables, permissions=(permission_manage_discounts,)
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["promotionCreate"]
+ assert not data["errors"]
+ assert data["promotion"]
+ assert len(data["promotion"]["rules"]) == 1
+ assert data["promotion"]["rules"][0]["cataloguePredicate"] == {}