Skip to content

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

Events and improved tests

Tomasz Szymanskia63886ce9869
diff --git a/saleor/giftcard/events.py b/saleor/giftcard/events.py
index d3596395a2..6d0e8e71b7 100644
--- a/saleor/giftcard/events.py
+++ b/saleor/giftcard/events.py
@@ -31,6 +31,18 @@ def gift_card_issued_event(
)
+def gift_card_sent(
+ gift_card_id: int, user_id: Optional[int], app_id: Optional[int], email: str
+):
+ return GiftCardEvent.objects.create(
+ gift_card_id=gift_card_id,
+ user_id=user_id,
+ app_id=app_id,
+ type=GiftCardEvents.SENT_TO_CUSTOMER,
+ parameters={"email": email},
+ )
+
+
def gift_card_balance_reset(
gift_card: GiftCard,
old_gift_card: GiftCard,
diff --git a/saleor/giftcard/notifications.py b/saleor/giftcard/notifications.py
index e0eb2ff53c..b1281d2433 100644
--- a/saleor/giftcard/notifications.py
+++ b/saleor/giftcard/notifications.py
@@ -2,10 +2,13 @@ from ..core.notifications import get_site_context
from ..core.notify_events import NotifyEventType
-def send_gift_card_notification(email, gift_card, manager):
+def send_gift_card_notification(user, app, email, gift_card, manager):
"""Trigger sending a gift card notification for the given recipient."""
payload = {
+ "user_id": user.id if user else None,
+ "app_id": app.id if app else None,
"recipient_email": email,
+ "gift_card_id": gift_card.id,
"code": gift_card.code,
**get_site_context(),
}
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index 204e85204e..3624e736c5 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -99,7 +99,7 @@ class GiftCardCreate(ModelMutation):
cls.clean_expiry_settings(cleaned_input, instance)
cls.clean_balance(cleaned_input, instance)
- cleaned_input.get("user_email", None)
+ # TODO: validate user email
return cleaned_input
@@ -182,14 +182,24 @@ class GiftCardCreate(ModelMutation):
cleaned_input["initial_balance_amount"] = amount
@classmethod
- def post_save_action(cls, info, instance, _cleaned_input):
+ def post_save_action(cls, info, instance, cleaned_input):
events.gift_card_issued_event(
gift_card=instance,
user=info.context.user,
app=info.context.app,
)
+ events.gift_card_sent(
+ gift_card_id=instance.id,
+ user_id=info.context.user.id if info.context.user else None,
+ app_id=info.context.app.id if info.context.app else None,
+ email=cleaned_input["user_email"],
+ )
send_gift_card_notification(
- info.context.user.email, instance, info.context.plugins
+ cleaned_input["created_by"],
+ cleaned_input["app"],
+ cleaned_input["user_email"],
+ instance,
+ info.context.plugins,
)
diff --git a/saleor/plugins/sendgrid/tasks.py b/saleor/plugins/sendgrid/tasks.py
index 9c2e00d979..adcadb5365 100644
--- a/saleor/plugins/sendgrid/tasks.py
+++ b/saleor/plugins/sendgrid/tasks.py
@@ -5,6 +5,7 @@ from sendgrid.helpers.mail import Mail
from ...account import events as account_events
from ...celeryconf import app
+from ...giftcard import events as giftcard_events
from ...invoice import events as invoice_events
from ...order import events as order_events
from . import SendgridConfiguration
@@ -302,6 +303,12 @@ def send_gift_card_email_task(payload: dict, configuration: dict):
template_id=configuration.send_gift_card_template_id,
payload=payload,
)
+ giftcard_events.gift_card_sent(
+ payload["gift_card_id"],
+ payload["user_id"],
+ payload["app_id"],
+ payload["recipient_email"],
+ )
@app.task(
diff --git a/saleor/plugins/sendgrid/tests/test_tasks.py b/saleor/plugins/sendgrid/tests/test_tasks.py
index a98736f722..608f97b12b 100644
--- a/saleor/plugins/sendgrid/tests/test_tasks.py
+++ b/saleor/plugins/sendgrid/tests/test_tasks.py
@@ -6,6 +6,8 @@ import pytest
from ....account import CustomerEvents
from ....account.models import CustomerEvent
from ....account.notifications import get_default_user_payload
+from ....giftcard import GiftCardEvents
+from ....giftcard.models import GiftCardEvent
from ....invoice import InvoiceEvents
from ....invoice.models import Invoice, InvoiceEvent
from ....order import OrderEvents, OrderEventsEmails
@@ -728,18 +730,16 @@ def test_send_order_refund_email_task_by_app(
@patch("saleor.plugins.sendgrid.tasks.send_email")
def test_send_gift_card_email_task_by_user(
- mocked_send_email, staff_user, order, sendgrid_email_plugin
+ mocked_send_email, staff_user, gift_card, sendgrid_email_plugin
):
template_id = "ABC1"
recipient_email = "[email protected]"
payload = {
- "order": get_default_order_payload(order, "http://localhost:8000/redirect"),
+ "gift_card_id": gift_card.id,
+ "user_id": staff_user.pk,
+ "app_id": None,
"recipient_email": recipient_email,
- "site_name": "Saleor",
- "domain": "localhost:8000",
- "requester_user_id": staff_user.pk,
- "requester_app_id": None,
}
plugin = sendgrid_email_plugin(
@@ -757,21 +757,27 @@ def test_send_gift_card_email_task_by_user(
payload=payload,
)
+ gift_card_event = GiftCardEvent.objects.get()
+ assert gift_card_event.type == GiftCardEvents.SENT_TO_CUSTOMER
+ assert gift_card_event.parameters == {
+ "email": recipient_email,
+ }
+ assert gift_card_event.user == staff_user
+ assert not gift_card_event.app
+
@patch("saleor.plugins.sendgrid.tasks.send_email")
def test_send_gift_card_email_task_by_app(
- mocked_send_email, app, order, sendgrid_email_plugin
+ mocked_send_email, app, gift_card, sendgrid_email_plugin
):
template_id = "ABC1"
recipient_email = "[email protected]"
payload = {
- "order": get_default_order_payload(order, "http://localhost:8000/redirect"),
+ "gift_card_id": gift_card.id,
+ "user_id": None,
+ "app_id": app.id,
"recipient_email": recipient_email,
- "site_name": "Saleor",
- "domain": "localhost:8000",
- "requester_user_id": None,
- "requester_app_id": app.pk,
}
plugin = sendgrid_email_plugin(
@@ -789,6 +795,14 @@ def test_send_gift_card_email_task_by_app(
payload=payload,
)
+ gift_card_event = GiftCardEvent.objects.get()
+ assert gift_card_event.type == GiftCardEvents.SENT_TO_CUSTOMER
+ assert gift_card_event.parameters == {
+ "email": recipient_email,
+ }
+ assert not gift_card_event.user
+ assert gift_card_event.app == app
+
@patch("saleor.plugins.sendgrid.tasks.send_email")
def test_send_order_confirmed_email_task_by_user(
diff --git a/saleor/plugins/user_email/tests/test_plugin.py b/saleor/plugins/user_email/tests/test_plugin.py
index 2f77afb5b6..e100d67477 100644
--- a/saleor/plugins/user_email/tests/test_plugin.py
+++ b/saleor/plugins/user_email/tests/test_plugin.py
@@ -70,6 +70,7 @@ def test_event_map():
NotifyEventType.ORDER_PAYMENT_CONFIRMATION,
NotifyEventType.ORDER_CANCELED,
NotifyEventType.ORDER_REFUND_CONFIRMATION,
+ NotifyEventType.SEND_GIFT_CARD,
],
)
@patch("saleor.plugins.user_email.plugin.get_user_event_map")