Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Trigger `GIFT_CARD_CREATED` event in `gift_cards_create` utility (#11924)
Rafał Pitoń6a65127a8fa6
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2482e0c47d..fbfe3b4668 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -82,6 +82,7 @@ Just so you know, changes mentioned in this section are in a preview state and c- Propagate voucher discount between checkout lines when charge_taxes is disabled - #11632 by @maarcingebala- Fix stock events triggers - #11714 by @jakubkuc- Accept the gift card code provided in the input - by @mociepka+- Fix `GIFT_CARD_CREATED` event not firing when order with gift cards is fulfilled - #11924 by @rafalp# 3.9.0diff --git a/saleor/giftcard/tests/test_utils.py b/saleor/giftcard/tests/test_utils.pyindex f0d93524da..51fd3fb8a5 100644--- a/saleor/giftcard/tests/test_utils.py+++ b/saleor/giftcard/tests/test_utils.py@@ -1,17 +1,23 @@+import jsonfrom datetime import date, timedeltafrom unittest.mock import patch+import grapheneimport pytestfrom dateutil.relativedelta import relativedeltafrom django.utils import timezone+from freezegun import freeze_timefrom ...core import TimePeriodTypefrom ...core.exceptions import GiftCardNotApplicable+from ...core.utils.json_serializer import CustomJsonEncoderfrom ...core.utils.promo_code import InvalidPromoCodefrom ...order.models import OrderLinefrom ...plugins.manager import get_plugins_managerfrom ...site import GiftCardSettingsExpiryTypefrom ...tests.utils import flush_post_commit_hooks+from ...webhook.event_types import WebhookEventAsyncType+from ...webhook.payloads import generate_meta, generate_requestorfrom .. import GiftCardEvents, GiftCardLineData, eventsfrom ..models import GiftCard, GiftCardEventfrom ..utils import (@@ -403,6 +409,87 @@ def test_gift_cards_create_multiple_quantity(assert send_notification_mock.call_count == quantity+@freeze_time("2022-05-12 12:00:00")+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")+@patch("saleor.giftcard.utils.send_gift_card_notification")+def test_gift_cards_create_trigger_webhook(+ send_notification_mock,+ mocked_webhook_trigger,+ mocked_get_webhooks_for_event,+ any_webhook,+ webhook_app,+ settings,+ order,+ gift_card_shippable_order_line,+ gift_card_non_shippable_order_line,+ site_settings,+ staff_user,+):+ # given+ mocked_get_webhooks_for_event.return_value = [any_webhook]+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]++ manager = get_plugins_manager()+ line_1, line_2 = gift_card_shippable_order_line, gift_card_non_shippable_order_line+ fulfillment = order.fulfillments.create(tracking_number="123")+ fulfillment_line_1 = fulfillment.lines.create(+ order_line=line_1,+ quantity=line_1.quantity,+ stock=line_1.allocations.get().stock,+ )+ fulfillment_line_2 = fulfillment.lines.create(+ order_line=line_2,+ quantity=line_2.quantity,+ stock=line_2.allocations.get().stock,+ )+ lines_data = [+ GiftCardLineData(+ quantity=1,+ order_line=line_1,+ variant=line_1.variant,+ fulfillment_line=fulfillment_line_1,+ ),+ GiftCardLineData(+ quantity=1,+ order_line=line_2,+ variant=line_2.variant,+ fulfillment_line=fulfillment_line_2,+ ),+ ]++ # when+ gift_cards = gift_cards_create(+ order, lines_data, site_settings, staff_user, None, manager+ )++ # then+ flush_post_commit_hooks()+ assert len(gift_cards) == len(lines_data)++ gift_card = gift_cards[-1]+ gift_card_global_id = graphene.Node.to_global_id("GiftCard", gift_card.id)++ mocked_webhook_trigger.assert_called_with(+ json.dumps(+ {+ "id": gift_card_global_id,+ "is_active": True,+ "meta": generate_meta(+ requestor_data=generate_requestor(),+ ),+ },+ cls=CustomJsonEncoder,+ ),+ WebhookEventAsyncType.GIFT_CARD_CREATED,+ [any_webhook],+ gift_card,+ None,+ )++ send_notification_mock.assert_called()++def test_get_gift_card_lines(gift_card_non_shippable_order_line, gift_card_shippable_order_line, order_line):diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.pyindex 7b0e0671ff..d82483e54c 100644--- a/saleor/giftcard/utils.py+++ b/saleor/giftcard/utils.py@@ -11,6 +11,7 @@ from django.utils import timezonefrom ..checkout.models import Checkoutfrom ..core.exceptions import GiftCardNotApplicablefrom ..core.tracing import traced_atomic_transaction+from ..core.utils.events import call_eventfrom ..core.utils.promo_code import InvalidPromoCode, generate_promo_codefrom ..order.actions import OrderFulfillmentLineInfo, create_fulfillmentsfrom ..order.models import OrderLine@@ -199,6 +200,9 @@ def gift_cards_create(gift_cards = GiftCard.objects.bulk_create(gift_cards)events.gift_cards_bought_event(gift_cards, order, requestor_user, app)+ for gift_card in gift_cards:+ call_event(manager.gift_card_created, gift_card)+channel_slug = order.channel.slug# send to customer all non-shippable gift cardstransaction.on_commit(