prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Gift card filter by `createdByEmail`
  ## Problem
  
  Customer support needs to find customers giftcards to be able to resend code, correct balance or inquiry about balance
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `saleor/graphql/giftcard/filters.py`
  Content:
  ```
  1 from typing import List
  2 from uuid import UUID
  3 
  4 import django_filters
  5 import graphene
  6 from django.db.models import Exists, OuterRef, Q
  7 from graphql.error import GraphQLError
  8 
  9 from ...account import models as account_models
  10 from ...giftcard import models
  11 from ...order import models as order_models
  12 from ...product import models as product_models
  13 from ..core.doc_category import DOC_CATEGORY_GIFT_CARDS
  14 from ..core.filters import (
  15     GlobalIDMultipleChoiceFilter,
  16     ListObjectTypeFilter,
  17     MetadataFilterBase,
  18     ObjectTypeFilter,
  19 )
  20 from ..core.types import (
  21     BaseInputObjectType,
  22     FilterInputObjectType,
  23     NonNullList,
  24     PriceRangeInput,
  25 )
  26 from ..utils import resolve_global_ids_to_primary_keys
  27 from .enums import GiftCardEventsEnum
  28 
  29 
  30 def filter_products(qs, _, value):
  31     if value:
  32         _, product_pks = resolve_global_ids_to_primary_keys(value, "Product")
  33         qs = filter_gift_cards_by_products(qs, product_pks)
  34     return qs
  35 
  36 
  37 def filter_gift_cards_by_products(qs, product_ids):
  38     products = product_models.Product.objects.filter(pk__in=product_ids)
  39     return qs.filter(Exists(products.filter(pk=OuterRef("product_id"))))
  40 
  41 
  42 def filter_used_by(qs, _, value):
  43     if value:
  44         _, user_pks = resolve_global_ids_to_primary_keys(value, "User")
  45         qs = filter_gift_cards_by_used_by_user(qs, user_pks)
  46     return qs
  47 
  48 
  49 def filter_gift_cards_by_used_by_user(qs, user_pks):
  50     users = account_models.User.objects.filter(pk__in=user_pks)
  51     return qs.filter(Exists(users.filter(pk=OuterRef("used_by_id"))))
  52 
  53 
  54 def filter_tags_list(qs, _, value):
  55     if not value:
  56         return qs
  57     tags = models.GiftCardTag.objects.filter(name__in=value)
  58     return qs.filter(Exists(tags.filter(pk=OuterRef("tags__id"))))
  59 
  60 
  61 def filter_gift_card_used(qs, _, value):
  62     if value is None:
  63         return qs
  64     return qs.filter(used_by_email__isnull=not value)
  65 
  66 
  67 def filter_currency(qs, _, value):
  68     if not value:
  69         return qs
  70     return qs.filter(currency=value)
  71 
  72 
  73 def _filter_by_price(qs, field, value):
  74     lookup = {}
  75     if lte := value.get("lte"):
  76         lookup[f"{field}_amount__lte"] = lte
  77     if gte := value.get("gte"):
  78         lookup[f"{field}_amount__gte"] = gte
  79     return qs.filter(**lookup)
  80 
  81 
  82 def filter_code(qs, _, value):
  83     if not value:
  84         return qs
  85     return qs.filter(code=value)
  86 
  87 
  88 class GiftCardFilter(MetadataFilterBase):
  89     tags = ListObjectTypeFilter(input_class=graphene.String, method=filter_tags_list)
  90     products = GlobalIDMultipleChoiceFilter(method=filter_products)
  91     used_by = GlobalIDMultipleChoiceFilter(method=filter_used_by)
  92     used = django_filters.BooleanFilter(method=filter_gift_card_used)
  93     currency = django_filters.CharFilter(method=filter_currency)
  94     current_balance = ObjectTypeFilter(
  95         input_class=PriceRangeInput, method="filter_current_balance"
  96     )
  97     initial_balance = ObjectTypeFilter(
  98         input_class=PriceRangeInput, method="filter_initial_balance"
  99     )
  100     is_active = django_filters.BooleanFilter()
  101     code = django_filters.CharFilter(method=filter_code)
  102 
  103     class Meta:
  104         model = models.GiftCard
  105         fields = ["is_active"]
  106 
  107     def filter_current_balance(self, queryset, name, value):
  108         check_currency_in_filter_data(self.data)
  109         return _filter_by_price(queryset, "current_balance", value)
  110 
  111     def filter_initial_balance(self, queryset, name, value):
  112         check_currency_in_filter_data(self.data)
  113         return _filter_by_price(queryset, "initial_balance", value)
  114 
  115 
  116 def check_currency_in_filter_data(filter_data: dict):
  117     currency = filter_data.get("currency")
  118     if not currency:
  119         raise GraphQLError(
  120             "You must provide a `currency` filter parameter for filtering by price."
  121         )
  122 
  123 
  124 class GiftCardFilterInput(FilterInputObjectType):
  125     class Meta:
  126         doc_category = DOC_CATEGORY_GIFT_CARDS
  127         filterset_class = GiftCardFilter
  128 
  129 
  130 def filter_events_by_type(events: List[models.GiftCardEvent], type_value: str):
  131     filtered_events = []
  132     for event in events:
  133         if event.type == type_value:
  134             filtered_events.append(event)
  135     return filtered_events
  136 
  137 
  138 def filter_events_by_orders(events: List[models.GiftCardEvent], order_ids: List[str]):
  139     order_pks = _get_order_pks(order_ids)
  140 
  141     filtered_events = []
  142     for event in events:
  143         if event.order_id in order_pks:
  144             filtered_events.append(event)
  145     return filtered_events
  146 
  147 
  148 def _get_order_pks(order_ids: List[str]):
  149     _, order_pks = resolve_global_ids_to_primary_keys(order_ids, "Order")
  150 
  151     pks = []
  152     old_pks = []
  153     for pk in order_pks:
  154         try:
  155             pks.append(UUID(pk))
  156         except ValueError:
  157             old_pks.append(pk)
  158 
  159     return order_models.Order.objects.filter(
  160         Q(id__in=pks) | (Q(use_old_id=True) & Q(number__in=old_pks))
  161     ).values_list("id", flat=True)
  162 
  163 
  164 class GiftCardEventFilterInput(BaseInputObjectType):
  165     type = graphene.Argument(GiftCardEventsEnum)
  166     orders = NonNullList(graphene.ID)
  167 
  168     class Meta:
  169         doc_category = DOC_CATEGORY_GIFT_CARDS
  170 
  171 
  172 def filter_gift_card_tag_search(qs, _, value):
  173     if not value:
  174         return qs
  175     return qs.filter(name__ilike=value)
  176 
  177 
  178 class GiftCardTagFilter(django_filters.FilterSet):
  179     search = django_filters.CharFilter(method=filter_gift_card_tag_search)
  180 
  181 
  182 class GiftCardTagFilterInput(FilterInputObjectType):
  183     class Meta:
  184         doc_category = DOC_CATEGORY_GIFT_CARDS
  185         filterset_class = GiftCardTagFilter
  186
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/saleor/graphql/giftcard/filters.py b/saleor/graphql/giftcard/filters.py
  --- a/saleor/graphql/giftcard/filters.py
  +++ b/saleor/graphql/giftcard/filters.py
  @@ -85,6 +85,12 @@
       return qs.filter(code=value)
   
   
  +def filter_created_by_email(qs, _, value):
  +    if not value:
  +        return qs
  +    return qs.filter(created_by_email=value)
  +
  +
   class GiftCardFilter(MetadataFilterBase):
       tags = ListObjectTypeFilter(input_class=graphene.String, method=filter_tags_list)
       products = GlobalIDMultipleChoiceFilter(method=filter_products)
  @@ -99,6 +105,7 @@
       )
       is_active = django_filters.BooleanFilter()
       code = django_filters.CharFilter(method=filter_code)
  +    created_by_email = django_filters.CharFilter(method=filter_created_by_email)
   
       class Meta:
           model = models.GiftCard
  
verification_info: {"golden_diff": "diff --git a/saleor/graphql/giftcard/filters.py b/saleor/graphql/giftcard/filters.py\n--- a/saleor/graphql/giftcard/filters.py\n+++ b/saleor/graphql/giftcard/filters.py\n@@ -85,6 +85,12 @@\n     return qs.filter(code=value)\n \n \n+def filter_created_by_email(qs, _, value):\n+    if not value:\n+        return qs\n+    return qs.filter(created_by_email=value)\n+\n+\n class GiftCardFilter(MetadataFilterBase):\n     tags = ListObjectTypeFilter(input_class=graphene.String, method=filter_tags_list)\n     products = GlobalIDMultipleChoiceFilter(method=filter_products)\n@@ -99,6 +105,7 @@\n     )\n     is_active = django_filters.BooleanFilter()\n     code = django_filters.CharFilter(method=filter_code)\n+    created_by_email = django_filters.CharFilter(method=filter_created_by_email)\n \n     class Meta:\n         model = models.GiftCard\n", "issue": "Gift card filter by `createdByEmail`\n## Problem\r\n\r\nCustomer support needs to find customers giftcards to be able to resend code, correct balance or inquiry about balance\r\n\n", "before_files": [{"content": "from typing import List\nfrom uuid import UUID\n\nimport django_filters\nimport graphene\nfrom django.db.models import Exists, OuterRef, Q\nfrom graphql.error import GraphQLError\n\nfrom ...account import models as account_models\nfrom ...giftcard import models\nfrom ...order import models as order_models\nfrom ...product import models as product_models\nfrom ..core.doc_category import DOC_CATEGORY_GIFT_CARDS\nfrom ..core.filters import (\n    GlobalIDMultipleChoiceFilter,\n    ListObjectTypeFilter,\n    MetadataFilterBase,\n    ObjectTypeFilter,\n)\nfrom ..core.types import (\n    BaseInputObjectType,\n    FilterInputObjectType,\n    NonNullList,\n    PriceRangeInput,\n)\nfrom ..utils import resolve_global_ids_to_primary_keys\nfrom .enums import GiftCardEventsEnum\n\n\ndef filter_products(qs, _, value):\n    if value:\n        _, product_pks = resolve_global_ids_to_primary_keys(value, \"Product\")\n        qs = filter_gift_cards_by_products(qs, product_pks)\n    return qs\n\n\ndef filter_gift_cards_by_products(qs, product_ids):\n    products = product_models.Product.objects.filter(pk__in=product_ids)\n    return qs.filter(Exists(products.filter(pk=OuterRef(\"product_id\"))))\n\n\ndef filter_used_by(qs, _, value):\n    if value:\n        _, user_pks = resolve_global_ids_to_primary_keys(value, \"User\")\n        qs = filter_gift_cards_by_used_by_user(qs, user_pks)\n    return qs\n\n\ndef filter_gift_cards_by_used_by_user(qs, user_pks):\n    users = account_models.User.objects.filter(pk__in=user_pks)\n    return qs.filter(Exists(users.filter(pk=OuterRef(\"used_by_id\"))))\n\n\ndef filter_tags_list(qs, _, value):\n    if not value:\n        return qs\n    tags = models.GiftCardTag.objects.filter(name__in=value)\n    return qs.filter(Exists(tags.filter(pk=OuterRef(\"tags__id\"))))\n\n\ndef filter_gift_card_used(qs, _, value):\n    if value is None:\n        return qs\n    return qs.filter(used_by_email__isnull=not value)\n\n\ndef filter_currency(qs, _, value):\n    if not value:\n        return qs\n    return qs.filter(currency=value)\n\n\ndef _filter_by_price(qs, field, value):\n    lookup = {}\n    if lte := value.get(\"lte\"):\n        lookup[f\"{field}_amount__lte\"] = lte\n    if gte := value.get(\"gte\"):\n        lookup[f\"{field}_amount__gte\"] = gte\n    return qs.filter(**lookup)\n\n\ndef filter_code(qs, _, value):\n    if not value:\n        return qs\n    return qs.filter(code=value)\n\n\nclass GiftCardFilter(MetadataFilterBase):\n    tags = ListObjectTypeFilter(input_class=graphene.String, method=filter_tags_list)\n    products = GlobalIDMultipleChoiceFilter(method=filter_products)\n    used_by = GlobalIDMultipleChoiceFilter(method=filter_used_by)\n    used = django_filters.BooleanFilter(method=filter_gift_card_used)\n    currency = django_filters.CharFilter(method=filter_currency)\n    current_balance = ObjectTypeFilter(\n        input_class=PriceRangeInput, method=\"filter_current_balance\"\n    )\n    initial_balance = ObjectTypeFilter(\n        input_class=PriceRangeInput, method=\"filter_initial_balance\"\n    )\n    is_active = django_filters.BooleanFilter()\n    code = django_filters.CharFilter(method=filter_code)\n\n    class Meta:\n        model = models.GiftCard\n        fields = [\"is_active\"]\n\n    def filter_current_balance(self, queryset, name, value):\n        check_currency_in_filter_data(self.data)\n        return _filter_by_price(queryset, \"current_balance\", value)\n\n    def filter_initial_balance(self, queryset, name, value):\n        check_currency_in_filter_data(self.data)\n        return _filter_by_price(queryset, \"initial_balance\", value)\n\n\ndef check_currency_in_filter_data(filter_data: dict):\n    currency = filter_data.get(\"currency\")\n    if not currency:\n        raise GraphQLError(\n            \"You must provide a `currency` filter parameter for filtering by price.\"\n        )\n\n\nclass GiftCardFilterInput(FilterInputObjectType):\n    class Meta:\n        doc_category = DOC_CATEGORY_GIFT_CARDS\n        filterset_class = GiftCardFilter\n\n\ndef filter_events_by_type(events: List[models.GiftCardEvent], type_value: str):\n    filtered_events = []\n    for event in events:\n        if event.type == type_value:\n            filtered_events.append(event)\n    return filtered_events\n\n\ndef filter_events_by_orders(events: List[models.GiftCardEvent], order_ids: List[str]):\n    order_pks = _get_order_pks(order_ids)\n\n    filtered_events = []\n    for event in events:\n        if event.order_id in order_pks:\n            filtered_events.append(event)\n    return filtered_events\n\n\ndef _get_order_pks(order_ids: List[str]):\n    _, order_pks = resolve_global_ids_to_primary_keys(order_ids, \"Order\")\n\n    pks = []\n    old_pks = []\n    for pk in order_pks:\n        try:\n            pks.append(UUID(pk))\n        except ValueError:\n            old_pks.append(pk)\n\n    return order_models.Order.objects.filter(\n        Q(id__in=pks) | (Q(use_old_id=True) & Q(number__in=old_pks))\n    ).values_list(\"id\", flat=True)\n\n\nclass GiftCardEventFilterInput(BaseInputObjectType):\n    type = graphene.Argument(GiftCardEventsEnum)\n    orders = NonNullList(graphene.ID)\n\n    class Meta:\n        doc_category = DOC_CATEGORY_GIFT_CARDS\n\n\ndef filter_gift_card_tag_search(qs, _, value):\n    if not value:\n        return qs\n    return qs.filter(name__ilike=value)\n\n\nclass GiftCardTagFilter(django_filters.FilterSet):\n    search = django_filters.CharFilter(method=filter_gift_card_tag_search)\n\n\nclass GiftCardTagFilterInput(FilterInputObjectType):\n    class Meta:\n        doc_category = DOC_CATEGORY_GIFT_CARDS\n        filterset_class = GiftCardTagFilter\n", "path": "saleor/graphql/giftcard/filters.py"}], "after_files": [{"content": "from typing import List\nfrom uuid import UUID\n\nimport django_filters\nimport graphene\nfrom django.db.models import Exists, OuterRef, Q\nfrom graphql.error import GraphQLError\n\nfrom ...account import models as account_models\nfrom ...giftcard import models\nfrom ...order import models as order_models\nfrom ...product import models as product_models\nfrom ..core.doc_category import DOC_CATEGORY_GIFT_CARDS\nfrom ..core.filters import (\n    GlobalIDMultipleChoiceFilter,\n    ListObjectTypeFilter,\n    MetadataFilterBase,\n    ObjectTypeFilter,\n)\nfrom ..core.types import (\n    BaseInputObjectType,\n    FilterInputObjectType,\n    NonNullList,\n    PriceRangeInput,\n)\nfrom ..utils import resolve_global_ids_to_primary_keys\nfrom .enums import GiftCardEventsEnum\n\n\ndef filter_products(qs, _, value):\n    if value:\n        _, product_pks = resolve_global_ids_to_primary_keys(value, \"Product\")\n        qs = filter_gift_cards_by_products(qs, product_pks)\n    return qs\n\n\ndef filter_gift_cards_by_products(qs, product_ids):\n    products = product_models.Product.objects.filter(pk__in=product_ids)\n    return qs.filter(Exists(products.filter(pk=OuterRef(\"product_id\"))))\n\n\ndef filter_used_by(qs, _, value):\n    if value:\n        _, user_pks = resolve_global_ids_to_primary_keys(value, \"User\")\n        qs = filter_gift_cards_by_used_by_user(qs, user_pks)\n    return qs\n\n\ndef filter_gift_cards_by_used_by_user(qs, user_pks):\n    users = account_models.User.objects.filter(pk__in=user_pks)\n    return qs.filter(Exists(users.filter(pk=OuterRef(\"used_by_id\"))))\n\n\ndef filter_tags_list(qs, _, value):\n    if not value:\n        return qs\n    tags = models.GiftCardTag.objects.filter(name__in=value)\n    return qs.filter(Exists(tags.filter(pk=OuterRef(\"tags__id\"))))\n\n\ndef filter_gift_card_used(qs, _, value):\n    if value is None:\n        return qs\n    return qs.filter(used_by_email__isnull=not value)\n\n\ndef filter_currency(qs, _, value):\n    if not value:\n        return qs\n    return qs.filter(currency=value)\n\n\ndef _filter_by_price(qs, field, value):\n    lookup = {}\n    if lte := value.get(\"lte\"):\n        lookup[f\"{field}_amount__lte\"] = lte\n    if gte := value.get(\"gte\"):\n        lookup[f\"{field}_amount__gte\"] = gte\n    return qs.filter(**lookup)\n\n\ndef filter_code(qs, _, value):\n    if not value:\n        return qs\n    return qs.filter(code=value)\n\n\ndef filter_created_by_email(qs, _, value):\n    if not value:\n        return qs\n    return qs.filter(created_by_email=value)\n\n\nclass GiftCardFilter(MetadataFilterBase):\n    tags = ListObjectTypeFilter(input_class=graphene.String, method=filter_tags_list)\n    products = GlobalIDMultipleChoiceFilter(method=filter_products)\n    used_by = GlobalIDMultipleChoiceFilter(method=filter_used_by)\n    used = django_filters.BooleanFilter(method=filter_gift_card_used)\n    currency = django_filters.CharFilter(method=filter_currency)\n    current_balance = ObjectTypeFilter(\n        input_class=PriceRangeInput, method=\"filter_current_balance\"\n    )\n    initial_balance = ObjectTypeFilter(\n        input_class=PriceRangeInput, method=\"filter_initial_balance\"\n    )\n    is_active = django_filters.BooleanFilter()\n    code = django_filters.CharFilter(method=filter_code)\n    created_by_email = django_filters.CharFilter(method=filter_created_by_email)\n\n    class Meta:\n        model = models.GiftCard\n        fields = [\"is_active\"]\n\n    def filter_current_balance(self, queryset, name, value):\n        check_currency_in_filter_data(self.data)\n        return _filter_by_price(queryset, \"current_balance\", value)\n\n    def filter_initial_balance(self, queryset, name, value):\n        check_currency_in_filter_data(self.data)\n        return _filter_by_price(queryset, \"initial_balance\", value)\n\n\ndef check_currency_in_filter_data(filter_data: dict):\n    currency = filter_data.get(\"currency\")\n    if not currency:\n        raise GraphQLError(\n            \"You must provide a `currency` filter parameter for filtering by price.\"\n        )\n\n\nclass GiftCardFilterInput(FilterInputObjectType):\n    class Meta:\n        doc_category = DOC_CATEGORY_GIFT_CARDS\n        filterset_class = GiftCardFilter\n\n\ndef filter_events_by_type(events: List[models.GiftCardEvent], type_value: str):\n    filtered_events = []\n    for event in events:\n        if event.type == type_value:\n            filtered_events.append(event)\n    return filtered_events\n\n\ndef filter_events_by_orders(events: List[models.GiftCardEvent], order_ids: List[str]):\n    order_pks = _get_order_pks(order_ids)\n\n    filtered_events = []\n    for event in events:\n        if event.order_id in order_pks:\n            filtered_events.append(event)\n    return filtered_events\n\n\ndef _get_order_pks(order_ids: List[str]):\n    _, order_pks = resolve_global_ids_to_primary_keys(order_ids, \"Order\")\n\n    pks = []\n    old_pks = []\n    for pk in order_pks:\n        try:\n            pks.append(UUID(pk))\n        except ValueError:\n            old_pks.append(pk)\n\n    return order_models.Order.objects.filter(\n        Q(id__in=pks) | (Q(use_old_id=True) & Q(number__in=old_pks))\n    ).values_list(\"id\", flat=True)\n\n\nclass GiftCardEventFilterInput(BaseInputObjectType):\n    type = graphene.Argument(GiftCardEventsEnum)\n    orders = NonNullList(graphene.ID)\n\n    class Meta:\n        doc_category = DOC_CATEGORY_GIFT_CARDS\n\n\ndef filter_gift_card_tag_search(qs, _, value):\n    if not value:\n        return qs\n    return qs.filter(name__ilike=value)\n\n\nclass GiftCardTagFilter(django_filters.FilterSet):\n    search = django_filters.CharFilter(method=filter_gift_card_tag_search)\n\n\nclass GiftCardTagFilterInput(FilterInputObjectType):\n    class Meta:\n        doc_category = DOC_CATEGORY_GIFT_CARDS\n        filterset_class = GiftCardTagFilter\n", "path": "saleor/graphql/giftcard/filters.py"}]}