Skip to content

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

Improve filter_customer on orders (#16386) (#16534)

Krzysztof Kwaśniaka30ea4113f49
diff --git a/saleor/account/migrations/0086_auto_20240805_0851.py b/saleor/account/migrations/0086_auto_20240805_0851.py
new file mode 100644
index 0000000000..dec1fab33e
--- /dev/null
+++ b/saleor/account/migrations/0086_auto_20240805_0851.py
@@ -0,0 +1,34 @@
+# Generated by Django 3.2.25 on 2024-08-05 08:51
+
+import django.contrib.postgres.indexes
+from django.contrib.postgres.operations import AddIndexConcurrently
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("account", "0085_address_validation_skipped"),
+ ]
+
+ atomic = False
+
+ operations = [
+ AddIndexConcurrently(
+ model_name="user",
+ index=django.contrib.postgres.indexes.GinIndex(
+ fields=["first_name"], name="first_name_gin", opclasses=["gin_trgm_ops"]
+ ),
+ ),
+ AddIndexConcurrently(
+ model_name="user",
+ index=django.contrib.postgres.indexes.GinIndex(
+ fields=["last_name"], name="last_name_gin", opclasses=["gin_trgm_ops"]
+ ),
+ ),
+ migrations.RunSQL(
+ sql="""
+ CREATE INDEX CONCURRENTLY idx_user_full_name ON account_user USING gin ((first_name || ' ' || last_name) gin_trgm_ops);
+ """,
+ reverse_sql="""DROP INDEX CONCURRENTLY idx_user_full_name;""",
+ ),
+ ]
diff --git a/saleor/account/models.py b/saleor/account/models.py
index d8f0bc899f..9d80daeac6 100644
--- a/saleor/account/models.py
+++ b/saleor/account/models.py
@@ -223,6 +223,16 @@ class User(
fields=["private_metadata"],
opclasses=["jsonb_path_ops"],
),
+ GinIndex(
+ fields=["first_name"],
+ name="first_name_gin",
+ opclasses=["gin_trgm_ops"],
+ ),
+ GinIndex(
+ fields=["last_name"],
+ name="last_name_gin",
+ opclasses=["gin_trgm_ops"],
+ ),
]
def __init__(self, *args, **kwargs):
diff --git a/saleor/graphql/order/filters.py b/saleor/graphql/order/filters.py
index 5968165df5..62f983b60a 100644
--- a/saleor/graphql/order/filters.py
+++ b/saleor/graphql/order/filters.py
@@ -3,10 +3,12 @@ from uuid import UUID
import django_filters
import graphene
from django.core.exceptions import ValidationError
-from django.db.models import Exists, OuterRef, Q
+from django.core.validators import validate_email
+from django.db.models import Exists, OuterRef, Q, Value
from django.utils import timezone
from graphql.error import GraphQLError
+from ...core.postgres import FlatConcat
from ...giftcard import GiftCardEvents
from ...giftcard.models import GiftCardEvent
from ...order.models import Order, OrderLine
@@ -79,13 +81,43 @@ def filter_status(qs, _, value):
return qs & query_objects
-def filter_customer(qs, _, value):
- qs = qs.filter(
+def _filter_customer_by_email_first_or_last_name(qs, value):
+ return qs.filter(
Q(user_email__ilike=value)
- | Q(user__email__trigram_similar=value)
- | Q(user__first_name__trigram_similar=value)
- | Q(user__last_name__trigram_similar=value)
+ | Q(user__email__ilike=value)
+ | Q(user__first_name__ilike=value)
+ | Q(user__last_name__ilike=value)
)
+
+
+def _filter_by_customer_full_name(qs, value):
+ try:
+ first, last = value.split(" ", 1)
+ except ValueError:
+ qs = _filter_customer_by_email_first_or_last_name(qs, value)
+ else:
+ qs = qs.alias(
+ user_full_name=FlatConcat(
+ "user__first_name",
+ Value(" "),
+ "user__last_name",
+ )
+ ).filter(
+ Q(user_full_name__iexact=value)
+ | Q(user_full_name__iexact=f"{last} {first}")
+ )
+
+ return qs
+
+
+def filter_customer(qs, _, value):
+ try:
+ validate_email(value)
+ except ValidationError:
+ qs = _filter_by_customer_full_name(qs, value)
+ else:
+ qs = qs.filter(Q(user_email__iexact=value) | Q(user__email__iexact=value))
+
return qs
diff --git a/saleor/graphql/order/tests/test_filters.py b/saleor/graphql/order/tests/test_filters.py
new file mode 100644
index 0000000000..936b322c91
--- /dev/null
+++ b/saleor/graphql/order/tests/test_filters.py
@@ -0,0 +1,198 @@
+from unittest.mock import ANY, patch
+
+import pytest
+
+from ....order.models import Order
+from ..filters import (
+ _filter_by_customer_full_name,
+ _filter_customer_by_email_first_or_last_name,
+ filter_customer,
+)
+
+
+def similar_customers_with_orders(order, customer_user, customer_user2, channel_USD):
+ customer_user.first_name = "Test"
+ customer_user.last_name = "User"
+ customer_user.save()
+ customer_user2.first_name = "tester"
+ customer_user2.last_name = "users"
+ customer_user2.save()
+ order2 = Order.objects.create(
+ billing_address=customer_user2.default_billing_address,
+ channel=channel_USD,
+ user_email=customer_user2.email,
+ user=customer_user2,
+ )
+ return order, order2, customer_user, customer_user2
+
+
+@patch(
+ "saleor.graphql.order.filters._filter_customer_by_email_first_or_last_name",
+ wraps=_filter_customer_by_email_first_or_last_name,
+)
+@patch(
+ "saleor.graphql.order.filters._filter_by_customer_full_name",
+ wraps=_filter_by_customer_full_name,
+)
+def test_filter_customer_by_email(
+ mock_filter_by_customer_full_name,
+ mock_filter_customer_by_email_first_or_last_name,
+ similar_customers_with_orders,
+):
+ # given
+ order, order2, customer_user, customer_user2 = similar_customers_with_orders
+ qs = Order.objects.all()
+ value = customer_user.email
+
+ # when
+ qs = filter_customer(qs, None, value)
+
+ # then
+ assert qs.count() == 1
+ assert qs.first() == order
+ mock_filter_by_customer_full_name.assert_not_called()
+ mock_filter_customer_by_email_first_or_last_name.assert_not_called()
+
+
+@patch(
+ "saleor.graphql.order.filters._filter_customer_by_email_first_or_last_name",
+ wraps=_filter_customer_by_email_first_or_last_name,
+)
+@patch(
+ "saleor.graphql.order.filters._filter_by_customer_full_name",
+ wraps=_filter_by_customer_full_name,
+)
+def test_filter_customer_by_first_name(
+ mock_filter_by_customer_full_name,
+ mock_filter_customer_by_email_first_or_last_name,
+ similar_customers_with_orders,
+):
+ # given
+ order, order2, customer_user, customer_user2 = similar_customers_with_orders
+ qs = Order.objects.all()
+ value = customer_user.first_name
+
+ # when
+ qs = filter_customer(qs, None, value)
+
+ # then
+ assert qs.count() == 2
+ assert order in qs
+ assert order2 in qs
+ mock_filter_by_customer_full_name.assert_called_once_with(ANY, value)
+ mock_filter_customer_by_email_first_or_last_name.assert_called_once_with(ANY, value)
+
+
+@patch(
+ "saleor.graphql.order.filters._filter_customer_by_email_first_or_last_name",
+ wraps=_filter_customer_by_email_first_or_last_name,
+)
+@patch(
+ "saleor.graphql.order.filters._filter_by_customer_full_name",
+ wraps=_filter_by_customer_full_name,
+)
+def test_filter_customer_by_last_name(
+ mock_filter_by_customer_full_name,
+ mock_filter_customer_by_email_first_or_last_name,
+ similar_customers_with_orders,
+):
+ # given
+ order, order2, customer_user, customer_user2 = similar_customers_with_orders
+ qs = Order.objects.all()
+ value = customer_user.last_name
+
+ # when
+ qs = filter_customer(qs, None, value)
+
+ # then
+ assert qs.count() == 2
+ assert order in qs
+ assert order2 in qs
+ mock_filter_by_customer_full_name.assert_called_once_with(ANY, value)
+ mock_filter_customer_by_email_first_or_last_name.assert_called_once_with(ANY, value)
+
+
+@patch(
+ "saleor.graphql.order.filters._filter_customer_by_email_first_or_last_name",
+ wraps=_filter_customer_by_email_first_or_last_name,
+)
+@patch(
+ "saleor.graphql.order.filters._filter_by_customer_full_name",
+ wraps=_filter_by_customer_full_name,
+)
+def test_filter_customer_by_full_name_first_last_name(
+ mock_filter_by_customer_full_name,
+ mock_filter_customer_by_email_first_or_last_name,
+ similar_customers_with_orders,
+):
+ # given
+ order, order2, customer_user, customer_user2 = similar_customers_with_orders
+ qs = Order.objects.all()
+ value = f"{customer_user.first_name} {customer_user.last_name}"
+
+ # when
+ qs = filter_customer(qs, None, value)
+
+ # then
+ assert qs.count() == 1
+ assert qs.first() == order
+ mock_filter_by_customer_full_name.assert_called_once_with(ANY, value)
+ mock_filter_customer_by_email_first_or_last_name.assert_not_called()
+
+
+@patch(
+ "saleor.graphql.order.filters._filter_customer_by_email_first_or_last_name",
+ wraps=_filter_customer_by_email_first_or_last_name,
+)
+@patch(
+ "saleor.graphql.order.filters._filter_by_customer_full_name",
+ wraps=_filter_by_customer_full_name,
+)
+def test_filter_customer_by_full_name_last_first_name(
+ mock_filter_by_customer_full_name,
+ mock_filter_customer_by_email_first_or_last_name,
+ similar_customers_with_orders,
+):
+ # given
+ order, order2, customer_user, customer_user2 = similar_customers_with_orders
+ qs = Order.objects.all()
+ value = f"{customer_user.last_name} {customer_user.first_name}"
+
+ # when
+ qs = filter_customer(qs, None, value)
+
+ # then
+ assert qs.count() == 1
+ assert qs.first() == order
+ mock_filter_by_customer_full_name.assert_called_once_with(ANY, value)
+ mock_filter_customer_by_email_first_or_last_name.assert_not_called()
+
+
+@patch(
+ "saleor.graphql.order.filters._filter_customer_by_email_first_or_last_name",
+ wraps=_filter_customer_by_email_first_or_last_name,
+)
+@patch(
+ "saleor.graphql.order.filters._filter_by_customer_full_name",
+ wraps=_filter_by_customer_full_name,
+)
+def test_filter_customer_by_email_domain(
+ mock_filter_by_customer_full_name,
+ mock_filter_customer_by_email_first_or_last_name,
+ similar_customers_with_orders,
+):
+ # given
+ order, order2, customer_user, customer_user2 = similar_customers_with_orders
+ qs = Order.objects.all()
+ value = customer_user.email.split("@")[1]
+
+ # when
+ qs = filter_customer(qs, None, value)
+
+ # then
+ assert qs.count() == 2
+ assert order in qs
+ assert order2 in qs
+ mock_filter_by_customer_full_name.assert_called_once_with(ANY, value)
+ mock_filter_customer_by_email_first_or_last_name.assert_called_once_with(ANY, value)