Skip to content

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

Adjust order transactions filter (#17976)

Iga Karbowiak1bc961307246
diff --git a/saleor/graphql/order/filters.py b/saleor/graphql/order/filters.py
index 5f10d4b863..da5527697a 100644
--- a/saleor/graphql/order/filters.py
+++ b/saleor/graphql/order/filters.py
@@ -21,16 +21,20 @@ from ..account.filters import AddressFilterInput, filter_address
from ..channel.filters import get_currency_from_filter_data
from ..core.doc_category import DOC_CATEGORY_ORDERS
from ..core.filters import (
- BooleanWhereFilter,
GlobalIDMultipleChoiceFilter,
- GlobalIDMultipleChoiceWhereFilter,
ListObjectTypeFilter,
MetadataFilterBase,
ObjectTypeFilter,
+)
+from ..core.filters.where_filters import (
+ BooleanWhereFilter,
+ GlobalIDMultipleChoiceWhereFilter,
+ ListObjectTypeWhereFilter,
+ MetadataWhereBase,
ObjectTypeWhereFilter,
OperationObjectTypeWhereFilter,
+ filter_where_metadata,
)
-from ..core.filters.where_filters import MetadataWhereBase, filter_where_metadata
from ..core.filters.where_input import (
FilterInputDescriptions,
GlobalIDFilterInput,
@@ -485,25 +489,23 @@ class PaymentMethodDetailsFilterInput(BaseInputObjectType):
def filter_card(qs, _, value):
if value is None:
return qs.none()
- transaction_query = TransactionItem.objects.using(qs.db).filter(
- payment_method_type=PaymentMethodType.CARD
- )
+ transaction_qs = qs.filter(payment_method_type=PaymentMethodType.CARD)
if brand_filter_value := value.get("brand"):
- transactions = filter_where_by_value_field(
- transaction_query, "cc_brand", brand_filter_value
+ transaction_qs = filter_where_by_value_field(
+ transaction_qs, "cc_brand", brand_filter_value
)
- return qs.filter(Exists(transactions.filter(order_id=OuterRef("id"))))
+ return transaction_qs
@staticmethod
def filter_type(qs, _, value):
if value is None:
return qs.none()
- transactions = filter_where_by_value_field(
- TransactionItem.objects.using(qs.db),
+ transaction_qs = filter_where_by_value_field(
+ qs,
"payment_method_type",
value,
)
- return qs.filter(Exists(transactions.filter(order_id=OuterRef("id"))))
+ return transaction_qs
class TransactionFilterInput(BaseInputObjectType):
@@ -534,10 +536,7 @@ class TransactionFilterInput(BaseInputObjectType):
if value is None:
return qs.none()
- transaction_qs = filter_where_metadata(
- TransactionItem.objects.using(qs.db), None, value
- )
- return qs.filter(Exists(transaction_qs.filter(order_id=OuterRef("id"))))
+ return filter_where_metadata(qs, None, value)
def filter_where_number(qs, _, value):
@@ -597,22 +596,34 @@ def filter_where_is_click_and_collect(qs, _, value):
def filter_where_transactions(qs, _, value):
- if value is None:
+ if not value:
return qs.none()
- metadata_value = value.get("metadata")
- payment_method_details_value = value.get("payment_method_details")
+ lookup = Q()
+ for input_data in value:
+ metadata_value = input_data.get("metadata")
+ payment_method_details_value = input_data.get("payment_method_details")
- if not any([metadata_value, payment_method_details_value]):
- return qs.none()
+ if not any([metadata_value, payment_method_details_value]):
+ return qs.none()
- if payment_method_details_value:
- qs = TransactionFilterInput.filter_payment_method_details(
- qs, _, payment_method_details_value
- )
- if metadata_value:
- qs = TransactionFilterInput.filter_metadata(qs, _, metadata_value)
- return qs
+ transaction_qs = None
+ if payment_method_details_value:
+ transaction_qs = TransactionFilterInput.filter_payment_method_details(
+ TransactionItem.objects.using(qs.db), _, payment_method_details_value
+ )
+ if metadata_value:
+ transaction_qs = TransactionFilterInput.filter_metadata(
+ transaction_qs or TransactionItem.objects.using(qs.db),
+ _,
+ metadata_value,
+ )
+ if transaction_qs is not None:
+ lookup &= Q(Exists(transaction_qs.filter(order_id=OuterRef("id"))))
+
+ if lookup:
+ return qs.filter(lookup)
+ return qs.none()
def filter_where_lines(qs, _, value):
@@ -770,10 +781,15 @@ class OrderWhere(MetadataWhereBase):
method=filter_where_lines_count,
help_text="Filter by number of lines in the order.",
)
- transactions = ObjectTypeWhereFilter(
+ transactions = ListObjectTypeWhereFilter(
input_class=TransactionFilterInput,
method=filter_where_transactions,
- help_text="Filter by transaction data associated with the order.",
+ help_text=(
+ "Filter by transaction data associated with the order."
+ "Each list item specifies conditions that must be satisfied by a single "
+ "transaction. The filter matches orders that have related objects "
+ "meeting all specified groups of conditions."
+ ),
)
total_gross = ObjectTypeWhereFilter(
input_class=PriceFilterInput,
@@ -921,10 +937,15 @@ class DraftOrderWhere(MetadataWhereBase):
method=filter_where_lines_count,
help_text="Filter by number of lines in the order.",
)
- transactions = ObjectTypeWhereFilter(
+ transactions = ListObjectTypeWhereFilter(
input_class=TransactionFilterInput,
method=filter_where_transactions,
- help_text="Filter by transaction data associated with the order.",
+ help_text=(
+ "Filter by transaction data associated with the order."
+ "Each list item specifies conditions that must be satisfied by a single "
+ "transaction. The filter matches orders that have related objects "
+ "meeting all specified groups of conditions."
+ ),
)
total_gross = ObjectTypeWhereFilter(
input_class=PriceFilterInput,
diff --git a/saleor/graphql/order/tests/queries/test_draft_order_with_where.py b/saleor/graphql/order/tests/queries/test_draft_order_with_where.py
index 578cc0af8a..da779bd67a 100644
--- a/saleor/graphql/order/tests/queries/test_draft_order_with_where.py
+++ b/saleor/graphql/order/tests/queries/test_draft_order_with_where.py
@@ -1343,68 +1343,113 @@ def test_draft_orders_filter_by_order_events(
[
(
{
- "paymentMethodDetails": {
- "type": {"eq": "CARD"},
- }
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "type": {"eq": "CARD"},
+ }
+ }
+ ]
},
[0, 2],
),
(
{
- "paymentMethodDetails": {
- "type": {"eq": "OTHER"},
- }
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "type": {"eq": "OTHER"},
+ }
+ }
+ ]
},
[1],
),
(
{
- "paymentMethodDetails": {
- "card": {
- "brand": {"eq": "Brand"},
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "card": {
+ "brand": {"eq": "Brand"},
+ }
+ }
}
- }
+ ]
},
[0],
),
(
{
- "paymentMethodDetails": {
- "card": {
- "brand": {"eq": "Brand4"},
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "card": {
+ "brand": {"eq": "Brand4"},
+ }
+ }
}
- }
+ ]
},
[2],
),
(
{
- "paymentMethodDetails": {
- "card": {
- "brand": {"eq": "Brand2"},
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "card": {
+ "brand": {"eq": "Brand2"},
+ }
+ }
}
- }
+ ]
},
[0],
),
(
{
- "paymentMethodDetails": {
- "type": {"oneOf": ["CARD", "OTHER"]},
- }
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "type": {"oneOf": ["CARD", "OTHER"]},
+ }
+ }
+ ]
},
[0, 1, 2],
),
(
{
- "paymentMethodDetails": {
- "card": {
- "brand": {"oneOf": ["Brand2", "Brand4"]},
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "card": {
+ "brand": {"oneOf": ["Brand2", "Brand4"]},
+ }
+ }
}
- }
+ ]
},
[0, 2],
),
+ (
+ {
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "type": {"eq": "CARD"},
+ }
+ },
+ {
+ "paymentMethodDetails": {
+ "card": {"brand": {"eq": "Brand"}},
+ }
+ },
+ ]
+ },
+ [0],
+ ),
],
)
def test_draft_orders_filter_by_transaction_payment_details(
@@ -1465,7 +1510,7 @@ def test_draft_orders_filter_by_transaction_payment_details(
)
permission_group_manage_orders.user_set.add(staff_api_client.user)
- variables = {"where": {"transactions": where}}
+ variables = {"where": where}
# when
response = staff_api_client.post_graphql(DRAFT_ORDERS_WHERE_QUERY, variables)
@@ -1479,20 +1524,47 @@ def test_draft_orders_filter_by_transaction_payment_details(
@pytest.mark.parametrize(
- ("metadata", "expected_indexes"),
+ ("metadata_list", "expected_indexes"),
[
- ({"key": "foo"}, [0, 1]),
- ({"key": "foo", "value": {"eq": "bar"}}, [0]),
- ({"key": "foo", "value": {"eq": "baz"}}, []),
- ({"key": "foo", "value": {"oneOf": ["bar", "zaz"]}}, [0, 1]),
- ({"key": "notfound"}, []),
- ({"key": "foo", "value": {"eq": None}}, []),
- ({"key": "foo", "value": {"oneOf": []}}, []),
- (None, []),
+ (
+ [
+ {"metadata": {"key": "foo"}},
+ {"metadata": {"key": "foo", "value": {"oneOf": ["bar", "zaz"]}}},
+ ],
+ [0, 1],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo", "value": {"eq": "bar"}}},
+ {"metadata": {"key": "foo", "value": {"eq": "zaz"}}},
+ ],
+ [],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo", "value": {"eq": "bar"}}},
+ {"metadata": {"key": "notfound"}},
+ ],
+ [],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo", "value": {"eq": "zaz"}}},
+ {"metadata": {"key": "foo"}},
+ ],
+ [1],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo", "value": {"eq": "baz"}}},
+ {"metadata": {"key": "notfound"}},
+ ],
+ [],
+ ),
],
)
def test_draft_orders_filter_by_transaction_metadata(
- metadata,
+ metadata_list,
expected_indexes,
draft_order_list,
staff_api_client,
@@ -1525,7 +1597,114 @@ def test_draft_orders_filter_by_transaction_metadata(
)
permission_group_manage_orders.user_set.add(staff_api_client.user)
- variables = {"where": {"transactions": {"metadata": metadata}}}
+ variables = {"where": {"transactions": metadata_list}}
+
+ # when
+ response = staff_api_client.post_graphql(DRAFT_ORDERS_WHERE_QUERY, variables)
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["draftOrders"]["edges"]
+ assert len(orders) == len(expected_indexes)
+ numbers = {node["node"]["number"] for node in orders}
+ assert numbers == {str(draft_order_list[i].number) for i in expected_indexes}
+
+
+ ("transaction_filters", "expected_indexes"),
+ [
+ (
+ [
+ {"metadata": {"key": "foo"}},
+ {"paymentMethodDetails": {"type": {"eq": "CARD"}}},
+ ],
+ [0, 2],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo"}},
+ {"paymentMethodDetails": {"type": {"eq": "OTHER"}}},
+ ],
+ [1],
+ ),
+ (
+ [
+ {"metadata": {"key": "notfound"}},
+ {"paymentMethodDetails": {"type": {"eq": "OTHER"}}},
+ ],
+ [],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo", "value": {"eq": "baz"}}},
+ {"paymentMethodDetails": {"type": {"eq": "CARD"}}},
+ ],
+ [0],
+ ),
+ ],
+)
+def test_draft_orders_filter_by_transactions_with_mixed_conditions(
+ transaction_filters,
+ expected_indexes,
+ draft_order_list,
+ staff_api_client,
+ permission_group_manage_orders,
+ transaction_item_generator,
+):
+ # given
+ transaction_item_generator(
+ order_id=draft_order_list[0].pk,
+ charged_value=draft_order_list[0].total.gross.amount,
+ payment_method_type="card",
+ payment_method_name="Credit card",
+ cc_brand="Brand",
+ cc_first_digits="1234",
+ cc_last_digits="5678",
+ cc_exp_month=12,
+ cc_exp_year=2025,
+ metadata={},
+ )
+
+ # second_transaction
+ transaction_item_generator(
+ order_id=draft_order_list[0].pk,
+ charged_value=draft_order_list[0].total.gross.amount,
+ payment_method_type="card",
+ payment_method_name="Second Credit card",
+ cc_brand="Brand2",
+ cc_first_digits="1234",
+ cc_last_digits="5678",
+ cc_exp_month=12,
+ cc_exp_year=2025,
+ metadata={"foo": "baz"},
+ )
+
+ # third_transaction
+ transaction_item_generator(
+ order_id=draft_order_list[1].pk,
+ charged_value=draft_order_list[1].total.gross.amount,
+ payment_method_type="other",
+ payment_method_name="Third payment method",
+ cc_brand=None,
+ cc_first_digits=None,
+ cc_last_digits=None,
+ cc_exp_month=None,
+ cc_exp_year=None,
+ metadata={"foo": "zaz"},
+ )
+
+ # fourth_transaction
+ transaction_item_generator(
+ order_id=draft_order_list[2].pk,
+ charged_value=draft_order_list[2].total.gross.amount,
+ payment_method_type="card",
+ payment_method_name="Fourth Credit card",
+ cc_brand="Brand4",
+ metadata={"foo": "bar"},
+ )
+
+ permission_group_manage_orders.user_set.add(staff_api_client.user)
+ variables = {"where": {"transactions": transaction_filters}}
# when
response = staff_api_client.post_graphql(DRAFT_ORDERS_WHERE_QUERY, variables)
diff --git a/saleor/graphql/order/tests/queries/test_order_with_where.py b/saleor/graphql/order/tests/queries/test_order_with_where.py
index e7650016b0..83964dbb27 100644
--- a/saleor/graphql/order/tests/queries/test_order_with_where.py
+++ b/saleor/graphql/order/tests/queries/test_order_with_where.py
@@ -2313,68 +2313,113 @@ def test_orders_filter_by_order_events(
[
(
{
- "paymentMethodDetails": {
- "type": {"eq": "CARD"},
- }
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "type": {"eq": "CARD"},
+ }
+ }
+ ]
},
[0, 2],
),
(
{
- "paymentMethodDetails": {
- "type": {"eq": "OTHER"},
- }
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "type": {"eq": "OTHER"},
+ }
+ }
+ ]
},
[1],
),
(
{
- "paymentMethodDetails": {
- "card": {
- "brand": {"eq": "Brand"},
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "card": {
+ "brand": {"eq": "Brand"},
+ }
+ }
}
- }
+ ]
},
[0],
),
(
{
- "paymentMethodDetails": {
- "card": {
- "brand": {"eq": "Brand4"},
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "card": {
+ "brand": {"eq": "Brand4"},
+ }
+ }
}
- }
+ ]
},
[2],
),
(
{
- "paymentMethodDetails": {
- "card": {
- "brand": {"eq": "Brand2"},
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "card": {
+ "brand": {"eq": "Brand2"},
+ }
+ }
}
- }
+ ]
},
[0],
),
(
{
- "paymentMethodDetails": {
- "type": {"oneOf": ["CARD", "OTHER"]},
- }
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "type": {"oneOf": ["CARD", "OTHER"]},
+ }
+ }
+ ]
},
[0, 1, 2],
),
(
{
- "paymentMethodDetails": {
- "card": {
- "brand": {"oneOf": ["Brand2", "Brand4"]},
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "card": {
+ "brand": {"oneOf": ["Brand2", "Brand4"]},
+ }
+ }
}
- }
+ ]
},
[0, 2],
),
+ (
+ {
+ "transactions": [
+ {
+ "paymentMethodDetails": {
+ "type": {"eq": "CARD"},
+ }
+ },
+ {
+ "paymentMethodDetails": {
+ "card": {"brand": {"eq": "Brand"}},
+ }
+ },
+ ]
+ },
+ [0],
+ ),
],
)
def test_orders_filter_by_transaction_payment_details(
@@ -2435,7 +2480,7 @@ def test_orders_filter_by_transaction_payment_details(
)
permission_group_manage_orders.user_set.add(staff_api_client.user)
- variables = {"where": {"transactions": where}}
+ variables = {"where": where}
# when
response = staff_api_client.post_graphql(ORDERS_WHERE_QUERY, variables)
@@ -2449,20 +2494,47 @@ def test_orders_filter_by_transaction_payment_details(
@pytest.mark.parametrize(
- ("metadata", "expected_indexes"),
+ ("metadata_list", "expected_indexes"),
[
- ({"key": "foo"}, [0, 1]),
- ({"key": "foo", "value": {"eq": "bar"}}, [0]),
- ({"key": "foo", "value": {"eq": "baz"}}, []),
- ({"key": "foo", "value": {"oneOf": ["bar", "zaz"]}}, [0, 1]),
- ({"key": "notfound"}, []),
- ({"key": "foo", "value": {"eq": None}}, []),
- ({"key": "foo", "value": {"oneOf": []}}, []),
- (None, []),
+ (
+ [
+ {"metadata": {"key": "foo"}},
+ {"metadata": {"key": "foo", "value": {"oneOf": ["bar", "zaz"]}}},
+ ],
+ [0, 1],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo", "value": {"eq": "bar"}}},
+ {"metadata": {"key": "foo", "value": {"eq": "zaz"}}},
+ ],
+ [],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo", "value": {"eq": "bar"}}},
+ {"metadata": {"key": "notfound"}},
+ ],
+ [],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo", "value": {"eq": "zaz"}}},
+ {"metadata": {"key": "foo"}},
+ ],
+ [1],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo", "value": {"eq": "baz"}}},
+ {"metadata": {"key": "notfound"}},
+ ],
+ [],
+ ),
],
)
def test_orders_filter_by_transaction_metadata(
- metadata,
+ metadata_list,
expected_indexes,
order_list,
staff_api_client,
@@ -2495,7 +2567,114 @@ def test_orders_filter_by_transaction_metadata(
)
permission_group_manage_orders.user_set.add(staff_api_client.user)
- variables = {"where": {"transactions": {"metadata": metadata}}}
+ variables = {"where": {"transactions": metadata_list}}
+
+ # when
+ response = staff_api_client.post_graphql(ORDERS_WHERE_QUERY, variables)
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["orders"]["edges"]
+ assert len(orders) == len(expected_indexes)
+ numbers = {node["node"]["number"] for node in orders}
+ assert numbers == {str(order_list[i].number) for i in expected_indexes}
+
+
+ ("transaction_filters", "expected_indexes"),
+ [
+ (
+ [
+ {"metadata": {"key": "foo"}},
+ {"paymentMethodDetails": {"type": {"eq": "CARD"}}},
+ ],
+ [0, 2],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo"}},
+ {"paymentMethodDetails": {"type": {"eq": "OTHER"}}},
+ ],
+ [1],
+ ),
+ (
+ [
+ {"metadata": {"key": "notfound"}},
+ {"paymentMethodDetails": {"type": {"eq": "OTHER"}}},
+ ],
+ [],
+ ),
+ (
+ [
+ {"metadata": {"key": "foo", "value": {"eq": "baz"}}},
+ {"paymentMethodDetails": {"type": {"eq": "CARD"}}},
+ ],
+ [0],
+ ),
+ ],
+)
+def test_orders_filter_by_transactions_with_mixed_conditions(
+ transaction_filters,
+ expected_indexes,
+ order_list,
+ staff_api_client,
+ permission_group_manage_orders,
+ transaction_item_generator,
+):
+ # given
+ transaction_item_generator(
+ order_id=order_list[0].pk,
+ charged_value=order_list[0].total.gross.amount,
+ payment_method_type="card",
+ payment_method_name="Credit card",
+ cc_brand="Brand",
+ cc_first_digits="1234",
+ cc_last_digits="5678",
+ cc_exp_month=12,
+ cc_exp_year=2025,
+ metadata={},
+ )
+
+ # second_transaction
+ transaction_item_generator(
+ order_id=order_list[0].pk,
+ charged_value=order_list[0].total.gross.amount,
+ payment_method_type="card",
+ payment_method_name="Second Credit card",
+ cc_brand="Brand2",
+ cc_first_digits="1234",
+ cc_last_digits="5678",
+ cc_exp_month=12,
+ cc_exp_year=2025,
+ metadata={"foo": "baz"},
+ )
+
+ # third_transaction
+ transaction_item_generator(
+ order_id=order_list[1].pk,
+ charged_value=order_list[1].total.gross.amount,
+ payment_method_type="other",
+ payment_method_name="Third payment method",
+ cc_brand=None,
+ cc_first_digits=None,
+ cc_last_digits=None,
+ cc_exp_month=None,
+ cc_exp_year=None,
+ metadata={"foo": "zaz"},
+ )
+
+ # fourth_transaction
+ transaction_item_generator(
+ order_id=order_list[2].pk,
+ charged_value=order_list[2].total.gross.amount,
+ payment_method_type="card",
+ payment_method_name="Fourth Credit card",
+ cc_brand="Brand4",
+ metadata={"foo": "bar"},
+ )
+
+ permission_group_manage_orders.user_set.add(staff_api_client.user)
+ variables = {"where": {"transactions": transaction_filters}}
# when
response = staff_api_client.post_graphql(ORDERS_WHERE_QUERY, variables)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 98fcbc84e6..bfa9f77bb5 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -13271,8 +13271,10 @@ input OrderWhereInput @doc(category: "Orders") {
"""Filter by number of lines in the order."""
linesCount: IntFilterInput
- """Filter by transaction data associated with the order."""
- transactions: TransactionFilterInput
+ """
+ Filter by transaction data associated with the order.Each list item specifies conditions that must be satisfied by a single transaction. The filter matches orders that have related objects meeting all specified groups of conditions.
+ """
+ transactions: [TransactionFilterInput!]
"""Filter by total gross amount of the order."""
totalGross: PriceFilterInput
@@ -13515,8 +13517,10 @@ input DraftOrderWhereInput @doc(category: "Orders") {
"""Filter by number of lines in the order."""
linesCount: IntFilterInput
- """Filter by transaction data associated with the order."""
- transactions: TransactionFilterInput
+ """
+ Filter by transaction data associated with the order.Each list item specifies conditions that must be satisfied by a single transaction. The filter matches orders that have related objects meeting all specified groups of conditions.
+ """
+ transactions: [TransactionFilterInput!]
"""Filter by total gross amount of the order."""
totalGross: PriceFilterInput