Skip to content

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

Add sort by to vouchers query

fowczarekac3a7cab2dc1
diff --git a/saleor/graphql/discount/resolvers.py b/saleor/graphql/discount/resolvers.py
index a4ae732e49..5a3741f1e0 100644
--- a/saleor/graphql/discount/resolvers.py
+++ b/saleor/graphql/discount/resolvers.py
@@ -2,15 +2,16 @@ import graphene_django_optimizer as gql_optimizer
from ...discount import models
from ..utils import filter_by_query_param, sort_queryset
-from .sorters import SaleSortField
+from .sorters import SaleSortField, VoucherSortField
VOUCHER_SEARCH_FIELDS = ("name", "code")
SALE_SEARCH_FIELDS = ("name", "value", "type")
-def resolve_vouchers(info, query):
+def resolve_vouchers(info, query, sort_by=None, **_kwargs):
qs = models.Voucher.objects.all()
qs = filter_by_query_param(qs, query, VOUCHER_SEARCH_FIELDS)
+ qs = sort_queryset(qs, sort_by, VoucherSortField)
return gql_optimizer.query(qs, info)
diff --git a/saleor/graphql/discount/schema.py b/saleor/graphql/discount/schema.py
index 9b87d0731b..af5bed7576 100644
--- a/saleor/graphql/discount/schema.py
+++ b/saleor/graphql/discount/schema.py
@@ -19,7 +19,7 @@ from .mutations import (
VoucherUpdate,
)
from .resolvers import resolve_sales, resolve_vouchers
-from .sorters import SaleSortingInput
+from .sorters import SaleSortingInput, VoucherSortingInput
from .types import Sale, Voucher
@@ -56,6 +56,7 @@ class DiscountQueries(graphene.ObjectType):
vouchers = FilterInputConnectionField(
Voucher,
filter=VoucherFilterInput(description="Filtering options for vouchers."),
+ sort_by=VoucherSortingInput(description="Sort voucher."),
query=graphene.String(description="Search vouchers by name or code."),
description="List of the shop's vouchers.",
)
@@ -74,7 +75,7 @@ class DiscountQueries(graphene.ObjectType):
@permission_required("discount.manage_discounts")
def resolve_vouchers(self, info, query=None, **_kwargs):
- return resolve_vouchers(info, query)
+ return resolve_vouchers(info, query, **_kwargs)
class DiscountMutations(graphene.ObjectType):
diff --git a/saleor/graphql/discount/sorters.py b/saleor/graphql/discount/sorters.py
index 9f164f5972..cc8015b529 100644
--- a/saleor/graphql/discount/sorters.py
+++ b/saleor/graphql/discount/sorters.py
@@ -29,3 +29,33 @@ class SaleSortingInput(SortInputObjectType):
class Meta:
sort_enum = SaleSortField
type_name = "sales"
+
+
+class VoucherSortField(graphene.Enum):
+ NAME = "name"
+ START_DATE = "start_date"
+ END_DATE = "end_date"
+ VALUE = "discount_value"
+ TYPE = "type"
+ USAGE_LIMIT = "usage_limit"
+
+ @property
+ def description(self):
+ # pylint: disable=no-member
+ if self in [
+ VoucherSortField.NAME,
+ VoucherSortField.START_DATE,
+ VoucherSortField.END_DATE,
+ VoucherSortField.VALUE,
+ VoucherSortField.TYPE,
+ VoucherSortField.USAGE_LIMIT,
+ ]:
+ sort_name = self.name.lower().replace("_", " ")
+ return f"Sort vouchers by {sort_name}."
+ raise ValueError("Unsupported enum value: %s" % self.value)
+
+
+class VoucherSortingInput(SortInputObjectType):
+ class Meta:
+ sort_enum = VoucherSortField
+ type_name = "vouchers"
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index fec1f77382..4740f025c9 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3472,7 +3472,7 @@ type Query {
sale(id: ID!): Sale
sales(filter: SaleFilterInput, sortBy: SaleSortingInput, query: String, before: String, after: String, first: Int, last: Int): SaleCountableConnection
voucher(id: ID!): Voucher
- vouchers(filter: VoucherFilterInput, query: String, before: String, after: String, first: Int, last: Int): VoucherCountableConnection
+ vouchers(filter: VoucherFilterInput, sortBy: VoucherSortingInput, query: String, before: String, after: String, first: Int, last: Int): VoucherCountableConnection
taxTypes: [TaxType]
checkout(token: UUID): Checkout
checkouts(before: String, after: String, first: Int, last: Int): CheckoutCountableConnection
@@ -4422,6 +4422,20 @@ type VoucherRemoveCatalogues {
voucher: Voucher
}
+enum VoucherSortField {
+ NAME
+ START_DATE
+ END_DATE
+ VALUE
+ TYPE
+ USAGE_LIMIT
+}
+
+input VoucherSortingInput {
+ direction: OrderDirection!
+ field: VoucherSortField
+}
+
type VoucherTranslatableContent implements Node {
id: ID!
name: String
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index 742afd50cd..b47446d590 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -959,6 +959,116 @@ def test_query_vouchers_with_filter_search(
assert len(data) == count
+QUERY_VOUCHER_WITH_SORT = """
+ query ($sort_by: VoucherSortingInput!) {
+ vouchers(first:5, sortBy: $sort_by) {
+ edges{
+ node{
+ name
+ }
+ }
+ }
+ }
+"""
+
+
+ "voucher_sort, result_order",
+ [
+ (
+ {"field": "NAME", "direction": "ASC"},
+ ["FreeShipping", "Voucher1", "Voucher2"],
+ ),
+ (
+ {"field": "NAME", "direction": "DESC"},
+ ["Voucher2", "Voucher1", "FreeShipping"],
+ ),
+ (
+ {"field": "VALUE", "direction": "ASC"},
+ ["Voucher2", "FreeShipping", "Voucher1"],
+ ),
+ (
+ {"field": "VALUE", "direction": "DESC"},
+ ["Voucher1", "FreeShipping", "Voucher2"],
+ ),
+ (
+ {"field": "TYPE", "direction": "ASC"},
+ ["Voucher1", "Voucher2", "FreeShipping"],
+ ),
+ (
+ {"field": "TYPE", "direction": "DESC"},
+ ["FreeShipping", "Voucher1", "Voucher2"],
+ ),
+ (
+ {"field": "START_DATE", "direction": "ASC"},
+ ["FreeShipping", "Voucher2", "Voucher1"],
+ ),
+ (
+ {"field": "START_DATE", "direction": "DESC"},
+ ["Voucher1", "Voucher2", "FreeShipping"],
+ ),
+ (
+ {"field": "END_DATE", "direction": "ASC"},
+ ["Voucher2", "FreeShipping", "Voucher1"],
+ ),
+ (
+ {"field": "END_DATE", "direction": "DESC"},
+ ["Voucher1", "FreeShipping", "Voucher2"],
+ ),
+ (
+ {"field": "USAGE_LIMIT", "direction": "ASC"},
+ ["Voucher1", "FreeShipping", "Voucher2"],
+ ),
+ (
+ {"field": "USAGE_LIMIT", "direction": "DESC"},
+ ["Voucher2", "FreeShipping", "Voucher1"],
+ ),
+ ],
+)
+def test_query_vouchers_with_sort(
+ voucher_sort, result_order, staff_api_client, permission_manage_discounts
+):
+ Voucher.objects.bulk_create(
+ [
+ Voucher(
+ name="Voucher1",
+ discount_value=123,
+ code="abc",
+ discount_value_type=DiscountValueType.FIXED,
+ type=VoucherType.ENTIRE_ORDER,
+ usage_limit=10,
+ ),
+ Voucher(
+ name="Voucher2",
+ discount_value=23,
+ code="123",
+ discount_value_type=DiscountValueType.FIXED,
+ type=VoucherType.ENTIRE_ORDER,
+ start_date=timezone.now().replace(year=2012, month=1, day=5),
+ end_date=timezone.now().replace(year=2013, month=1, day=5),
+ ),
+ Voucher(
+ name="FreeShipping",
+ discount_value=100,
+ code="xyz",
+ discount_value_type=DiscountValueType.PERCENTAGE,
+ type=VoucherType.SHIPPING,
+ start_date=timezone.now().replace(year=2011, month=1, day=5),
+ end_date=timezone.now().replace(year=2015, month=12, day=31),
+ usage_limit=1000,
+ ),
+ ]
+ )
+ variables = {"sort_by": voucher_sort}
+ staff_api_client.user.user_permissions.add(permission_manage_discounts)
+ response = staff_api_client.post_graphql(QUERY_VOUCHER_WITH_SORT, variables)
+ content = get_graphql_content(response)
+ vouchers = content["data"]["vouchers"]["edges"]
+
+ for order, voucher_name in enumerate(result_order):
+ assert vouchers[order]["node"]["name"] == voucher_name
+
+
@pytest.mark.parametrize(
"sale_filter, start_date, end_date, count",
[
@@ -1119,13 +1229,13 @@ def test_query_sales_with_filter_search(
QUERY_SALE_WITH_SORT = """
query ($sort_by: SaleSortingInput!) {
sales(first:5, sortBy: $sort_by) {
- edges{
- node{
- name
- }
+ edges{
+ node{
+ name
}
}
}
+ }
"""
@@ -1144,7 +1254,7 @@ QUERY_SALE_WITH_SORT = """
({"field": "END_DATE", "direction": "DESC"}, ["BigSale", "Sale3", "Sale2"]),
],
)
-def test_query_staff_members_with_sort(
+def test_query_sales_with_sort(
sale_sort, result_order, staff_api_client, permission_manage_discounts
):
Sale.objects.bulk_create(