Skip to content

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

Fix unserializable decimal values in catalogue predicate. (#14616)

Piotr Zabieglik13db894721de
diff --git a/saleor/discount/migrations/0064_alter_promotionrule_catalogue_predicate.py b/saleor/discount/migrations/0064_alter_promotionrule_catalogue_predicate.py
new file mode 100644
index 0000000000..932aa27232
--- /dev/null
+++ b/saleor/discount/migrations/0064_alter_promotionrule_catalogue_predicate.py
@@ -0,0 +1,21 @@
+from django.db import migrations, models
+
+import saleor.core.utils.json_serializer
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("discount", "0063_basediscount_voucher_code"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="promotionrule",
+ name="catalogue_predicate",
+ field=models.JSONField(
+ blank=True,
+ default=dict,
+ encoder=saleor.core.utils.json_serializer.CustomJsonEncoder,
+ ),
+ ),
+ ]
diff --git a/saleor/discount/migrations/0068_merge_20231106_1013.py b/saleor/discount/migrations/0068_merge_20231106_1013.py
new file mode 100644
index 0000000000..1bed9d5a40
--- /dev/null
+++ b/saleor/discount/migrations/0068_merge_20231106_1013.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.22 on 2023-11-06 10:13
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("discount", "0064_alter_promotionrule_catalogue_predicate"),
+ ("discount", "0067_voucher_single_use"),
+ ]
+
+ operations = []
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index 66f087b530..69ace9d877 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -359,7 +359,9 @@ class PromotionRule(models.Model):
Promotion, on_delete=models.CASCADE, related_name="rules"
)
channels = models.ManyToManyField(Channel)
- catalogue_predicate = models.JSONField(blank=True, default=dict)
+ catalogue_predicate = models.JSONField(
+ blank=True, default=dict, encoder=CustomJsonEncoder
+ )
reward_value_type = models.CharField(
max_length=255, choices=RewardValueType.CHOICES, blank=True, null=True
)
diff --git a/saleor/graphql/discount/tests/mutations/test_promotion_rule_create.py b/saleor/graphql/discount/tests/mutations/test_promotion_rule_create.py
index c89ee7bc3b..61117f4c50 100644
--- a/saleor/graphql/discount/tests/mutations/test_promotion_rule_create.py
+++ b/saleor/graphql/discount/tests/mutations/test_promotion_rule_create.py
@@ -907,3 +907,42 @@ def test_promotion_rule_create_events(
assert PromotionEvents.RULE_CREATED.upper() == events[0]["type"]
assert events[0]["ruleId"] == data["promotionRule"]["id"]
+
+
+@patch("saleor.plugins.manager.PluginsManager.promotion_rule_created")
+@patch(
+ "saleor.product.tasks.update_products_discounted_prices_for_promotion_task.delay"
+)
+def test_promotion_rule_create_serializable_decimal_in_predicate(
+ update_products_discounted_prices_for_promotion_task_mock,
+ promotion_rule_created_mock,
+ staff_api_client,
+ permission_group_manage_discounts,
+ promotion,
+):
+ # given
+ permission_group_manage_discounts.user_set.add(staff_api_client.user)
+ catalogue_predicate = {
+ "productPredicate": {"minimalPrice": {"range": {"gte": "25"}}}
+ }
+ reward_value = Decimal("10")
+ reward_value_type = RewardValueTypeEnum.PERCENTAGE.name
+ promotion_id = graphene.Node.to_global_id("Promotion", promotion.id)
+
+ variables = {
+ "input": {
+ "promotion": promotion_id,
+ "rewardValueType": reward_value_type,
+ "rewardValue": reward_value,
+ "cataloguePredicate": catalogue_predicate,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(PROMOTION_RULE_CREATE_MUTATION, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["promotionRuleCreate"]
+ assert not data["errors"]
+ assert data["promotionRule"]["cataloguePredicate"] == catalogue_predicate