Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Extend gift card events
IKarbowiak0c6de141461f
diff --git a/saleor/giftcard/__init__.py b/saleor/giftcard/__init__.pyindex 3460def1f1..3124796766 100644--- a/saleor/giftcard/__init__.py+++ b/saleor/giftcard/__init__.py@@ -22,9 +22,11 @@ class GiftCardEvents:DEACTIVATED = "deactivated"BALANCE_RESET = "balance_reset"EXPIRY_SETTINGS_UPDATED = "expiry_settings_updated"+ EXPIRY_DATE_SET = "expiry_date_set"SENT_TO_CUSTOMER = "sent_to_customer"RESENT = "resent"NOTE_ADDED = "note_added"+ USED_IN_ORDER = "used_in_order"CHOICES = [(ISSUED, "The gift card was created be staff user or app."),@@ -34,7 +36,9 @@ class GiftCardEvents:(DEACTIVATED, "The gift card was deactivated."),(BALANCE_RESET, "The gift card balance was reset."),(EXPIRY_SETTINGS_UPDATED, "The gift card expiry settings was updated."),+ (EXPIRY_DATE_SET, "The expiry date was set."),(SENT_TO_CUSTOMER, "The gift card was sent to the customer."),(RESENT, "The gift card was resent to the customer."),(NOTE_ADDED, "A note was added to the gift card."),+ (USED_IN_ORDER, "The gift card was used in order."),]diff --git a/saleor/giftcard/events.py b/saleor/giftcard/events.pyindex d815b69ccf..d213baab78 100644--- a/saleor/giftcard/events.py+++ b/saleor/giftcard/events.py@@ -1,4 +1,5 @@-from typing import Iterable, Optional+from datetime import date+from typing import Iterable, Optional, Tuplefrom ..account.models import Userfrom ..app.models import App@@ -187,3 +188,51 @@ def gift_card_note_added(type=GiftCardEvents.NOTE_ADDED,parameters={"message": message},)+++def gift_cards_expiry_date_set(+ expiry_data: Iterable[Tuple[int, date]],+ user: UserType,+ app: AppType,+):+ if not user_is_valid(user):+ user = None+ events = [+ GiftCardEvent(+ gift_card_id=gift_card_id,+ user=user,+ app=app,+ type=GiftCardEvents.EXPIRY_DATE_SET,+ parameters={"expiry": {"expiry_date": expiry_date}},+ )+ for gift_card_id, expiry_date in expiry_data+ ]+ return GiftCardEvent.objects.bulk_create(events)+++def gift_cards_used_in_order(+ balance_data: Iterable[Tuple[GiftCard, float]],+ order_id: int,+ user: UserType,+ app: AppType,+):+ if not user_is_valid(user):+ user = None+ events = [+ GiftCardEvent(+ gift_card=gift_card,+ user=user,+ app=app,+ type=GiftCardEvents.USED_IN_ORDER,+ parameters={+ "order_id": order_id,+ "balance": {+ "currency": gift_card.currency,+ "current_balance": gift_card.current_balance.amount,+ "old_current_balance": previous_balance,+ },+ },+ )+ for gift_card, previous_balance in balance_data+ ]+ return GiftCardEvent.objects.bulk_create(events)diff --git a/saleor/giftcard/migrations/0008_alter_giftcardevent_type.py b/saleor/giftcard/migrations/0008_alter_giftcardevent_type.pyindex 8250357131..510c41903f 100644--- a/saleor/giftcard/migrations/0008_alter_giftcardevent_type.py+++ b/saleor/giftcard/migrations/0008_alter_giftcardevent_type.py@@ -25,9 +25,11 @@ class Migration(migrations.Migration):"expiry_settings_updated","The gift card expiry settings was updated.",),+ ("expiry_date_set", "The expiry date was set."),("sent_to_customer", "The gift card was sent to the customer."),("resent", "The gift card was resent to the customer."),("note_added", "A note was added to the gift card."),+ ("used_in_order", "The gift card was used in order."),],max_length=255,),diff --git a/saleor/graphql/giftcard/tests/queries/test_gift_card.py b/saleor/graphql/giftcard/tests/queries/test_gift_card.pyindex 3c1a831292..9196a9b74d 100644--- a/saleor/graphql/giftcard/tests/queries/test_gift_card.py+++ b/saleor/graphql/giftcard/tests/queries/test_gift_card.py@@ -1,7 +1,9 @@+from datetime import timedelta+import graphenefrom django.utils import timezone-from .....giftcard import GiftCardEvents+from .....giftcard import GiftCardEvents, eventsfrom .....giftcard.models import GiftCardEventfrom ....tests.utils import (assert_no_permission,@@ -559,3 +561,81 @@ def test_query_gift_card_events(== parameters["expiry"]["expiry_period_type"].upper())assert events_data[1]["expiry"]["oldExpiryPeriod"] is None+++def test_query_gift_card_expiry_date_set_event(+ staff_api_client,+ gift_card_expiry_period,+ staff_user,+ permission_manage_gift_card,+ permission_manage_apps,+):+ # given+ expiry_date = timezone.now().date() + timedelta(days=400)+ expiry_data = [(gift_card_expiry_period.id, expiry_date)]+ events.gift_cards_expiry_date_set(expiry_data, staff_user, None)+ variables = {+ "id": graphene.Node.to_global_id("GiftCard", gift_card_expiry_period.pk)+ }++ # when+ response = staff_api_client.post_graphql(+ QUERY_GIFT_CARD_EVENTS,+ variables,+ permissions=[permission_manage_gift_card, permission_manage_apps],+ )++ # then+ content = get_graphql_content(response)+ events_data = content["data"]["giftCard"]["events"]+ assert len(events_data) == gift_card_expiry_period.events.count()+ event_data = events_data[0]+ assert event_data["expiry"]["expiryDate"] == expiry_date.isoformat()+ assert event_data["user"]["email"] == staff_user.email+ assert event_data["app"] is None+ assert event_data["type"] == GiftCardEvents.EXPIRY_DATE_SET.upper()+ assert event_data["balance"] is None+++def test_query_gift_card_used_in_order_event(+ staff_api_client,+ gift_card,+ app,+ permission_manage_gift_card,+ permission_manage_apps,+ permission_manage_users,+):+ # given+ previous_balance = 10.0+ balance_data = [(gift_card, previous_balance)]+ order_id = 1+ events.gift_cards_used_in_order(balance_data, order_id, None, app)+ variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card.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)+ events_data = content["data"]["giftCard"]["events"]+ assert len(events_data) == gift_card.events.count()+ event_data = events_data[0]+ assert (+ event_data["balance"]["currentBalance"]["amount"]+ == gift_card.current_balance_amount+ )+ assert event_data["balance"]["currentBalance"]["currency"] == gift_card.currency+ assert event_data["balance"]["oldCurrentBalance"]["amount"] == previous_balance+ assert event_data["balance"]["oldCurrentBalance"]["currency"] == gift_card.currency+ assert event_data["expiry"] is None+ assert events_data[0]["user"] is None+ assert events_data[0]["app"]["name"] == app.name+ assert event_data["type"] == GiftCardEvents.USED_IN_ORDER.upper()diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex dd5948bd67..2a4606e18a 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -2379,9 +2379,11 @@ enum GiftCardEventsEnum {DEACTIVATEDBALANCE_RESETEXPIRY_SETTINGS_UPDATED+ EXPIRY_DATE_SETSENT_TO_CUSTOMERRESENTNOTE_ADDED+ USED_IN_ORDER}input GiftCardExpirySettingsInput {