Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
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.pyindex 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_contentCREATE_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.pyindex 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 = (tagcreatedlastUsedOn+ boughtInChannelinitialBalance {currencyamountdiff --git a/saleor/graphql/giftcard/tests/deprecated/test_gift_card_mutations.py b/saleor/graphql/giftcard/tests/deprecated/test_gift_card_mutations.pyindex 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_contentCREATE_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: $channelexpiryDate: $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.pyindex 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 = """tagcreatedlastUsedOn+ boughtInChannelinitialBalance {currencyamount@@ -84,6 +85,7 @@ def test_query_gift_card_with_permissions(assert data["tag"] == gift_card.tagassert data["created"] == gift_card.created.isoformat()assert data["lastUsedOn"] == gift_card.last_used_on+ assert data["boughtInChannel"] is Noneassert data["initialBalance"]["currency"] == gift_card.initial_balance.currencyassert data["initialBalance"]["amount"] == gift_card.initial_balance.amountassert data["currentBalance"]["currency"] == gift_card.current_balance.currency@@ -364,6 +366,7 @@ QUERY_GIFT_CARD_EVENTS = """query giftCard($id: ID!) {giftCard(id: $id){id+ boughtInChannelevents {iddate@@ -584,3 +587,42 @@ def test_query_gift_card_used_in_order_event(assert events_data[0]["user"] is Noneassert events_data[0]["app"]["name"] == app.nameassert 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.namediff --git a/saleor/graphql/giftcard/types.py b/saleor/graphql/giftcard/types.pyindex 4057403a01..8f328b9ddc 100644--- a/saleor/graphql/giftcard/types.py+++ b/saleor/graphql/giftcard/types.py@@ -8,17 +8,19 @@ from ...core.anonymize import obfuscate_emailfrom ...core.exceptions import PermissionDeniedfrom ...core.permissions import AccountPermissions, AppPermission, GiftcardPermissionsfrom ...core.tracing import traced_resolver-from ...giftcard import models+from ...giftcard import GiftCardEvents, modelsfrom ..account.dataloaders import UserByUserIdLoaderfrom ..account.utils import requestor_has_accessfrom ..app.dataloaders import AppByIdLoaderfrom ..app.types import Appfrom ..channel import ChannelContext+from ..channel.dataloaders import ChannelByIdLoaderfrom ..core.connection import CountableDjangoObjectTypefrom ..core.descriptions import ADDED_IN_31, DEPRECATED_IN_3X_FIELDfrom ..core.types.money import Moneyfrom ..decorators import permission_requiredfrom ..meta.types import ObjectWithMetadata+from ..order.dataloaders import OrderByIdLoaderfrom ..product.dataloaders.products import ProductByIdLoaderfrom ..utils import get_user_or_app_from_contextfrom .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,+ )# DEPRECATEDuser = 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@staticmethoddef resolve_user(root: models.GiftCard, info):diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 3a29a3c366..9b528352bc 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -2247,6 +2247,7 @@ type GiftCard implements Node & ObjectWithMetadata {app: Appproduct: Productevents: [GiftCardEvent!]!+ boughtInChannel: Stringuser: 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.")