Skip to content

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

Add possibility to filter paymentes by list of ids. (#10821)

Krzysztof Waliczek959e39f8ec9d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 487e34f90d..afc612443e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add ability to filter by slug. #10578 by @kadewu
- Affected types: Attribute, Category, Collection, Menu, Page, Product, ProductType, Warehouse
- Deprecated `slug` in filter for `menus`. Use `slugs` instead
+- Add ability to filter payments by list of ids. #10821 by @kadewu
- Add ability to filter customers by ids. #10694 by @kadewu
### Other changes
diff --git a/saleor/graphql/payment/filters.py b/saleor/graphql/payment/filters.py
index e3b6bbeb13..5256c9dcca 100644
--- a/saleor/graphql/payment/filters.py
+++ b/saleor/graphql/payment/filters.py
@@ -1,11 +1,15 @@
import django_filters
+from ..core.descriptions import ADDED_IN_38
from ..core.filters import GlobalIDMultipleChoiceFilter
from ..core.types import FilterInputObjectType
from .types import Payment
class PaymentFilter(django_filters.FilterSet):
+ ids = GlobalIDMultipleChoiceFilter(
+ field_name="id", help_text=f"Filter by ids. {ADDED_IN_38}"
+ )
checkouts = GlobalIDMultipleChoiceFilter(field_name="checkout")
class Meta:
diff --git a/saleor/graphql/payment/tests/queries/test_payments_filter.py b/saleor/graphql/payment/tests/queries/test_payments_filter.py
index 5c04fd92d0..4d1d1233f9 100644
--- a/saleor/graphql/payment/tests/queries/test_payments_filter.py
+++ b/saleor/graphql/payment/tests/queries/test_payments_filter.py
@@ -74,3 +74,100 @@ def test_query_payments_filter_by_checkout(
graphene.Node.to_global_id("Payment", payment.pk)
for payment in [payment2, payment3]
}
+
+
+def test_query_payments_filter_by_one_id(
+ payments_dummy, permission_manage_orders, staff_api_client
+):
+ # given
+ search_payment = payments_dummy[0]
+
+ variables = {
+ "filter": {
+ "ids": [graphene.Node.to_global_id("Payment", search_payment.pk)],
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ PAYMENT_QUERY, variables, permissions=[permission_manage_orders]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ result_payments = content["data"]["payments"]["edges"]
+
+ assert len(result_payments) == 1
+ _, id = graphene.Node.from_global_id(result_payments[0]["node"]["id"])
+ assert id == str(search_payment.pk)
+
+
+def test_query_payments_filter_by_multiple_ids(
+ payments_dummy, permission_manage_orders, staff_api_client
+):
+ # given
+ search_payments = payments_dummy[:2]
+ search_payments_ids = [
+ graphene.Node.to_global_id("Payment", payment.pk) for payment in search_payments
+ ]
+
+ variables = {"filter": {"ids": search_payments_ids}}
+
+ # when
+ response = staff_api_client.post_graphql(
+ PAYMENT_QUERY, variables, permissions=[permission_manage_orders]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ expected_ids = [str(payment.pk) for payment in search_payments]
+
+ result_payments = content["data"]["payments"]["edges"]
+
+ assert len(result_payments) == len(search_payments)
+ for result_payment in result_payments:
+ _, id = graphene.Node.from_global_id(result_payment["node"]["id"])
+ assert id in expected_ids
+
+
+def test_query_payments_filter_by_empty_id_list(
+ payments_dummy, permission_manage_orders, staff_api_client
+):
+ # given
+ variables = {"filter": {"ids": []}}
+
+ # when
+ response = staff_api_client.post_graphql(
+ PAYMENT_QUERY, variables, permissions=[permission_manage_orders]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ expected_ids = [str(payment.pk) for payment in payments_dummy]
+
+ result_payments = content["data"]["payments"]["edges"]
+
+ assert len(result_payments) == len(payments_dummy)
+ for result_payment in result_payments:
+ _, id = graphene.Node.from_global_id(result_payment["node"]["id"])
+ assert id in expected_ids
+
+
+def test_query_payments_filter_by_not_existing_id(
+ payments_dummy, permission_manage_orders, staff_api_client
+):
+ # given
+ search_pk = max([payment.pk for payment in payments_dummy]) + 1
+ search_id = graphene.Node.to_global_id("Payment", search_pk)
+ variables = {"filter": {"ids": [search_id]}}
+
+ # when
+ response = staff_api_client.post_graphql(
+ PAYMENT_QUERY, variables, permissions=[permission_manage_orders]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ result_payments = content["data"]["payments"]["edges"]
+
+ assert len(result_payments) == 0
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 1df6b74857..1cebc7f2c9 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -9879,6 +9879,12 @@ type PaymentCountableEdge {
}
input PaymentFilterInput {
+ """
+ Filter by ids.
+
+ Added in Saleor 3.8.
+ """
+ ids: [ID!]
checkouts: [ID!]
}
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 8af2fc1323..65464cc3e4 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -5212,6 +5212,37 @@ def payment_dummy(db, order_with_lines):
)
+def payments_dummy(order_with_lines):
+ return Payment.objects.bulk_create(
+ [
+ Payment(
+ gateway="mirumee.payments.dummy",
+ order=order_with_lines,
+ is_active=True,
+ cc_first_digits="4111",
+ cc_last_digits="1111",
+ cc_brand="visa",
+ cc_exp_month=12,
+ cc_exp_year=2027,
+ total=order_with_lines.total.gross.amount,
+ currency=order_with_lines.currency,
+ billing_first_name=order_with_lines.billing_address.first_name,
+ billing_last_name=order_with_lines.billing_address.last_name,
+ billing_company_name=order_with_lines.billing_address.company_name,
+ billing_address_1=order_with_lines.billing_address.street_address_1,
+ billing_address_2=order_with_lines.billing_address.street_address_2,
+ billing_city=order_with_lines.billing_address.city,
+ billing_postal_code=order_with_lines.billing_address.postal_code,
+ billing_country_code=order_with_lines.billing_address.country.code,
+ billing_country_area=order_with_lines.billing_address.country_area,
+ billing_email=order_with_lines.user_email,
+ )
+ for _ in range(3)
+ ]
+ )
+
+
@pytest.fixture
def payment(payment_dummy, payment_app):
gateway_id = "credit-card"