Skip to content

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

Enable filtering of orders based on metadata from associated transaction items (#17879)

Maciej Korycinski82e92a9504ad
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2d2f666239..1bf737a4ce 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,11 +24,12 @@ All notable, unreleased changes to this project will be documented in this file.
- Filter by number of lines in the order.
- Filter by order total gross and net price amount.
- Filter by order metadata.
- - Filter by order by associated lines metadata.
+ - Filter by associated lines metadata.
- Filter by the product type of related order lines.
- Filter by associated event type and date.
- Filter by associated payment method name and type.
- Filter by associated billing and shipping address phone number and country code.
+ - Filter by associated transactionItems metadata.
- Extend the `Page` type with an `attribute` field. Adds support for querying a specific attribute on a page by `slug`, returning the matching attribute and its assigned values, or null if no match is found.
- Enhanced order search options. Orders can now be searched using:
- The order's ID
diff --git a/saleor/graphql/order/filters.py b/saleor/graphql/order/filters.py
index cf232634c4..44d3ba4d13 100644
--- a/saleor/graphql/order/filters.py
+++ b/saleor/graphql/order/filters.py
@@ -510,6 +510,9 @@ class TransactionFilterInput(BaseInputObjectType):
payment_method_details = PaymentMethodDetailsFilterInput(
description="Filter by payment method details used to pay for the order.",
)
+ metadata = MetadataFilterInput(
+ description="Filter by metadata fields of transactions."
+ )
class Meta:
doc_category = DOC_CATEGORY_ORDERS
@@ -526,6 +529,16 @@ class TransactionFilterInput(BaseInputObjectType):
return PaymentMethodDetailsFilterInput.filter_type(qs, _, filter_value)
return qs.none()
+ @staticmethod
+ def filter_metadata(qs, _, value):
+ 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"))))
+
class OrderWhere(MetadataWhereBase):
ids = GlobalIDMultipleChoiceWhereFilter(method=filter_by_ids("Order"))
@@ -775,11 +788,20 @@ class OrderWhere(MetadataWhereBase):
def filter_transactions(qs, _, value):
if value is None:
return qs.none()
- if filter_value := value.get("payment_method_details"):
- return TransactionFilterInput.filter_payment_method_details(
- qs, _, filter_value
+
+ metadata_value = value.get("metadata")
+ payment_method_details_value = value.get("payment_method_details")
+
+ 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
)
- return qs.none()
+ if metadata_value:
+ qs = TransactionFilterInput.filter_metadata(qs, _, metadata_value)
+ return qs
@staticmethod
def filter_total_gross(qs, _, value):
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 9f12debb12..e7650016b0 100644
--- a/saleor/graphql/order/tests/queries/test_order_with_where.py
+++ b/saleor/graphql/order/tests/queries/test_order_with_where.py
@@ -2448,6 +2448,66 @@ def test_orders_filter_by_transaction_payment_details(
assert numbers == {str(order_list[index].number) for index in indexes}
+ ("metadata", "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, []),
+ ],
+)
+def test_orders_filter_by_transaction_metadata(
+ metadata,
+ 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,
+ metadata={"foo": "bar"},
+ )
+
+ transaction_item_generator(
+ order_id=order_list[0].pk,
+ charged_value=order_list[0].total.gross.amount,
+ metadata={},
+ )
+
+ transaction_item_generator(
+ order_id=order_list[1].pk,
+ charged_value=order_list[1].total.gross.amount,
+ metadata={"foo": "zaz"},
+ )
+
+ transaction_item_generator(
+ order_id=order_list[2].pk,
+ charged_value=order_list[2].total.gross.amount,
+ metadata={},
+ )
+
+ permission_group_manage_orders.user_set.add(staff_api_client.user)
+ variables = {"where": {"transactions": {"metadata": metadata}}}
+
+ # 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}
+
+
@pytest.mark.parametrize(
("address_filter", "expected_indexes"),
[
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index bb339da44b..4beb5174e7 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -13198,6 +13198,9 @@ input LinesFilterInput @doc(category: "Orders") {
input TransactionFilterInput @doc(category: "Orders") {
"""Filter by payment method details used to pay for the order."""
paymentMethodDetails: PaymentMethodDetailsFilterInput
+
+ """Filter by metadata fields of transactions."""
+ metadata: MetadataFilterInput
}
input PaymentMethodDetailsFilterInput {
diff --git a/saleor/payment/tests/fixtures/transaction_item.py b/saleor/payment/tests/fixtures/transaction_item.py
index 07cfaa942a..ebf7763638 100644
--- a/saleor/payment/tests/fixtures/transaction_item.py
+++ b/saleor/payment/tests/fixtures/transaction_item.py
@@ -32,9 +32,12 @@ def transaction_item_generator():
cc_last_digits=None,
cc_exp_month=None,
cc_exp_year=None,
+ metadata=None,
):
if available_actions is None:
available_actions = []
+ if metadata is None:
+ metadata = {}
transaction = TransactionItem.objects.create(
token=uuid.uuid4(),
name=name,
@@ -56,6 +59,7 @@ def transaction_item_generator():
cc_last_digits=cc_last_digits,
cc_exp_month=cc_exp_month,
cc_exp_year=cc_exp_year,
+ metadata=metadata,
)
create_manual_adjustment_events(
transaction=transaction,