Skip to content

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

Add filter through channels in orders query (#6633)

Filip Owczarekf6165d77e0a4
diff --git a/saleor/graphql/order/filters.py b/saleor/graphql/order/filters.py
index b598ddae40..bc9ff49fdf 100644
--- a/saleor/graphql/order/filters.py
+++ b/saleor/graphql/order/filters.py
@@ -1,11 +1,14 @@
import django_filters
from django.db.models import Sum
+from graphene_django.filter import GlobalIDMultipleChoiceFilter
from ...order.models import Order
+from ..channel.types import Channel
from ..core.filters import ListObjectTypeFilter, ObjectTypeFilter
from ..core.types.common import DateRangeInput
from ..core.utils import from_global_id_strict_type
from ..payment.enums import PaymentChargeStatusEnum
+from ..utils import resolve_global_ids_to_primary_keys
from ..utils.filters import filter_by_query_param, filter_range_field
from .enums import OrderStatusFilter
@@ -82,10 +85,18 @@ def filter_order_search(qs, _, value):
return qs
+def filter_channels(qs, _, values):
+ if values:
+ _, channels_ids = resolve_global_ids_to_primary_keys(values, Channel)
+ qs = qs.filter(channel_id__in=channels_ids)
+ return qs
+
+
class DraftOrderFilter(django_filters.FilterSet):
customer = django_filters.CharFilter(method=filter_customer)
created = ObjectTypeFilter(input_class=DateRangeInput, method=filter_created_range)
search = django_filters.CharFilter(method=filter_order_search)
+ channels = GlobalIDMultipleChoiceFilter(method=filter_channels)
class Meta:
model = Order
@@ -100,6 +111,7 @@ class OrderFilter(DraftOrderFilter):
customer = django_filters.CharFilter(method=filter_customer)
created = ObjectTypeFilter(input_class=DateRangeInput, method=filter_created_range)
search = django_filters.CharFilter(method=filter_order_search)
+ channels = GlobalIDMultipleChoiceFilter(method=filter_channels)
class Meta:
model = Order
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 9ea8b8bab9..ae8de39f8c 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -4094,6 +4094,94 @@ def test_order_bulk_cancel_as_app(
assert mock_cancel_order.call_count == expected_count
+def test_order_query_with_filter_channels_with_one_channel(
+ orders_query_with_filter,
+ staff_api_client,
+ permission_manage_orders,
+ orders,
+ channel_USD,
+):
+ # given
+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.pk)
+ variables = {"filter": {"channels": [channel_id]}}
+
+ # when
+ response = staff_api_client.post_graphql(
+ orders_query_with_filter, variables, permissions=(permission_manage_orders,)
+ )
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["orders"]["edges"]
+ assert len(orders) == 3
+
+
+def test_order_query_with_filter_channels_without_channel(
+ orders_query_with_filter, staff_api_client, permission_manage_orders, orders,
+):
+ # given
+ variables = {"filter": {"channels": []}}
+
+ # when
+ response = staff_api_client.post_graphql(
+ orders_query_with_filter, variables, permissions=(permission_manage_orders,)
+ )
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["orders"]["edges"]
+ assert len(orders) == 5
+
+
+def test_order_query_with_filter_channels_with_many_channel(
+ orders_query_with_filter,
+ staff_api_client,
+ permission_manage_orders,
+ orders,
+ channel_USD,
+ channel_PLN,
+ other_channel_USD,
+):
+ # given
+ Order.objects.create(channel=other_channel_USD)
+ channel_usd_id = graphene.Node.to_global_id("Channel", channel_USD.pk)
+ channel_pln_id = graphene.Node.to_global_id("Channel", channel_PLN.pk)
+ variables = {"filter": {"channels": [channel_pln_id, channel_usd_id]}}
+
+ # when
+ response = staff_api_client.post_graphql(
+ orders_query_with_filter, variables, permissions=(permission_manage_orders,)
+ )
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["orders"]["edges"]
+ assert len(orders) == 5
+ assert Order.objects.non_draft().count() == 6
+
+
+def test_order_query_with_filter_channels_with_empty_channel(
+ orders_query_with_filter,
+ staff_api_client,
+ permission_manage_orders,
+ orders,
+ other_channel_USD,
+):
+ # given
+ channel_id = graphene.Node.to_global_id("Channel", other_channel_USD.pk)
+ variables = {"filter": {"channels": [channel_id]}}
+
+ # when
+ response = staff_api_client.post_graphql(
+ orders_query_with_filter, variables, permissions=(permission_manage_orders,)
+ )
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["orders"]["edges"]
+ assert len(orders) == 0
+
+
@pytest.mark.parametrize(
"orders_filter, count",
[
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 547540fee6..fe10cdb2e6 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2940,6 +2940,7 @@ input OrderDraftFilterInput {
customer: String
created: DateRangeInput
search: String
+ channels: [ID]
}
type OrderError {
@@ -3073,6 +3074,7 @@ input OrderFilterInput {
customer: String
created: DateRangeInput
search: String
+ channels: [ID]
}
type OrderFulfill {