We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Fetch user address by its ID
### What I'm trying to achieve
I need to create a checkout in the saleor using a separate service that uses a service account. To do that I need to provide billing and shipping addresses, both of which I store in the saleor in user's addresses and store only IDs on my side.
There is currently no way to fetch a single address by its ID using a query.

### Describe a proposed solution
Add query:
`address(id: ID!): Address`
that is accessible for at least a service account, preferably by address owner too. 



--- 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/account/resolvers.py`
Content:
```
1 from itertools import chain
2 from typing import Optional
3 
4 import graphene
5 import graphene_django_optimizer as gql_optimizer
6 from django.db.models import QuerySet
7 from graphql_jwt.exceptions import PermissionDenied
8 from i18naddress import get_validation_rules
9 
10 from ...account import models
11 from ...core.permissions import AccountPermissions
12 from ...payment import gateway
13 from ...payment.utils import fetch_customer_id
14 from ..utils import filter_by_query_param, sort_queryset
15 from .sorters import ServiceAccountSortField, UserSortField, UserSortingInput
16 from .types import AddressValidationData, ChoiceValue
17 from .utils import get_allowed_fields_camel_case, get_required_fields_camel_case
18 
19 USER_SEARCH_FIELDS = (
20     "email",
21     "first_name",
22     "last_name",
23     "default_shipping_address__first_name",
24     "default_shipping_address__last_name",
25     "default_shipping_address__city",
26     "default_shipping_address__country",
27 )
28 
29 
30 def sort_users(qs: QuerySet, sort_by: UserSortingInput) -> QuerySet:
31     if sort_by:
32         return sort_queryset(qs, sort_by, UserSortField)
33     return qs.order_by("email")
34 
35 
36 def resolve_customers(info, query, sort_by=None, **_kwargs):
37     qs = models.User.objects.customers()
38     qs = filter_by_query_param(
39         queryset=qs, query=query, search_fields=USER_SEARCH_FIELDS
40     )
41     qs = sort_users(qs, sort_by)
42     qs = qs.distinct()
43     return gql_optimizer.query(qs, info)
44 
45 
46 def resolve_staff_users(info, query, sort_by=None, **_kwargs):
47     qs = models.User.objects.staff()
48     qs = filter_by_query_param(
49         queryset=qs, query=query, search_fields=USER_SEARCH_FIELDS
50     )
51     qs = sort_users(qs, sort_by)
52     qs = qs.distinct()
53     return gql_optimizer.query(qs, info)
54 
55 
56 def resolve_user(info, id):
57     requester = info.context.user or info.context.service_account
58     if requester:
59         _model, user_pk = graphene.Node.from_global_id(id)
60         if requester.has_perms(
61             [AccountPermissions.MANAGE_STAFF, AccountPermissions.MANAGE_USERS]
62         ):
63             return models.User.objects.filter(pk=user_pk).first()
64         if requester.has_perm(AccountPermissions.MANAGE_STAFF):
65             return models.User.objects.staff().filter(pk=user_pk).first()
66         if requester.has_perm(AccountPermissions.MANAGE_USERS):
67             return models.User.objects.customers().filter(pk=user_pk).first()
68     return PermissionDenied()
69 
70 
71 def resolve_service_accounts(info, sort_by=None, **_kwargs):
72     qs = models.ServiceAccount.objects.all()
73     qs = sort_queryset(qs, sort_by, ServiceAccountSortField)
74     return gql_optimizer.query(qs, info)
75 
76 
77 def resolve_address_validation_rules(
78     info,
79     country_code: str,
80     country_area: Optional[str],
81     city: Optional[str],
82     city_area: Optional[str],
83 ):
84 
85     params = {
86         "country_code": country_code,
87         "country_area": country_area,
88         "city": city,
89         "city_area": city_area,
90     }
91     rules = get_validation_rules(params)
92     return AddressValidationData(
93         country_code=rules.country_code,
94         country_name=rules.country_name,
95         address_format=rules.address_format,
96         address_latin_format=rules.address_latin_format,
97         allowed_fields=get_allowed_fields_camel_case(rules.allowed_fields),
98         required_fields=get_required_fields_camel_case(rules.required_fields),
99         upper_fields=rules.upper_fields,
100         country_area_type=rules.country_area_type,
101         country_area_choices=[
102             ChoiceValue(area[0], area[1]) for area in rules.country_area_choices
103         ],
104         city_type=rules.city_type,
105         city_choices=[ChoiceValue(area[0], area[1]) for area in rules.city_choices],
106         city_area_type=rules.city_type,
107         city_area_choices=[
108             ChoiceValue(area[0], area[1]) for area in rules.city_area_choices
109         ],
110         postal_code_type=rules.postal_code_type,
111         postal_code_matchers=[
112             compiled.pattern for compiled in rules.postal_code_matchers
113         ],
114         postal_code_examples=rules.postal_code_examples,
115         postal_code_prefix=rules.postal_code_prefix,
116     )
117 
118 
119 def resolve_payment_sources(user: models.User):
120     stored_customer_accounts = (
121         (gtw["name"], fetch_customer_id(user, gtw["name"]))
122         for gtw in gateway.list_gateways()
123     )
124     return list(
125         chain(
126             *[
127                 prepare_graphql_payment_sources_type(
128                     gateway.list_payment_sources(gtw, customer_id)
129                 )
130                 for gtw, customer_id in stored_customer_accounts
131                 if customer_id is not None
132             ]
133         )
134     )
135 
136 
137 def prepare_graphql_payment_sources_type(payment_sources):
138     sources = []
139     for src in payment_sources:
140         sources.append(
141             {
142                 "gateway": src.gateway,
143                 "credit_card_info": {
144                     "last_digits": src.credit_card_info.last_4,
145                     "exp_year": src.credit_card_info.exp_year,
146                     "exp_month": src.credit_card_info.exp_month,
147                     "brand": "",
148                     "first_digits": "",
149                 },
150             }
151         )
152     return sources
153
```

Path: `saleor/graphql/account/schema.py`
Content:
```
1 import graphene
2 from graphql_jwt.decorators import login_required
3 
4 from ...core.permissions import AccountPermissions
5 from ..core.fields import FilterInputConnectionField
6 from ..core.types import FilterInputObjectType
7 from ..decorators import one_of_permissions_required, permission_required
8 from .bulk_mutations import CustomerBulkDelete, StaffBulkDelete, UserBulkSetActive
9 from .enums import CountryCodeEnum
10 from .filters import CustomerFilter, ServiceAccountFilter, StaffUserFilter
11 from .mutations.account import (
12     AccountAddressCreate,
13     AccountAddressDelete,
14     AccountAddressUpdate,
15     AccountDelete,
16     AccountRegister,
17     AccountRequestDeletion,
18     AccountSetDefaultAddress,
19     AccountUpdate,
20     AccountUpdateMeta,
21 )
22 from .mutations.base import (
23     PasswordChange,
24     RequestPasswordReset,
25     SetPassword,
26     UserClearMeta,
27     UserUpdateMeta,
28 )
29 from .mutations.service_account import (
30     ServiceAccountClearPrivateMeta,
31     ServiceAccountCreate,
32     ServiceAccountDelete,
33     ServiceAccountTokenCreate,
34     ServiceAccountTokenDelete,
35     ServiceAccountUpdate,
36     ServiceAccountUpdatePrivateMeta,
37 )
38 from .mutations.staff import (
39     AddressCreate,
40     AddressDelete,
41     AddressSetDefault,
42     AddressUpdate,
43     CustomerCreate,
44     CustomerDelete,
45     CustomerUpdate,
46     StaffCreate,
47     StaffDelete,
48     StaffUpdate,
49     UserAvatarDelete,
50     UserAvatarUpdate,
51     UserClearPrivateMeta,
52     UserUpdatePrivateMeta,
53 )
54 from .resolvers import (
55     resolve_address_validation_rules,
56     resolve_customers,
57     resolve_service_accounts,
58     resolve_staff_users,
59     resolve_user,
60 )
61 from .sorters import ServiceAccountSortingInput, UserSortingInput
62 from .types import AddressValidationData, ServiceAccount, User
63 
64 
65 class CustomerFilterInput(FilterInputObjectType):
66     class Meta:
67         filterset_class = CustomerFilter
68 
69 
70 class StaffUserInput(FilterInputObjectType):
71     class Meta:
72         filterset_class = StaffUserFilter
73 
74 
75 class ServiceAccountFilterInput(FilterInputObjectType):
76     class Meta:
77         filterset_class = ServiceAccountFilter
78 
79 
80 class AccountQueries(graphene.ObjectType):
81     address_validation_rules = graphene.Field(
82         AddressValidationData,
83         description="Returns address validation rules.",
84         country_code=graphene.Argument(
85             CountryCodeEnum,
86             description="Two-letter ISO 3166-1 country code.",
87             required=True,
88         ),
89         country_area=graphene.Argument(
90             graphene.String, description="Designation of a region, province or state."
91         ),
92         city=graphene.Argument(graphene.String, description="City or a town name."),
93         city_area=graphene.Argument(
94             graphene.String, description="Sublocality like a district."
95         ),
96     )
97     customers = FilterInputConnectionField(
98         User,
99         filter=CustomerFilterInput(description="Filtering options for customers."),
100         sort_by=UserSortingInput(description="Sort customers."),
101         description="List of the shop's customers.",
102     )
103     me = graphene.Field(User, description="Return the currently authenticated user.")
104     staff_users = FilterInputConnectionField(
105         User,
106         filter=StaffUserInput(description="Filtering options for staff users."),
107         sort_by=UserSortingInput(description="Sort staff users."),
108         description="List of the shop's staff users.",
109     )
110     service_accounts = FilterInputConnectionField(
111         ServiceAccount,
112         filter=ServiceAccountFilterInput(
113             description="Filtering options for service accounts."
114         ),
115         sort_by=ServiceAccountSortingInput(description="Sort service accounts."),
116         description="List of the service accounts.",
117     )
118     service_account = graphene.Field(
119         ServiceAccount,
120         id=graphene.Argument(
121             graphene.ID, description="ID of the service account.", required=True
122         ),
123         description="Look up a service account by ID.",
124     )
125 
126     user = graphene.Field(
127         User,
128         id=graphene.Argument(graphene.ID, description="ID of the user.", required=True),
129         description="Look up a user by ID.",
130     )
131 
132     def resolve_address_validation_rules(
133         self, info, country_code, country_area=None, city=None, city_area=None
134     ):
135         return resolve_address_validation_rules(
136             info,
137             country_code,
138             country_area=country_area,
139             city=city,
140             city_area=city_area,
141         )
142 
143     @permission_required(AccountPermissions.MANAGE_SERVICE_ACCOUNTS)
144     def resolve_service_accounts(self, info, **kwargs):
145         return resolve_service_accounts(info, **kwargs)
146 
147     @permission_required(AccountPermissions.MANAGE_SERVICE_ACCOUNTS)
148     def resolve_service_account(self, info, id):
149         return graphene.Node.get_node_from_global_id(info, id, ServiceAccount)
150 
151     @permission_required(AccountPermissions.MANAGE_USERS)
152     def resolve_customers(self, info, query=None, **kwargs):
153         return resolve_customers(info, query=query, **kwargs)
154 
155     @login_required
156     def resolve_me(self, info):
157         return info.context.user
158 
159     @permission_required(AccountPermissions.MANAGE_STAFF)
160     def resolve_staff_users(self, info, query=None, **kwargs):
161         return resolve_staff_users(info, query=query, **kwargs)
162 
163     @one_of_permissions_required(
164         [AccountPermissions.MANAGE_STAFF, AccountPermissions.MANAGE_USERS]
165     )
166     def resolve_user(self, info, id):
167         return resolve_user(info, id)
168 
169 
170 class AccountMutations(graphene.ObjectType):
171     # Base mutations
172     request_password_reset = RequestPasswordReset.Field()
173     set_password = SetPassword.Field()
174     password_change = PasswordChange.Field()
175 
176     # Account mutations
177     account_address_create = AccountAddressCreate.Field()
178     account_address_update = AccountAddressUpdate.Field()
179     account_address_delete = AccountAddressDelete.Field()
180     account_set_default_address = AccountSetDefaultAddress.Field()
181 
182     account_register = AccountRegister.Field()
183     account_update = AccountUpdate.Field()
184     account_request_deletion = AccountRequestDeletion.Field()
185     account_delete = AccountDelete.Field()
186 
187     account_update_meta = AccountUpdateMeta.Field()
188 
189     # Staff mutation
190     address_create = AddressCreate.Field()
191     address_update = AddressUpdate.Field()
192     address_delete = AddressDelete.Field()
193     address_set_default = AddressSetDefault.Field()
194 
195     customer_create = CustomerCreate.Field()
196     customer_update = CustomerUpdate.Field()
197     customer_delete = CustomerDelete.Field()
198     customer_bulk_delete = CustomerBulkDelete.Field()
199 
200     staff_create = StaffCreate.Field()
201     staff_update = StaffUpdate.Field()
202     staff_delete = StaffDelete.Field()
203     staff_bulk_delete = StaffBulkDelete.Field()
204 
205     user_avatar_update = UserAvatarUpdate.Field()
206     user_avatar_delete = UserAvatarDelete.Field()
207     user_bulk_set_active = UserBulkSetActive.Field()
208 
209     user_update_metadata = UserUpdateMeta.Field()
210     user_clear_metadata = UserClearMeta.Field()
211 
212     user_update_private_metadata = UserUpdatePrivateMeta.Field()
213     user_clear_private_metadata = UserClearPrivateMeta.Field()
214 
215     service_account_create = ServiceAccountCreate.Field()
216     service_account_update = ServiceAccountUpdate.Field()
217     service_account_delete = ServiceAccountDelete.Field()
218 
219     service_account_update_private_metadata = ServiceAccountUpdatePrivateMeta.Field()
220     service_account_clear_private_metadata = ServiceAccountClearPrivateMeta.Field()
221 
222     service_account_token_create = ServiceAccountTokenCreate.Field()
223     service_account_token_delete = ServiceAccountTokenDelete.Field()
224
```
--- 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()

```

diff --git a/saleor/graphql/account/resolvers.py b/saleor/graphql/account/resolvers.py
--- a/saleor/graphql/account/resolvers.py
+++ b/saleor/graphql/account/resolvers.py
@@ -150,3 +150,14 @@
             }
         )
     return sources
+
+
+def resolve_address(info, id):
+    user = info.context.user
+    service_account = info.context.service_account
+    _model, address_pk = graphene.Node.from_global_id(id)
+    if service_account:
+        return models.Address.objects.filter(pk=address_pk).first()
+    if user and not user.is_anonymous:
+        return user.addresses.filter(id=address_pk).first()
+    return PermissionDenied()
diff --git a/saleor/graphql/account/schema.py b/saleor/graphql/account/schema.py
--- a/saleor/graphql/account/schema.py
+++ b/saleor/graphql/account/schema.py
@@ -52,6 +52,7 @@
     UserUpdatePrivateMeta,
 )
 from .resolvers import (
+    resolve_address,
     resolve_address_validation_rules,
     resolve_customers,
     resolve_service_accounts,
@@ -59,7 +60,7 @@
     resolve_user,
 )
 from .sorters import ServiceAccountSortingInput, UserSortingInput
-from .types import AddressValidationData, ServiceAccount, User
+from .types import Address, AddressValidationData, ServiceAccount, User
 
 
 class CustomerFilterInput(FilterInputObjectType):
@@ -94,6 +95,13 @@
             graphene.String, description="Sublocality like a district."
         ),
     )
+    address = graphene.Field(
+        Address,
+        id=graphene.Argument(
+            graphene.ID, description="ID of an address.", required=True
+        ),
+        description="Look up an address by ID.",
+    )
     customers = FilterInputConnectionField(
         User,
         filter=CustomerFilterInput(description="Filtering options for customers."),
@@ -166,6 +174,9 @@
     def resolve_user(self, info, id):
         return resolve_user(info, id)
 
+    def resolve_address(self, info, id):
+        return resolve_address(info, id)
+
 
 class AccountMutations(graphene.ObjectType):
     # Base mutations

{"golden_diff": "diff --git a/saleor/graphql/account/resolvers.py b/saleor/graphql/account/resolvers.py\n--- a/saleor/graphql/account/resolvers.py\n+++ b/saleor/graphql/account/resolvers.py\n@@ -150,3 +150,14 @@\n             }\n         )\n     return sources\n+\n+\n+def resolve_address(info, id):\n+    user = info.context.user\n+    service_account = info.context.service_account\n+    _model, address_pk = graphene.Node.from_global_id(id)\n+    if service_account:\n+        return models.Address.objects.filter(pk=address_pk).first()\n+    if user and not user.is_anonymous:\n+        return user.addresses.filter(id=address_pk).first()\n+    return PermissionDenied()\ndiff --git a/saleor/graphql/account/schema.py b/saleor/graphql/account/schema.py\n--- a/saleor/graphql/account/schema.py\n+++ b/saleor/graphql/account/schema.py\n@@ -52,6 +52,7 @@\n     UserUpdatePrivateMeta,\n )\n from .resolvers import (\n+    resolve_address,\n     resolve_address_validation_rules,\n     resolve_customers,\n     resolve_service_accounts,\n@@ -59,7 +60,7 @@\n     resolve_user,\n )\n from .sorters import ServiceAccountSortingInput, UserSortingInput\n-from .types import AddressValidationData, ServiceAccount, User\n+from .types import Address, AddressValidationData, ServiceAccount, User\n \n \n class CustomerFilterInput(FilterInputObjectType):\n@@ -94,6 +95,13 @@\n             graphene.String, description=\"Sublocality like a district.\"\n         ),\n     )\n+    address = graphene.Field(\n+        Address,\n+        id=graphene.Argument(\n+            graphene.ID, description=\"ID of an address.\", required=True\n+        ),\n+        description=\"Look up an address by ID.\",\n+    )\n     customers = FilterInputConnectionField(\n         User,\n         filter=CustomerFilterInput(description=\"Filtering options for customers.\"),\n@@ -166,6 +174,9 @@\n     def resolve_user(self, info, id):\n         return resolve_user(info, id)\n \n+    def resolve_address(self, info, id):\n+        return resolve_address(info, id)\n+\n \n class AccountMutations(graphene.ObjectType):\n     # Base mutations\n", "issue": "Fetch user address by its ID\n### What I'm trying to achieve\r\nI need to create a checkout in the saleor using a separate service that uses a service account. To do that I need to provide billing and shipping addresses, both of which I store in the saleor in user's addresses and store only IDs on my side.\r\nThere is currently no way to fetch a single address by its ID using a query.\r\n\r\n### Describe a proposed solution\r\nAdd query:\r\n`address(id: ID!): Address`\r\nthat is accessible for at least a service account, preferably by address owner too. \r\n\r\n\n", "before_files": [{"content": "from itertools import chain\nfrom typing import Optional\n\nimport graphene\nimport graphene_django_optimizer as gql_optimizer\nfrom django.db.models import QuerySet\nfrom graphql_jwt.exceptions import PermissionDenied\nfrom i18naddress import get_validation_rules\n\nfrom ...account import models\nfrom ...core.permissions import AccountPermissions\nfrom ...payment import gateway\nfrom ...payment.utils import fetch_customer_id\nfrom ..utils import filter_by_query_param, sort_queryset\nfrom .sorters import ServiceAccountSortField, UserSortField, UserSortingInput\nfrom .types import AddressValidationData, ChoiceValue\nfrom .utils import get_allowed_fields_camel_case, get_required_fields_camel_case\n\nUSER_SEARCH_FIELDS = (\n    \"email\",\n    \"first_name\",\n    \"last_name\",\n    \"default_shipping_address__first_name\",\n    \"default_shipping_address__last_name\",\n    \"default_shipping_address__city\",\n    \"default_shipping_address__country\",\n)\n\n\ndef sort_users(qs: QuerySet, sort_by: UserSortingInput) -> QuerySet:\n    if sort_by:\n        return sort_queryset(qs, sort_by, UserSortField)\n    return qs.order_by(\"email\")\n\n\ndef resolve_customers(info, query, sort_by=None, **_kwargs):\n    qs = models.User.objects.customers()\n    qs = filter_by_query_param(\n        queryset=qs, query=query, search_fields=USER_SEARCH_FIELDS\n    )\n    qs = sort_users(qs, sort_by)\n    qs = qs.distinct()\n    return gql_optimizer.query(qs, info)\n\n\ndef resolve_staff_users(info, query, sort_by=None, **_kwargs):\n    qs = models.User.objects.staff()\n    qs = filter_by_query_param(\n        queryset=qs, query=query, search_fields=USER_SEARCH_FIELDS\n    )\n    qs = sort_users(qs, sort_by)\n    qs = qs.distinct()\n    return gql_optimizer.query(qs, info)\n\n\ndef resolve_user(info, id):\n    requester = info.context.user or info.context.service_account\n    if requester:\n        _model, user_pk = graphene.Node.from_global_id(id)\n        if requester.has_perms(\n            [AccountPermissions.MANAGE_STAFF, AccountPermissions.MANAGE_USERS]\n        ):\n            return models.User.objects.filter(pk=user_pk).first()\n        if requester.has_perm(AccountPermissions.MANAGE_STAFF):\n            return models.User.objects.staff().filter(pk=user_pk).first()\n        if requester.has_perm(AccountPermissions.MANAGE_USERS):\n            return models.User.objects.customers().filter(pk=user_pk).first()\n    return PermissionDenied()\n\n\ndef resolve_service_accounts(info, sort_by=None, **_kwargs):\n    qs = models.ServiceAccount.objects.all()\n    qs = sort_queryset(qs, sort_by, ServiceAccountSortField)\n    return gql_optimizer.query(qs, info)\n\n\ndef resolve_address_validation_rules(\n    info,\n    country_code: str,\n    country_area: Optional[str],\n    city: Optional[str],\n    city_area: Optional[str],\n):\n\n    params = {\n        \"country_code\": country_code,\n        \"country_area\": country_area,\n        \"city\": city,\n        \"city_area\": city_area,\n    }\n    rules = get_validation_rules(params)\n    return AddressValidationData(\n        country_code=rules.country_code,\n        country_name=rules.country_name,\n        address_format=rules.address_format,\n        address_latin_format=rules.address_latin_format,\n        allowed_fields=get_allowed_fields_camel_case(rules.allowed_fields),\n        required_fields=get_required_fields_camel_case(rules.required_fields),\n        upper_fields=rules.upper_fields,\n        country_area_type=rules.country_area_type,\n        country_area_choices=[\n            ChoiceValue(area[0], area[1]) for area in rules.country_area_choices\n        ],\n        city_type=rules.city_type,\n        city_choices=[ChoiceValue(area[0], area[1]) for area in rules.city_choices],\n        city_area_type=rules.city_type,\n        city_area_choices=[\n            ChoiceValue(area[0], area[1]) for area in rules.city_area_choices\n        ],\n        postal_code_type=rules.postal_code_type,\n        postal_code_matchers=[\n            compiled.pattern for compiled in rules.postal_code_matchers\n        ],\n        postal_code_examples=rules.postal_code_examples,\n        postal_code_prefix=rules.postal_code_prefix,\n    )\n\n\ndef resolve_payment_sources(user: models.User):\n    stored_customer_accounts = (\n        (gtw[\"name\"], fetch_customer_id(user, gtw[\"name\"]))\n        for gtw in gateway.list_gateways()\n    )\n    return list(\n        chain(\n            *[\n                prepare_graphql_payment_sources_type(\n                    gateway.list_payment_sources(gtw, customer_id)\n                )\n                for gtw, customer_id in stored_customer_accounts\n                if customer_id is not None\n            ]\n        )\n    )\n\n\ndef prepare_graphql_payment_sources_type(payment_sources):\n    sources = []\n    for src in payment_sources:\n        sources.append(\n            {\n                \"gateway\": src.gateway,\n                \"credit_card_info\": {\n                    \"last_digits\": src.credit_card_info.last_4,\n                    \"exp_year\": src.credit_card_info.exp_year,\n                    \"exp_month\": src.credit_card_info.exp_month,\n                    \"brand\": \"\",\n                    \"first_digits\": \"\",\n                },\n            }\n        )\n    return sources\n", "path": "saleor/graphql/account/resolvers.py"}, {"content": "import graphene\nfrom graphql_jwt.decorators import login_required\n\nfrom ...core.permissions import AccountPermissions\nfrom ..core.fields import FilterInputConnectionField\nfrom ..core.types import FilterInputObjectType\nfrom ..decorators import one_of_permissions_required, permission_required\nfrom .bulk_mutations import CustomerBulkDelete, StaffBulkDelete, UserBulkSetActive\nfrom .enums import CountryCodeEnum\nfrom .filters import CustomerFilter, ServiceAccountFilter, StaffUserFilter\nfrom .mutations.account import (\n    AccountAddressCreate,\n    AccountAddressDelete,\n    AccountAddressUpdate,\n    AccountDelete,\n    AccountRegister,\n    AccountRequestDeletion,\n    AccountSetDefaultAddress,\n    AccountUpdate,\n    AccountUpdateMeta,\n)\nfrom .mutations.base import (\n    PasswordChange,\n    RequestPasswordReset,\n    SetPassword,\n    UserClearMeta,\n    UserUpdateMeta,\n)\nfrom .mutations.service_account import (\n    ServiceAccountClearPrivateMeta,\n    ServiceAccountCreate,\n    ServiceAccountDelete,\n    ServiceAccountTokenCreate,\n    ServiceAccountTokenDelete,\n    ServiceAccountUpdate,\n    ServiceAccountUpdatePrivateMeta,\n)\nfrom .mutations.staff import (\n    AddressCreate,\n    AddressDelete,\n    AddressSetDefault,\n    AddressUpdate,\n    CustomerCreate,\n    CustomerDelete,\n    CustomerUpdate,\n    StaffCreate,\n    StaffDelete,\n    StaffUpdate,\n    UserAvatarDelete,\n    UserAvatarUpdate,\n    UserClearPrivateMeta,\n    UserUpdatePrivateMeta,\n)\nfrom .resolvers import (\n    resolve_address_validation_rules,\n    resolve_customers,\n    resolve_service_accounts,\n    resolve_staff_users,\n    resolve_user,\n)\nfrom .sorters import ServiceAccountSortingInput, UserSortingInput\nfrom .types import AddressValidationData, ServiceAccount, User\n\n\nclass CustomerFilterInput(FilterInputObjectType):\n    class Meta:\n        filterset_class = CustomerFilter\n\n\nclass StaffUserInput(FilterInputObjectType):\n    class Meta:\n        filterset_class = StaffUserFilter\n\n\nclass ServiceAccountFilterInput(FilterInputObjectType):\n    class Meta:\n        filterset_class = ServiceAccountFilter\n\n\nclass AccountQueries(graphene.ObjectType):\n    address_validation_rules = graphene.Field(\n        AddressValidationData,\n        description=\"Returns address validation rules.\",\n        country_code=graphene.Argument(\n            CountryCodeEnum,\n            description=\"Two-letter ISO 3166-1 country code.\",\n            required=True,\n        ),\n        country_area=graphene.Argument(\n            graphene.String, description=\"Designation of a region, province or state.\"\n        ),\n        city=graphene.Argument(graphene.String, description=\"City or a town name.\"),\n        city_area=graphene.Argument(\n            graphene.String, description=\"Sublocality like a district.\"\n        ),\n    )\n    customers = FilterInputConnectionField(\n        User,\n        filter=CustomerFilterInput(description=\"Filtering options for customers.\"),\n        sort_by=UserSortingInput(description=\"Sort customers.\"),\n        description=\"List of the shop's customers.\",\n    )\n    me = graphene.Field(User, description=\"Return the currently authenticated user.\")\n    staff_users = FilterInputConnectionField(\n        User,\n        filter=StaffUserInput(description=\"Filtering options for staff users.\"),\n        sort_by=UserSortingInput(description=\"Sort staff users.\"),\n        description=\"List of the shop's staff users.\",\n    )\n    service_accounts = FilterInputConnectionField(\n        ServiceAccount,\n        filter=ServiceAccountFilterInput(\n            description=\"Filtering options for service accounts.\"\n        ),\n        sort_by=ServiceAccountSortingInput(description=\"Sort service accounts.\"),\n        description=\"List of the service accounts.\",\n    )\n    service_account = graphene.Field(\n        ServiceAccount,\n        id=graphene.Argument(\n            graphene.ID, description=\"ID of the service account.\", required=True\n        ),\n        description=\"Look up a service account by ID.\",\n    )\n\n    user = graphene.Field(\n        User,\n        id=graphene.Argument(graphene.ID, description=\"ID of the user.\", required=True),\n        description=\"Look up a user by ID.\",\n    )\n\n    def resolve_address_validation_rules(\n        self, info, country_code, country_area=None, city=None, city_area=None\n    ):\n        return resolve_address_validation_rules(\n            info,\n            country_code,\n            country_area=country_area,\n            city=city,\n            city_area=city_area,\n        )\n\n    @permission_required(AccountPermissions.MANAGE_SERVICE_ACCOUNTS)\n    def resolve_service_accounts(self, info, **kwargs):\n        return resolve_service_accounts(info, **kwargs)\n\n    @permission_required(AccountPermissions.MANAGE_SERVICE_ACCOUNTS)\n    def resolve_service_account(self, info, id):\n        return graphene.Node.get_node_from_global_id(info, id, ServiceAccount)\n\n    @permission_required(AccountPermissions.MANAGE_USERS)\n    def resolve_customers(self, info, query=None, **kwargs):\n        return resolve_customers(info, query=query, **kwargs)\n\n    @login_required\n    def resolve_me(self, info):\n        return info.context.user\n\n    @permission_required(AccountPermissions.MANAGE_STAFF)\n    def resolve_staff_users(self, info, query=None, **kwargs):\n        return resolve_staff_users(info, query=query, **kwargs)\n\n    @one_of_permissions_required(\n        [AccountPermissions.MANAGE_STAFF, AccountPermissions.MANAGE_USERS]\n    )\n    def resolve_user(self, info, id):\n        return resolve_user(info, id)\n\n\nclass AccountMutations(graphene.ObjectType):\n    # Base mutations\n    request_password_reset = RequestPasswordReset.Field()\n    set_password = SetPassword.Field()\n    password_change = PasswordChange.Field()\n\n    # Account mutations\n    account_address_create = AccountAddressCreate.Field()\n    account_address_update = AccountAddressUpdate.Field()\n    account_address_delete = AccountAddressDelete.Field()\n    account_set_default_address = AccountSetDefaultAddress.Field()\n\n    account_register = AccountRegister.Field()\n    account_update = AccountUpdate.Field()\n    account_request_deletion = AccountRequestDeletion.Field()\n    account_delete = AccountDelete.Field()\n\n    account_update_meta = AccountUpdateMeta.Field()\n\n    # Staff mutation\n    address_create = AddressCreate.Field()\n    address_update = AddressUpdate.Field()\n    address_delete = AddressDelete.Field()\n    address_set_default = AddressSetDefault.Field()\n\n    customer_create = CustomerCreate.Field()\n    customer_update = CustomerUpdate.Field()\n    customer_delete = CustomerDelete.Field()\n    customer_bulk_delete = CustomerBulkDelete.Field()\n\n    staff_create = StaffCreate.Field()\n    staff_update = StaffUpdate.Field()\n    staff_delete = StaffDelete.Field()\n    staff_bulk_delete = StaffBulkDelete.Field()\n\n    user_avatar_update = UserAvatarUpdate.Field()\n    user_avatar_delete = UserAvatarDelete.Field()\n    user_bulk_set_active = UserBulkSetActive.Field()\n\n    user_update_metadata = UserUpdateMeta.Field()\n    user_clear_metadata = UserClearMeta.Field()\n\n    user_update_private_metadata = UserUpdatePrivateMeta.Field()\n    user_clear_private_metadata = UserClearPrivateMeta.Field()\n\n    service_account_create = ServiceAccountCreate.Field()\n    service_account_update = ServiceAccountUpdate.Field()\n    service_account_delete = ServiceAccountDelete.Field()\n\n    service_account_update_private_metadata = ServiceAccountUpdatePrivateMeta.Field()\n    service_account_clear_private_metadata = ServiceAccountClearPrivateMeta.Field()\n\n    service_account_token_create = ServiceAccountTokenCreate.Field()\n    service_account_token_delete = ServiceAccountTokenDelete.Field()\n", "path": "saleor/graphql/account/schema.py"}], "after_files": [{"content": "from itertools import chain\nfrom typing import Optional\n\nimport graphene\nimport graphene_django_optimizer as gql_optimizer\nfrom django.db.models import QuerySet\nfrom graphql_jwt.exceptions import PermissionDenied\nfrom i18naddress import get_validation_rules\n\nfrom ...account import models\nfrom ...core.permissions import AccountPermissions\nfrom ...payment import gateway\nfrom ...payment.utils import fetch_customer_id\nfrom ..utils import filter_by_query_param, sort_queryset\nfrom .sorters import ServiceAccountSortField, UserSortField, UserSortingInput\nfrom .types import AddressValidationData, ChoiceValue\nfrom .utils import get_allowed_fields_camel_case, get_required_fields_camel_case\n\nUSER_SEARCH_FIELDS = (\n    \"email\",\n    \"first_name\",\n    \"last_name\",\n    \"default_shipping_address__first_name\",\n    \"default_shipping_address__last_name\",\n    \"default_shipping_address__city\",\n    \"default_shipping_address__country\",\n)\n\n\ndef sort_users(qs: QuerySet, sort_by: UserSortingInput) -> QuerySet:\n    if sort_by:\n        return sort_queryset(qs, sort_by, UserSortField)\n    return qs.order_by(\"email\")\n\n\ndef resolve_customers(info, query, sort_by=None, **_kwargs):\n    qs = models.User.objects.customers()\n    qs = filter_by_query_param(\n        queryset=qs, query=query, search_fields=USER_SEARCH_FIELDS\n    )\n    qs = sort_users(qs, sort_by)\n    qs = qs.distinct()\n    return gql_optimizer.query(qs, info)\n\n\ndef resolve_staff_users(info, query, sort_by=None, **_kwargs):\n    qs = models.User.objects.staff()\n    qs = filter_by_query_param(\n        queryset=qs, query=query, search_fields=USER_SEARCH_FIELDS\n    )\n    qs = sort_users(qs, sort_by)\n    qs = qs.distinct()\n    return gql_optimizer.query(qs, info)\n\n\ndef resolve_user(info, id):\n    requester = info.context.user or info.context.service_account\n    if requester:\n        _model, user_pk = graphene.Node.from_global_id(id)\n        if requester.has_perms(\n            [AccountPermissions.MANAGE_STAFF, AccountPermissions.MANAGE_USERS]\n        ):\n            return models.User.objects.filter(pk=user_pk).first()\n        if requester.has_perm(AccountPermissions.MANAGE_STAFF):\n            return models.User.objects.staff().filter(pk=user_pk).first()\n        if requester.has_perm(AccountPermissions.MANAGE_USERS):\n            return models.User.objects.customers().filter(pk=user_pk).first()\n    return PermissionDenied()\n\n\ndef resolve_service_accounts(info, sort_by=None, **_kwargs):\n    qs = models.ServiceAccount.objects.all()\n    qs = sort_queryset(qs, sort_by, ServiceAccountSortField)\n    return gql_optimizer.query(qs, info)\n\n\ndef resolve_address_validation_rules(\n    info,\n    country_code: str,\n    country_area: Optional[str],\n    city: Optional[str],\n    city_area: Optional[str],\n):\n\n    params = {\n        \"country_code\": country_code,\n        \"country_area\": country_area,\n        \"city\": city,\n        \"city_area\": city_area,\n    }\n    rules = get_validation_rules(params)\n    return AddressValidationData(\n        country_code=rules.country_code,\n        country_name=rules.country_name,\n        address_format=rules.address_format,\n        address_latin_format=rules.address_latin_format,\n        allowed_fields=get_allowed_fields_camel_case(rules.allowed_fields),\n        required_fields=get_required_fields_camel_case(rules.required_fields),\n        upper_fields=rules.upper_fields,\n        country_area_type=rules.country_area_type,\n        country_area_choices=[\n            ChoiceValue(area[0], area[1]) for area in rules.country_area_choices\n        ],\n        city_type=rules.city_type,\n        city_choices=[ChoiceValue(area[0], area[1]) for area in rules.city_choices],\n        city_area_type=rules.city_type,\n        city_area_choices=[\n            ChoiceValue(area[0], area[1]) for area in rules.city_area_choices\n        ],\n        postal_code_type=rules.postal_code_type,\n        postal_code_matchers=[\n            compiled.pattern for compiled in rules.postal_code_matchers\n        ],\n        postal_code_examples=rules.postal_code_examples,\n        postal_code_prefix=rules.postal_code_prefix,\n    )\n\n\ndef resolve_payment_sources(user: models.User):\n    stored_customer_accounts = (\n        (gtw[\"name\"], fetch_customer_id(user, gtw[\"name\"]))\n        for gtw in gateway.list_gateways()\n    )\n    return list(\n        chain(\n            *[\n                prepare_graphql_payment_sources_type(\n                    gateway.list_payment_sources(gtw, customer_id)\n                )\n                for gtw, customer_id in stored_customer_accounts\n                if customer_id is not None\n            ]\n        )\n    )\n\n\ndef prepare_graphql_payment_sources_type(payment_sources):\n    sources = []\n    for src in payment_sources:\n        sources.append(\n            {\n                \"gateway\": src.gateway,\n                \"credit_card_info\": {\n                    \"last_digits\": src.credit_card_info.last_4,\n                    \"exp_year\": src.credit_card_info.exp_year,\n                    \"exp_month\": src.credit_card_info.exp_month,\n                    \"brand\": \"\",\n                    \"first_digits\": \"\",\n                },\n            }\n        )\n    return sources\n\n\ndef resolve_address(info, id):\n    user = info.context.user\n    service_account = info.context.service_account\n    _model, address_pk = graphene.Node.from_global_id(id)\n    if service_account:\n        return models.Address.objects.filter(pk=address_pk).first()\n    if user and not user.is_anonymous:\n        return user.addresses.filter(id=address_pk).first()\n    return PermissionDenied()\n", "path": "saleor/graphql/account/resolvers.py"}, {"content": "import graphene\nfrom graphql_jwt.decorators import login_required\n\nfrom ...core.permissions import AccountPermissions\nfrom ..core.fields import FilterInputConnectionField\nfrom ..core.types import FilterInputObjectType\nfrom ..decorators import one_of_permissions_required, permission_required\nfrom .bulk_mutations import CustomerBulkDelete, StaffBulkDelete, UserBulkSetActive\nfrom .enums import CountryCodeEnum\nfrom .filters import CustomerFilter, ServiceAccountFilter, StaffUserFilter\nfrom .mutations.account import (\n    AccountAddressCreate,\n    AccountAddressDelete,\n    AccountAddressUpdate,\n    AccountDelete,\n    AccountRegister,\n    AccountRequestDeletion,\n    AccountSetDefaultAddress,\n    AccountUpdate,\n    AccountUpdateMeta,\n)\nfrom .mutations.base import (\n    PasswordChange,\n    RequestPasswordReset,\n    SetPassword,\n    UserClearMeta,\n    UserUpdateMeta,\n)\nfrom .mutations.service_account import (\n    ServiceAccountClearPrivateMeta,\n    ServiceAccountCreate,\n    ServiceAccountDelete,\n    ServiceAccountTokenCreate,\n    ServiceAccountTokenDelete,\n    ServiceAccountUpdate,\n    ServiceAccountUpdatePrivateMeta,\n)\nfrom .mutations.staff import (\n    AddressCreate,\n    AddressDelete,\n    AddressSetDefault,\n    AddressUpdate,\n    CustomerCreate,\n    CustomerDelete,\n    CustomerUpdate,\n    StaffCreate,\n    StaffDelete,\n    StaffUpdate,\n    UserAvatarDelete,\n    UserAvatarUpdate,\n    UserClearPrivateMeta,\n    UserUpdatePrivateMeta,\n)\nfrom .resolvers import (\n    resolve_address,\n    resolve_address_validation_rules,\n    resolve_customers,\n    resolve_service_accounts,\n    resolve_staff_users,\n    resolve_user,\n)\nfrom .sorters import ServiceAccountSortingInput, UserSortingInput\nfrom .types import Address, AddressValidationData, ServiceAccount, User\n\n\nclass CustomerFilterInput(FilterInputObjectType):\n    class Meta:\n        filterset_class = CustomerFilter\n\n\nclass StaffUserInput(FilterInputObjectType):\n    class Meta:\n        filterset_class = StaffUserFilter\n\n\nclass ServiceAccountFilterInput(FilterInputObjectType):\n    class Meta:\n        filterset_class = ServiceAccountFilter\n\n\nclass AccountQueries(graphene.ObjectType):\n    address_validation_rules = graphene.Field(\n        AddressValidationData,\n        description=\"Returns address validation rules.\",\n        country_code=graphene.Argument(\n            CountryCodeEnum,\n            description=\"Two-letter ISO 3166-1 country code.\",\n            required=True,\n        ),\n        country_area=graphene.Argument(\n            graphene.String, description=\"Designation of a region, province or state.\"\n        ),\n        city=graphene.Argument(graphene.String, description=\"City or a town name.\"),\n        city_area=graphene.Argument(\n            graphene.String, description=\"Sublocality like a district.\"\n        ),\n    )\n    address = graphene.Field(\n        Address,\n        id=graphene.Argument(\n            graphene.ID, description=\"ID of an address.\", required=True\n        ),\n        description=\"Look up an address by ID.\",\n    )\n    customers = FilterInputConnectionField(\n        User,\n        filter=CustomerFilterInput(description=\"Filtering options for customers.\"),\n        sort_by=UserSortingInput(description=\"Sort customers.\"),\n        description=\"List of the shop's customers.\",\n    )\n    me = graphene.Field(User, description=\"Return the currently authenticated user.\")\n    staff_users = FilterInputConnectionField(\n        User,\n        filter=StaffUserInput(description=\"Filtering options for staff users.\"),\n        sort_by=UserSortingInput(description=\"Sort staff users.\"),\n        description=\"List of the shop's staff users.\",\n    )\n    service_accounts = FilterInputConnectionField(\n        ServiceAccount,\n        filter=ServiceAccountFilterInput(\n            description=\"Filtering options for service accounts.\"\n        ),\n        sort_by=ServiceAccountSortingInput(description=\"Sort service accounts.\"),\n        description=\"List of the service accounts.\",\n    )\n    service_account = graphene.Field(\n        ServiceAccount,\n        id=graphene.Argument(\n            graphene.ID, description=\"ID of the service account.\", required=True\n        ),\n        description=\"Look up a service account by ID.\",\n    )\n\n    user = graphene.Field(\n        User,\n        id=graphene.Argument(graphene.ID, description=\"ID of the user.\", required=True),\n        description=\"Look up a user by ID.\",\n    )\n\n    def resolve_address_validation_rules(\n        self, info, country_code, country_area=None, city=None, city_area=None\n    ):\n        return resolve_address_validation_rules(\n            info,\n            country_code,\n            country_area=country_area,\n            city=city,\n            city_area=city_area,\n        )\n\n    @permission_required(AccountPermissions.MANAGE_SERVICE_ACCOUNTS)\n    def resolve_service_accounts(self, info, **kwargs):\n        return resolve_service_accounts(info, **kwargs)\n\n    @permission_required(AccountPermissions.MANAGE_SERVICE_ACCOUNTS)\n    def resolve_service_account(self, info, id):\n        return graphene.Node.get_node_from_global_id(info, id, ServiceAccount)\n\n    @permission_required(AccountPermissions.MANAGE_USERS)\n    def resolve_customers(self, info, query=None, **kwargs):\n        return resolve_customers(info, query=query, **kwargs)\n\n    @login_required\n    def resolve_me(self, info):\n        return info.context.user\n\n    @permission_required(AccountPermissions.MANAGE_STAFF)\n    def resolve_staff_users(self, info, query=None, **kwargs):\n        return resolve_staff_users(info, query=query, **kwargs)\n\n    @one_of_permissions_required(\n        [AccountPermissions.MANAGE_STAFF, AccountPermissions.MANAGE_USERS]\n    )\n    def resolve_user(self, info, id):\n        return resolve_user(info, id)\n\n    def resolve_address(self, info, id):\n        return resolve_address(info, id)\n\n\nclass AccountMutations(graphene.ObjectType):\n    # Base mutations\n    request_password_reset = RequestPasswordReset.Field()\n    set_password = SetPassword.Field()\n    password_change = PasswordChange.Field()\n\n    # Account mutations\n    account_address_create = AccountAddressCreate.Field()\n    account_address_update = AccountAddressUpdate.Field()\n    account_address_delete = AccountAddressDelete.Field()\n    account_set_default_address = AccountSetDefaultAddress.Field()\n\n    account_register = AccountRegister.Field()\n    account_update = AccountUpdate.Field()\n    account_request_deletion = AccountRequestDeletion.Field()\n    account_delete = AccountDelete.Field()\n\n    account_update_meta = AccountUpdateMeta.Field()\n\n    # Staff mutation\n    address_create = AddressCreate.Field()\n    address_update = AddressUpdate.Field()\n    address_delete = AddressDelete.Field()\n    address_set_default = AddressSetDefault.Field()\n\n    customer_create = CustomerCreate.Field()\n    customer_update = CustomerUpdate.Field()\n    customer_delete = CustomerDelete.Field()\n    customer_bulk_delete = CustomerBulkDelete.Field()\n\n    staff_create = StaffCreate.Field()\n    staff_update = StaffUpdate.Field()\n    staff_delete = StaffDelete.Field()\n    staff_bulk_delete = StaffBulkDelete.Field()\n\n    user_avatar_update = UserAvatarUpdate.Field()\n    user_avatar_delete = UserAvatarDelete.Field()\n    user_bulk_set_active = UserBulkSetActive.Field()\n\n    user_update_metadata = UserUpdateMeta.Field()\n    user_clear_metadata = UserClearMeta.Field()\n\n    user_update_private_metadata = UserUpdatePrivateMeta.Field()\n    user_clear_private_metadata = UserClearPrivateMeta.Field()\n\n    service_account_create = ServiceAccountCreate.Field()\n    service_account_update = ServiceAccountUpdate.Field()\n    service_account_delete = ServiceAccountDelete.Field()\n\n    service_account_update_private_metadata = ServiceAccountUpdatePrivateMeta.Field()\n    service_account_clear_private_metadata = ServiceAccountClearPrivateMeta.Field()\n\n    service_account_token_create = ServiceAccountTokenCreate.Field()\n    service_account_token_delete = ServiceAccountTokenDelete.Field()\n", "path": "saleor/graphql/account/schema.py"}]}