Skip to content

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

Fix sending gift card multiple times

IKarbowiakf0316fed098d
diff --git a/saleor/giftcard/notifications.py b/saleor/giftcard/notifications.py
index 344654ad35..034ff19a84 100644
--- a/saleor/giftcard/notifications.py
+++ b/saleor/giftcard/notifications.py
@@ -2,7 +2,7 @@ from ..core.notifications import get_site_context
from ..core.notify_events import NotifyEventType
-def send_gift_card_notification(user, app, email, gift_card, manager):
+def send_gift_card_notification(user, app, email, gift_card, manager, channel_slug):
"""Trigger sending a gift card notification for the given recipient."""
payload = {
"requester": {
@@ -24,4 +24,6 @@ def send_gift_card_notification(user, app, email, gift_card, manager):
},
**get_site_context(),
}
- manager.notify(NotifyEventType.SEND_GIFT_CARD, payload=payload)
+ manager.notify(
+ NotifyEventType.SEND_GIFT_CARD, payload=payload, channel_slug=channel_slug
+ )
diff --git a/saleor/giftcard/tests/test_utils.py b/saleor/giftcard/tests/test_utils.py
index 9e4ef54e42..52c8cf048f 100644
--- a/saleor/giftcard/tests/test_utils.py
+++ b/saleor/giftcard/tests/test_utils.py
@@ -245,7 +245,12 @@ def test_gift_cards_create(
assert shippable_event.parameters == {"order_id": order.id, "expiry_date": None}
send_notification_mock.assert_called_once_with(
- staff_user, None, user_email, non_shippable_gift_card, manager
+ staff_user,
+ None,
+ user_email,
+ non_shippable_gift_card,
+ manager,
+ order.channel.slug,
)
@@ -311,7 +316,12 @@ def test_gift_cards_create_expiry_date_set(
}
send_notification_mock.assert_called_once_with(
- staff_user, None, user_email, non_shippable_gift_card, manager
+ staff_user,
+ None,
+ user_email,
+ non_shippable_gift_card,
+ manager,
+ order.channel.slug,
)
diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.py
index fc70e5c580..478187c0da 100644
--- a/saleor/giftcard/utils.py
+++ b/saleor/giftcard/utils.py
@@ -198,9 +198,10 @@ def gift_cards_create(
gift_cards = GiftCard.objects.bulk_create(gift_cards)
events.gift_cards_bought(gift_cards, order.id, requestor_user, app)
+ channel_slug = order.channel.slug
# send to customer all non-shippable gift cards
send_gift_cards_to_customer(
- non_shippable_gift_cards, user_email, requestor_user, app, manager
+ non_shippable_gift_cards, user_email, requestor_user, app, manager, channel_slug
)
return gift_cards
@@ -222,8 +223,11 @@ def send_gift_cards_to_customer(
requestor_user: Optional["User"],
app: Optional["App"],
manager: "PluginsManager",
+ channel_slug: str,
):
for gift_card in gift_cards:
- send_gift_card_notification(requestor_user, app, user_email, gift_card, manager)
+ send_gift_card_notification(
+ requestor_user, app, user_email, gift_card, manager, channel_slug
+ )
events.gift_cards_sent(gift_cards, requestor_user, app, user_email)
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index e887d4267b..b46a7dddc9 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -173,6 +173,7 @@ class GiftCardCreate(ModelMutation):
cleaned_input["user_email"],
instance,
info.context.plugins,
+ channel_slug=None,
)
events.gift_card_sent(
gift_card_id=instance.id,
@@ -362,6 +363,8 @@ class GiftCardResend(BaseMutation):
target_email,
gift_card,
info.context.plugins,
+ # TODO: should be fulfill with channel_slug from input
+ channel_slug="",
)
events.gift_card_resent(
gift_card_id=gift_card.id,