Skip to content

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

Add filter by ids for vouchers (#10157)

Jakubca00fb2955d1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5783f893d9..cd21a3f575 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Drop `create_thumbnails` command
### Other changes
+- Add `VoucherFilter.ids` filter - #10157 by @Jakubkuc
- Allow values of different attributes to share the same slug - #10138 by @IKarbowiak
- Fix inconsistent beat scheduling and compatibility with db scheduler - #10185 by @NyanKiyoshi<br/>
This fixes the following bugs:
@@ -25,6 +26,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Database backend: `celery --app saleor.celeryconf:app beat --scheduler saleor.schedulers.schedulers.DatabaseScheduler`
- Shelve backend: `celery --app saleor.celeryconf:app beat --scheduler saleor.schedulers.schedulers.PersistentScheduler`
+
# 3.5.0
### GraphQL API
diff --git a/saleor/graphql/discount/filters.py b/saleor/graphql/discount/filters.py
index 3493acec8e..8e46ff4e1b 100644
--- a/saleor/graphql/discount/filters.py
+++ b/saleor/graphql/discount/filters.py
@@ -7,9 +7,14 @@ from django.utils import timezone
from ...discount import DiscountValueType
from ...discount.models import Sale, SaleChannelListing, Voucher, VoucherQueryset
-from ..core.filters import ListObjectTypeFilter, MetadataFilterBase, ObjectTypeFilter
+from ..core.filters import (
+ GlobalIDMultipleChoiceFilter,
+ ListObjectTypeFilter,
+ MetadataFilterBase,
+ ObjectTypeFilter,
+)
from ..core.types import DateTimeRangeInput, IntRangeInput
-from ..utils.filters import filter_range_field
+from ..utils.filters import filter_by_id, filter_range_field
from .enums import DiscountStatusEnum, DiscountValueTypeEnum, VoucherDiscountType
@@ -90,6 +95,7 @@ class VoucherFilter(MetadataFilterBase):
)
started = ObjectTypeFilter(input_class=DateTimeRangeInput, method=filter_started)
search = django_filters.CharFilter(method=filter_voucher_search)
+ ids = GlobalIDMultipleChoiceFilter(method=filter_by_id("Voucher"))
class Meta:
model = Voucher
diff --git a/saleor/graphql/discount/tests/test_voucher_sorting_with_channels.py b/saleor/graphql/discount/tests/test_voucher_sorting_with_channels.py
index 11ccd666a6..86bc7b7ea0 100644
--- a/saleor/graphql/discount/tests/test_voucher_sorting_with_channels.py
+++ b/saleor/graphql/discount/tests/test_voucher_sorting_with_channels.py
@@ -1,3 +1,4 @@
+import graphene
import pytest
from ....discount import DiscountValueType, VoucherType
@@ -117,6 +118,7 @@ QUERY_VOUCHERS_WITH_SORTING_AND_FILTERING = """
edges {
node {
name
+ id
}
}
}
@@ -273,3 +275,72 @@ def test_vouchers_with_sorting_and_not_existing_channel_asc(
# then
content = get_graphql_content(response)
assert not content["data"]["vouchers"]["edges"]
+
+
+ "filter_by, vouchers_count",
+ [
+ ({"status": "ACTIVE"}, 4),
+ ({"status": "SCHEDULED"}, 0),
+ ({"status": "EXPIRED"}, 0),
+ ({"timesUsed": {"gte": 11}}, 1),
+ ({"timesUsed": {"lte": 1}}, 2),
+ ({"discountType": "PERCENTAGE"}, 3),
+ ({"discountType": "FIXED"}, 1),
+ ({"discountType": "SHIPPING"}, 0),
+ ({"search": "Code"}, 4),
+ ],
+)
+def test_vouchers_with_filter_and_channel_USD(
+ filter_by,
+ vouchers_count,
+ staff_api_client,
+ permission_manage_discounts,
+ vouchers_for_sorting_with_channels,
+ channel_USD,
+):
+ # given
+ variables = {"filter": filter_by, "channel": channel_USD.slug}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_VOUCHERS_WITH_SORTING_AND_FILTERING,
+ variables,
+ permissions=[permission_manage_discounts],
+ check_no_permissions=False,
+ )
+
+ # then
+ content = get_graphql_content(response)
+ vouchers_nodes = content["data"]["vouchers"]["edges"]
+ assert len(vouchers_nodes) == vouchers_count
+
+
+def test_vouchers_with_filter_by_ids_and_channel_USD(
+ staff_api_client,
+ permission_manage_discounts,
+ vouchers_for_sorting_with_channels,
+ channel_USD,
+):
+ # given
+ vouchers = [
+ vouchers_for_sorting_with_channels[0],
+ vouchers_for_sorting_with_channels[1],
+ ]
+ ids = [graphene.Node.to_global_id("Voucher", voucher.id) for voucher in vouchers]
+ variables = {"filter": {"ids": ids}, "channel": channel_USD.slug}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_VOUCHERS_WITH_SORTING_AND_FILTERING,
+ variables,
+ permissions=[permission_manage_discounts],
+ check_no_permissions=False,
+ )
+
+ # then
+ content = get_graphql_content(response)
+ vouchers_nodes = content["data"]["vouchers"]["edges"]
+ assert len(vouchers_nodes) == len(vouchers)
+ for voucher in vouchers_nodes:
+ assert voucher["node"]["id"] in ids
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 4fb8788932..a180f1ea6f 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -10093,6 +10093,7 @@ input VoucherFilterInput {
started: DateTimeRangeInput
search: String
metadata: [MetadataFilter!]
+ ids: [ID!]
}
enum VoucherDiscountType {