Skip to content

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

Create NOTE_ADDED when note provided in GiftCardCreate mutation

IKarbowiak427902a5b6b7
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index c986304328..903a10044d 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -183,11 +183,17 @@ class GiftCardCreate(ModelMutation):
@classmethod
def post_save_action(cls, info, instance, cleaned_input):
+ user = info.context.user
+ app = info.context.app
events.gift_card_issued_event(
gift_card=instance,
- user=info.context.user,
- app=info.context.app,
+ user=user,
+ app=app,
)
+ if note := cleaned_input.get("note"):
+ events.gift_card_note_added_event(
+ gift_card=instance, user=user, app=app, message=note
+ )
if email := cleaned_input.get("user_email"):
send_gift_card_notification(
cleaned_input.get("created_by"),
diff --git a/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py b/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py
index 1652731b1b..2a00a5ce84 100644
--- a/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py
+++ b/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py
@@ -44,6 +44,7 @@ CREATE_GIFT_CARD_MUTATION = """
}
events {
type
+ message
user {
email
}
@@ -94,6 +95,7 @@ def test_create_never_expiry_gift_card(
initial_balance = 100
currency = "USD"
tag = "gift-card-tag"
+ note = "This is gift card note that will be save in gift card event."
variables = {
"input": {
"balance": {
@@ -103,7 +105,7 @@ def test_create_never_expiry_gift_card(
"userEmail": customer_user.email,
"channel": channel_USD.slug,
"tag": tag,
- "note": "This is gift card note that will be save in gift card event.",
+ "note": note,
"isActive": True,
}
}
@@ -139,8 +141,8 @@ def test_create_never_expiry_gift_card(
assert data["initialBalance"]["amount"] == initial_balance
assert data["currentBalance"]["amount"] == initial_balance
- assert len(data["events"]) == 1
- created_event = data["events"][0]
+ assert len(data["events"]) == 2
+ created_event, note_added = data["events"]
assert created_event["type"] == GiftCardEvents.ISSUED.upper()
assert created_event["user"]["email"] == staff_api_client.user.email
@@ -152,6 +154,11 @@ def test_create_never_expiry_gift_card(
assert not created_event["balance"]["oldInitialBalance"]
assert not created_event["balance"]["oldCurrentBalance"]
+ assert note_added["type"] == GiftCardEvents.NOTE_ADDED.upper()
+ assert note_added["user"]["email"] == staff_api_client.user.email
+ assert not note_added["app"]
+ assert note_added["message"] == note
+
gift_card = GiftCard.objects.get()
send_notification_mock.assert_called_once_with(
staff_api_client.user,
@@ -177,6 +184,7 @@ def test_create_gift_card_by_app(
initial_balance = 100
currency = "USD"
tag = "gift-card-tag"
+ note = "This is gift card note that will be save in gift card event."
variables = {
"input": {
"balance": {
@@ -184,7 +192,7 @@ def test_create_gift_card_by_app(
"currency": currency,
},
"tag": tag,
- "note": "This is gift card note that will be save in gift card event.",
+ "note": note,
"expiryDate": None,
"isActive": False,
}
@@ -217,8 +225,8 @@ def test_create_gift_card_by_app(
assert data["initialBalance"]["amount"] == initial_balance
assert data["currentBalance"]["amount"] == initial_balance
- assert len(data["events"]) == 1
- created_event = data["events"][0]
+ assert len(data["events"]) == 2
+ created_event, note_added = data["events"]
assert created_event["type"] == GiftCardEvents.ISSUED.upper()
assert not created_event["user"]
@@ -230,6 +238,11 @@ def test_create_gift_card_by_app(
assert not created_event["balance"]["oldInitialBalance"]
assert not created_event["balance"]["oldCurrentBalance"]
+ assert note_added["type"] == GiftCardEvents.NOTE_ADDED.upper()
+ assert not note_added["user"]
+ assert note_added["app"]["name"] == app_api_client.app.name
+ assert note_added["message"] == note
+
send_notification_mock.assert_not_called()
@@ -499,7 +512,6 @@ def test_create_gift_card_with_expiry_date(
"userEmail": customer_user.email,
"channel": channel_USD.slug,
"tag": tag,
- "note": "This is gift card note that will be save in gift card event.",
"expiryDate": date_value,
"isActive": True,
}