Skip to content

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

Extend GfitCard with boughtInChannel field

IKarbowiak6912a504c540
diff --git a/saleor/graphql/giftcard/tests/benchmark/test_gift_card_mutations.py b/saleor/graphql/giftcard/tests/benchmark/test_gift_card_mutations.py
index b81c2a3ce8..8ec0783591 100644
--- a/saleor/graphql/giftcard/tests/benchmark/test_gift_card_mutations.py
+++ b/saleor/graphql/giftcard/tests/benchmark/test_gift_card_mutations.py
@@ -8,11 +8,11 @@ from ....tests.utils import get_graphql_content
CREATE_GIFT_CARD_MUTATION = """
mutation giftCardCreate(
- $balance: PriceInput!, $userEmail: String, $tag: String,
+ $balance: PriceInput!, $userEmail: String, $tag: String, $channel: String,
$note: String, $expiryDate: Date, $isActive: Boolean!
){
giftCardCreate(input: {
- balance: $balance, userEmail: $userEmail, tag: $tag,
+ balance: $balance, userEmail: $userEmail, tag: $tag, channel: $channel,
expiryDate: $expiryDate, note: $note, isActive: $isActive }) {
giftCard {
id
@@ -88,6 +88,7 @@ CREATE_GIFT_CARD_MUTATION = """
def test_create_never_expiry_gift_card(
staff_api_client,
customer_user,
+ channel_USD,
permission_manage_gift_card,
permission_manage_users,
permission_manage_apps,
@@ -103,6 +104,7 @@ def test_create_never_expiry_gift_card(
"currency": currency,
},
"userEmail": customer_user.email,
+ "channel": channel_USD.slug,
"tag": tag,
"note": "This is gift card note that will be save in gift card event.",
"expiry_date": None,
diff --git a/saleor/graphql/giftcard/tests/benchmark/test_gift_card_queries.py b/saleor/graphql/giftcard/tests/benchmark/test_gift_card_queries.py
index 1b1064657a..57fa521fd7 100644
--- a/saleor/graphql/giftcard/tests/benchmark/test_gift_card_queries.py
+++ b/saleor/graphql/giftcard/tests/benchmark/test_gift_card_queries.py
@@ -56,6 +56,7 @@ FRAGMENT_GIFT_CARD_DETAILS = (
tag
created
lastUsedOn
+ boughtInChannel
initialBalance {
currency
amount
diff --git a/saleor/graphql/giftcard/tests/deprecated/test_gift_card_mutations.py b/saleor/graphql/giftcard/tests/deprecated/test_gift_card_mutations.py
index c38c7aea95..b164a16377 100644
--- a/saleor/graphql/giftcard/tests/deprecated/test_gift_card_mutations.py
+++ b/saleor/graphql/giftcard/tests/deprecated/test_gift_card_mutations.py
@@ -4,13 +4,13 @@ from ....tests.utils import get_graphql_content
CREATE_GIFT_CARD_MUTATION = """
mutation giftCardCreate(
- $startDate: Date, $endDate: Date, $expiryDate: Date
+ $startDate: Date, $endDate: Date, $expiryDate: Date, $channel: String,
$balance: PriceInput!, $userEmail: String, $isActive: Boolean!
){
giftCardCreate(input: {
startDate: $startDate,
endDate: $endDate,
- balance: $balance, userEmail: $userEmail,
+ balance: $balance, userEmail: $userEmail, channel: $channel
expiryDate: $expiryDate, isActive: $isActive
}) {
giftCard {
@@ -49,6 +49,7 @@ CREATE_GIFT_CARD_MUTATION = """
def test_create_never_expiry_gift_card(
staff_api_client,
customer_user,
+ channel_USD,
permission_manage_gift_card,
permission_manage_users,
permission_manage_apps,
@@ -65,6 +66,7 @@ def test_create_never_expiry_gift_card(
"currency": currency,
},
"userEmail": customer_user.email,
+ "channel": channel_USD.slug,
"tag": tag,
"note": "This is gift card note that will be save in gift card event.",
"startDate": start_date.isoformat(),
diff --git a/saleor/graphql/giftcard/tests/queries/test_gift_card.py b/saleor/graphql/giftcard/tests/queries/test_gift_card.py
index 2e974f0dfe..5257ed14ac 100644
--- a/saleor/graphql/giftcard/tests/queries/test_gift_card.py
+++ b/saleor/graphql/giftcard/tests/queries/test_gift_card.py
@@ -23,6 +23,7 @@ QUERY_GIFT_CARD_BY_ID = """
tag
created
lastUsedOn
+ boughtInChannel
initialBalance {
currency
amount
@@ -84,6 +85,7 @@ def test_query_gift_card_with_permissions(
assert data["tag"] == gift_card.tag
assert data["created"] == gift_card.created.isoformat()
assert data["lastUsedOn"] == gift_card.last_used_on
+ assert data["boughtInChannel"] is None
assert data["initialBalance"]["currency"] == gift_card.initial_balance.currency
assert data["initialBalance"]["amount"] == gift_card.initial_balance.amount
assert data["currentBalance"]["currency"] == gift_card.current_balance.currency
@@ -364,6 +366,7 @@ QUERY_GIFT_CARD_EVENTS = """
query giftCard($id: ID!) {
giftCard(id: $id){
id
+ boughtInChannel
events {
id
date
@@ -584,3 +587,42 @@ def test_query_gift_card_used_in_order_event(
assert events_data[0]["user"] is None
assert events_data[0]["app"]["name"] == app.name
assert event_data["type"] == GiftCardEvents.USED_IN_ORDER.upper()
+
+
+def test_query_gift_card_bought_event(
+ staff_api_client,
+ gift_card_expiry_date,
+ order,
+ app,
+ permission_manage_gift_card,
+ permission_manage_apps,
+ permission_manage_users,
+):
+ # given
+ events.gift_cards_bought([gift_card_expiry_date], order.id, None, app)
+ variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card_expiry_date.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_GIFT_CARD_EVENTS,
+ variables,
+ permissions=[
+ permission_manage_gift_card,
+ permission_manage_apps,
+ permission_manage_users,
+ ],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["giftCard"]
+ events_data = data["events"]
+ assert data["boughtInChannel"] == order.channel.slug
+ assert len(events_data) == gift_card_expiry_date.events.count()
+ event_data = events_data[0]
+ assert event_data["balance"] is None
+ assert event_data["expiryDate"] == gift_card_expiry_date.expiry_date.isoformat()
+ assert event_data["orderId"] == graphene.Node.to_global_id("Order", order.id)
+ assert event_data["type"] == GiftCardEvents.BOUGHT.upper()
+ assert event_data["user"] is None
+ assert event_data["app"]["name"] == app.name
diff --git a/saleor/graphql/giftcard/types.py b/saleor/graphql/giftcard/types.py
index 4057403a01..8f328b9ddc 100644
--- a/saleor/graphql/giftcard/types.py
+++ b/saleor/graphql/giftcard/types.py
@@ -8,17 +8,19 @@ from ...core.anonymize import obfuscate_email
from ...core.exceptions import PermissionDenied
from ...core.permissions import AccountPermissions, AppPermission, GiftcardPermissions
from ...core.tracing import traced_resolver
-from ...giftcard import models
+from ...giftcard import GiftCardEvents, models
from ..account.dataloaders import UserByUserIdLoader
from ..account.utils import requestor_has_access
from ..app.dataloaders import AppByIdLoader
from ..app.types import App
from ..channel import ChannelContext
+from ..channel.dataloaders import ChannelByIdLoader
from ..core.connection import CountableDjangoObjectType
from ..core.descriptions import ADDED_IN_31, DEPRECATED_IN_3X_FIELD
from ..core.types.money import Money
from ..decorators import permission_required
from ..meta.types import ObjectWithMetadata
+from ..order.dataloaders import OrderByIdLoader
from ..product.dataloaders.products import ProductByIdLoader
from ..utils import get_user_or_app_from_context
from .dataloaders import GiftCardEventsByGiftCardIdLoader
@@ -217,6 +219,12 @@ class GiftCard(CountableDjangoObjectType):
required=True,
)
tag = graphene.String(description=f"{ADDED_IN_31} The gift card tag.")
+ bought_in_channel = graphene.String(
+ description=(
+ "{ADDED_IN_31} Slug of the channel where the gift card was bought."
+ ),
+ required=False,
+ )
# DEPRECATED
user = graphene.Field(
@@ -372,6 +380,41 @@ class GiftCard(CountableDjangoObjectType):
def resolve_events(root: models.GiftCard, _info):
return GiftCardEventsByGiftCardIdLoader(_info.context).load(root.id)
+ @staticmethod
+ @traced_resolver
+ def resolve_bought_in_channel(root: models.GiftCard, info):
+ def with_bought_event(events):
+ bought_event = None
+ for event in events:
+ if event.type == GiftCardEvents.BOUGHT:
+ bought_event = event
+ break
+
+ if bought_event is None:
+ return None
+
+ def with_order(order):
+ if not order or not order.channel_id:
+ return None
+
+ def get_channel_slug(channel):
+ return channel.slug if channel else None
+
+ return (
+ ChannelByIdLoader(info.context)
+ .load(order.channel_id)
+ .then(get_channel_slug)
+ )
+
+ order_id = bought_event.parameters["order_id"]
+ return OrderByIdLoader(info.context).load(order_id).then(with_order)
+
+ return (
+ GiftCardEventsByGiftCardIdLoader(info.context)
+ .load(root.id)
+ .then(with_bought_event)
+ )
+
# DEPRECATED
@staticmethod
def resolve_user(root: models.GiftCard, info):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 3a29a3c366..9b528352bc 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2247,6 +2247,7 @@ type GiftCard implements Node & ObjectWithMetadata {
app: App
product: Product
events: [GiftCardEvent!]!
+ boughtInChannel: String
user: User @deprecated(reason: "This field will be removed in Saleor 4.0. Use `createdBy` field instead.")
endDate: DateTime @deprecated(reason: "This field will be removed in Saleor 4.0. Use `expiryDate` field instead.")
startDate: DateTime @deprecated(reason: "This field will be removed in Saleor 4.0.")