Skip to content

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

Add query for get gift cards for checkout

fowczarek5b4a41271e7b
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.py
index 303a2c2163..d63c4165e8 100644
--- a/saleor/checkout/models.py
+++ b/saleor/checkout/models.py
@@ -13,6 +13,7 @@ from django_prices.models import MoneyField
from ..account.models import Address
from ..core.utils.taxes import ZERO_TAXED_MONEY, zero_money
from ..core.weight import zero_weight
+from ..giftcard.models import GiftCard
from ..shipping.models import ShippingMethod
CENTS = Decimal("0.01")
@@ -73,6 +74,7 @@ class Checkout(models.Model):
discount_name = models.CharField(max_length=255, blank=True, null=True)
translated_discount_name = models.CharField(max_length=255, blank=True, null=True)
voucher_code = models.CharField(max_length=12, blank=True, null=True)
+ gift_cards = models.ManyToManyField(GiftCard, blank=True, related_name="gift_cards")
objects = CheckoutQueryset.as_manager()
diff --git a/saleor/giftcard/models.py b/saleor/giftcard/models.py
index 4b8bfdaf14..9486649e91 100644
--- a/saleor/giftcard/models.py
+++ b/saleor/giftcard/models.py
@@ -41,3 +41,7 @@ class GiftCard(models.Model):
pgettext_lazy("Permission description", "Manage gift cards."),
),
)
+
+ @property
+ def display_code(self):
+ return "****%s" % self.code[-4:]
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index cc95897424..d59acaef95 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -6,6 +6,7 @@ from ...checkout import models
from ...core.utils.taxes import get_taxes_for_address
from ..core.connection import CountableDjangoObjectType
from ..core.types.money import TaxedMoney
+from ..giftcard.types import GiftCard
from ..order.utils import applicable_shipping_methods
from ..payment.enums import PaymentGatewayEnum
from ..shipping.types import ShippingMethod
@@ -49,6 +50,10 @@ class Checkout(CountableDjangoObjectType):
required=True,
)
email = graphene.String(description="Email of a customer", required=True)
+ gift_cards = gql_optimizer.field(
+ graphene.List(GiftCard, description="List of userd gift cards"),
+ model_field="gift_cards",
+ )
is_shipping_required = graphene.Boolean(
description="Returns True, if checkout requires shipping.", required=True
)
@@ -84,6 +89,7 @@ class Checkout(CountableDjangoObjectType):
"created",
"discount_amount",
"discount_name",
+ "gift_cards",
"is_shipping_required",
"last_change",
"note",
@@ -129,6 +135,10 @@ class Checkout(CountableDjangoObjectType):
def resolve_available_payment_gateways(_: models.Checkout, _info):
return settings.CHECKOUT_PAYMENT_GATEWAYS.keys()
+ @staticmethod
+ def resolve_gift_cards(root: models.Checkout, _info):
+ return root.gift_cards.all()
+
@staticmethod
def resolve_is_shipping_required(root: models.Checkout, _info):
return root.is_shipping_required()
diff --git a/saleor/graphql/giftcard/types.py b/saleor/graphql/giftcard/types.py
index a99e08f90d..1fa33e2c88 100644
--- a/saleor/graphql/giftcard/types.py
+++ b/saleor/graphql/giftcard/types.py
@@ -1,12 +1,25 @@
from textwrap import dedent
-from graphene import relay
+import graphene
+from graphql_jwt.decorators import permission_required
from ...giftcard import models
from ..core.connection import CountableDjangoObjectType
class GiftCard(CountableDjangoObjectType):
+ display_code = graphene.String(
+ description="Code in format with allows displaying in a user interface."
+ )
+ buyer = graphene.Field(
+ "saleor.graphql.account.types.User",
+ description="The customer who bought a gift card.",
+ )
+ creator = graphene.Field(
+ "saleor.graphql.account.types.User",
+ description="The staff who created a gift card.",
+ )
+
class Meta:
description = dedent(
"""
@@ -26,5 +39,16 @@ class GiftCard(CountableDjangoObjectType):
"initial_balance",
"current_balance",
]
- interfaces = [relay.Node]
+ interfaces = [graphene.relay.Node]
model = models.GiftCard
+
+ def resolve_display_code(self, _info):
+ return self.display_code
+
+ @permission_required("giftcard.manage_gift_card")
+ def resolve_buyer(self, _info):
+ return self.buyer
+
+ @permission_required("giftcard.manage_gift_card")
+ def resolve_creator(self, _info):
+ return self.creator
diff --git a/tests/api/test_gift_card.py b/tests/api/test_gift_card.py
index a9f233be6e..2649819961 100644
--- a/tests/api/test_gift_card.py
+++ b/tests/api/test_gift_card.py
@@ -11,12 +11,6 @@ def test_query_own_gift_card(user_api_client, staff_user, gift_card):
query giftCard($id: ID!) {
giftCard(id: $id){
code
- buyer {
- email
- }
- creator {
- email
- }
created
startDate
expirationDate
@@ -37,8 +31,6 @@ def test_query_own_gift_card(user_api_client, staff_user, gift_card):
content = get_graphql_content(response)
data = content["data"]["giftCard"]
assert data["code"] == gift_card.code
- assert data["buyer"]["email"] == gift_card.buyer.email
- assert data["creator"]["email"] == staff_user.email
assert data["created"] == gift_card.created.isoformat()
assert data["startDate"] == gift_card.start_date.isoformat()
assert data["expirationDate"] == gift_card.expiration_date