Skip to content

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

Add code to gift card query when gift card owner or staff

fowczareka7fb8cd7af98
diff --git a/saleor/graphql/giftcard/types.py b/saleor/graphql/giftcard/types.py
index 3809840772..4b4da6529c 100644
--- a/saleor/graphql/giftcard/types.py
+++ b/saleor/graphql/giftcard/types.py
@@ -11,6 +11,12 @@ class GiftCard(CountableDjangoObjectType):
display_code = graphene.String(
description="Code in format with allows displaying in a user interface."
)
+ code = graphene.String(
+ description=dedent(
+ """Gift card code available for gift card owner or staff for gift card
+ without a customer."""
+ )
+ )
user = graphene.Field(
"saleor.graphql.account.types.User",
description="The customer who bought a gift card.",
@@ -25,6 +31,7 @@ class GiftCard(CountableDjangoObjectType):
)
only_fields = [
"user",
+ "code",
"created",
"start_date",
"end_date",
@@ -44,3 +51,12 @@ class GiftCard(CountableDjangoObjectType):
@permission_required("giftcard.manage_gift_card")
def resolve_user(root: models.GiftCard, *_args, **_kwargs):
return root.user
+
+ @staticmethod
+ def resolve_code(root: models.GiftCard, info, **_kwargs):
+ viewer = info.context.user
+ if viewer.has_perm("giftcard.manage_gift_card") and not root.user:
+ return root.code
+ if viewer == root.user:
+ return root.code
+ return None
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index e854b7006e..0fb21c1c11 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1209,6 +1209,7 @@ type Geolocalization {
}
type GiftCard implements Node {
+ code: String
user: User
created: DateTime!
startDate: Date!
diff --git a/tests/api/test_gift_card.py b/tests/api/test_gift_card.py
index f0ab8f90c4..47515c1baf 100644
--- a/tests/api/test_gift_card.py
+++ b/tests/api/test_gift_card.py
@@ -31,6 +31,61 @@ def test_query_gift_card_with_premissions(
assert data["user"]["email"] == gift_card.user.email
+def test_query_gift_card_code_without_user(
+ staff_api_client, gift_card_created_by_staff, permission_manage_gift_card
+):
+ query = """
+ query giftCard($id: ID!) {
+ giftCard(id: $id){
+ id
+ displayCode
+ code
+ user{
+ email
+ }
+ }
+ }
+ """
+ gift_card = gift_card_created_by_staff
+ gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.pk)
+ variables = {"id": gift_card_id}
+ staff_api_client.user.user_permissions.add(permission_manage_gift_card)
+ response = staff_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["giftCard"]
+ assert data["id"] == gift_card_id
+ assert data["displayCode"] == gift_card.display_code
+ assert data["code"] == gift_card.code
+ assert not data["user"]
+
+
+def test_query_gift_card_code_with_user(
+ staff_api_client, gift_card, permission_manage_gift_card
+):
+ query = """
+ query giftCard($id: ID!) {
+ giftCard(id: $id){
+ id
+ displayCode
+ code
+ user{
+ email
+ }
+ }
+ }
+ """
+ gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.pk)
+ variables = {"id": gift_card_id}
+ staff_api_client.user.user_permissions.add(permission_manage_gift_card)
+ response = staff_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["giftCard"]
+ assert data["id"] == gift_card_id
+ assert data["displayCode"] == gift_card.display_code
+ assert data["user"]["email"]
+ assert not data["code"]
+
+
def test_query_gift_card_without_premissions(
user_api_client, gift_card_created_by_staff
):
@@ -86,6 +141,7 @@ def test_query_own_gift_cards(user_api_client, gift_card, gift_card_created_by_s
node {
id
displayCode
+ code
}
}
totalCount
@@ -99,6 +155,7 @@ def test_query_own_gift_cards(user_api_client, gift_card, gift_card_created_by_s
data = content["data"]["me"]["giftCards"]
assert data["edges"][0]["node"]["id"] == gift_card_id
assert data["edges"][0]["node"]["displayCode"] == gift_card.display_code
+ assert data["edges"][0]["node"]["code"] == gift_card.code
assert data["totalCount"] == 1