Skip to content

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

Move email events to email plugin (#6557)

Maciej Korycinski662a6f8dbe9a
diff --git a/saleor/account/events.py b/saleor/account/events.py
index de5e3b591a..bd498a8274 100644
--- a/saleor/account/events.py
+++ b/saleor/account/events.py
@@ -32,10 +32,10 @@ def customer_email_change_request_event(
def customer_email_changed_event(
- *, user: User, parameters: dict
+ *, user_id: int, parameters: dict
) -> Optional[CustomerEvent]:
return CustomerEvent.objects.create(
- user=user, type=CustomerEvents.EMAIL_CHANGED, parameters=parameters
+ user_id=user_id, type=CustomerEvents.EMAIL_CHANGED, parameters=parameters
)
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index 53386c6af9..8cca95795e 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -299,7 +299,7 @@ def _create_order(*, checkout: Checkout, order_data: dict, user: User) -> Order:
manager = get_plugins_manager()
# Send the order confirmation email
transaction.on_commit(
- lambda: send_order_confirmation(order, checkout.redirect_url, user, manager)
+ lambda: send_order_confirmation(order, checkout.redirect_url, manager)
)
# TODO we can drop it and subscribe admin and user email plugins to the same event
transaction.on_commit(
diff --git a/saleor/checkout/tests/test_checkout_complete.py b/saleor/checkout/tests/test_checkout_complete.py
index 4bb79e1a38..6a417909d0 100644
--- a/saleor/checkout/tests/test_checkout_complete.py
+++ b/saleor/checkout/tests/test_checkout_complete.py
@@ -1,20 +1,29 @@
+from unittest import mock
+
import pytest
from django.contrib.auth.models import AnonymousUser
from ...account import CustomerEvents
from ...account.models import CustomerEvent
from ...core.exceptions import InsufficientStock
+from ...core.notify_events import NotifyEventType
from ...core.taxes import zero_money, zero_taxed_money
-from ...order import OrderEvents, OrderEventsEmails
+from ...order import OrderEvents
from ...order.models import OrderEvent
+from ...order.notifications import get_default_order_payload
from ...tests.utils import flush_post_commit_hooks
from .. import calculations
from ..complete_checkout import _create_order, _prepare_order_data
from ..utils import add_variant_to_checkout
[email protected]("saleor.plugins.manager.PluginsManager.notify")
def test_create_order_captured_payment_creates_expected_events(
- checkout_with_item, customer_user, shipping_method, payment_txn_captured,
+ mock_notify,
+ checkout_with_item,
+ customer_user,
+ shipping_method,
+ payment_txn_captured,
):
checkout = checkout_with_item
checkout_user = customer_user
@@ -47,9 +56,7 @@ def test_create_order_captured_payment_creates_expected_events(
order_placed_event,
payment_captured_event,
order_fully_paid_event,
- payment_email_sent_event,
order_confirmed_event,
- order_placed_email_sent_event,
) = order.events.all() # type: OrderEvent
# Ensure the correct order event was created
@@ -90,21 +97,27 @@ def test_create_order_captured_payment_creates_expected_events(
# should not have any additional parameters
assert not order_fully_paid_event.parameters
- # Ensure the correct email sent event was created
- # should be email sent event
- assert payment_email_sent_event.type == OrderEvents.EMAIL_SENT
- # ensure the user is none or valid
- assert payment_email_sent_event.user == checkout_user
- # ensure the mail event is related to order
- assert payment_email_sent_event.order is order
- # ensure a date was set
- assert payment_email_sent_event.date
- # ensure the correct parameters were set
- assert payment_email_sent_event.parameters == {
- "email": order.get_customer_email(),
- "email_type": OrderEventsEmails.PAYMENT,
+ expected_order_payload = {
+ "order": get_default_order_payload(order, checkout.redirect_url),
+ "recipient_email": order.get_customer_email(),
+ "site_name": "mirumee.com",
+ "domain": "mirumee.com",
}
+ expected_payment_payload = {
+ "order": get_default_order_payload(order),
+ "recipient_email": order.get_customer_email(),
+ "payment": {
+ "created": payment_txn_captured.created,
+ "modified": payment_txn_captured.modified,
+ "charge_status": payment_txn_captured.charge_status,
+ "total": payment_txn_captured.total,
+ "captured_amount": payment_txn_captured.captured_amount,
+ "currency": payment_txn_captured.currency,
+ },
+ "site_name": "mirumee.com",
+ "domain": "mirumee.com",
+ }
# Ensure the correct order confirmed event was created
# should be order confirmed event
assert order_confirmed_event.type == OrderEvents.CONFIRMED
@@ -117,20 +130,15 @@ def test_create_order_captured_payment_creates_expected_events(
# ensure the event parameters are empty
assert order_confirmed_event.parameters == {}
- # Ensure the correct email sent event was created
- # should be email sent event
- assert order_placed_email_sent_event.type == OrderEvents.EMAIL_SENT
- # ensure the user is none or valid
- assert order_placed_email_sent_event.user == checkout_user
- # ensure the mail event is related to order
- assert order_placed_email_sent_event.order is order
- # ensure a date was set
- assert order_placed_email_sent_event.date
- # ensure the correct parameters were set
- assert order_placed_email_sent_event.parameters == {
- "email": order.get_customer_email(),
- "email_type": OrderEventsEmails.ORDER_CONFIRMATION,
- }
+ mock_notify.assert_has_calls(
+ [
+ mock.call(NotifyEventType.ORDER_CONFIRMATION, expected_order_payload),
+ mock.call(
+ NotifyEventType.ORDER_PAYMENT_CONFIRMATION, expected_payment_payload
+ ),
+ ],
+ any_order=True,
+ )
# Ensure the correct customer event was created if the user was not anonymous
placement_event = customer_user.events.get() # type: CustomerEvent
@@ -143,8 +151,13 @@ def test_create_order_captured_payment_creates_expected_events(
# mock_send_staff_order_confirmation.assert_called_once_with(order.pk)
[email protected]("saleor.plugins.manager.PluginsManager.notify")
def test_create_order_captured_payment_creates_expected_events_anonymous_user(
- checkout_with_item, customer_user, shipping_method, payment_txn_captured,
+ mock_notify,
+ checkout_with_item,
+ customer_user,
+ shipping_method,
+ payment_txn_captured,
):
checkout = checkout_with_item
checkout_user = None
@@ -178,9 +191,7 @@ def test_create_order_captured_payment_creates_expected_events_anonymous_user(
order_placed_event,
payment_captured_event,
order_fully_paid_event,
- payment_email_sent_event,
order_confirmed_event,
- order_placed_email_sent_event,
) = order.events.all() # type: OrderEvent
# Ensure the correct order event was created
@@ -221,19 +232,26 @@ def test_create_order_captured_payment_creates_expected_events_anonymous_user(
# should not have any additional parameters
assert not order_fully_paid_event.parameters
- # Ensure the correct email sent event was created
- # should be email sent event
- assert payment_email_sent_event.type == OrderEvents.EMAIL_SENT
- # ensure the user is none or valid
- assert payment_email_sent_event.user == checkout_user
- # ensure the mail event is related to order
- assert payment_email_sent_event.order is order
- # ensure a date was set
- assert payment_email_sent_event.date
- # ensure the correct parameters were set
- assert payment_email_sent_event.parameters == {
- "email": order.get_customer_email(),
- "email_type": OrderEventsEmails.PAYMENT,
+ expected_order_payload = {
+ "order": get_default_order_payload(order, checkout.redirect_url),
+ "recipient_email": order.get_customer_email(),
+ "site_name": "mirumee.com",
+ "domain": "mirumee.com",
+ }
+
+ expected_payment_payload = {
+ "order": get_default_order_payload(order),
+ "recipient_email": order.get_customer_email(),
+ "payment": {
+ "created": payment_txn_captured.created,
+ "modified": payment_txn_captured.modified,
+ "charge_status": payment_txn_captured.charge_status,
+ "total": payment_txn_captured.total,
+ "captured_amount": payment_txn_captured.captured_amount,
+ "currency": payment_txn_captured.currency,
+ },
+ "site_name": "mirumee.com",
+ "domain": "mirumee.com",
}
# Ensure the correct order confirmed event was created
@@ -248,27 +266,27 @@ def test_create_order_captured_payment_creates_expected_events_anonymous_user(
# ensure the event parameters are empty
assert order_confirmed_event.parameters == {}
- # Ensure the correct email sent event was created
- # should be email sent event
- assert order_placed_email_sent_event.type == OrderEvents.EMAIL_SENT
- # ensure the user is none or valid
- assert order_placed_email_sent_event.user == checkout_user
- # ensure the mail event is related to order
- assert order_placed_email_sent_event.order is order
- # ensure a date was set
- assert order_placed_email_sent_event.date
- # ensure the correct parameters were set
- assert order_placed_email_sent_event.parameters == {
- "email": order.get_customer_email(),
- "email_type": OrderEventsEmails.ORDER_CONFIRMATION,
- }
+ mock_notify.assert_has_calls(
+ [
+ mock.call(NotifyEventType.ORDER_CONFIRMATION, expected_order_payload),
+ mock.call(
+ NotifyEventType.ORDER_PAYMENT_CONFIRMATION, expected_payment_payload
+ ),
+ ],
+ any_order=True,
+ )
# Check no event was created if the user was anonymous
assert not CustomerEvent.objects.exists() # should not have created any event
[email protected]("saleor.plugins.manager.PluginsManager.notify")
def test_create_order_preauth_payment_creates_expected_events(
- checkout_with_item, customer_user, shipping_method, payment_txn_preauth,
+ mock_notify,
+ checkout_with_item,
+ customer_user,
+ shipping_method,
+ payment_txn_preauth,
):
checkout = checkout_with_item
checkout_user = customer_user
@@ -301,7 +319,6 @@ def test_create_order_preauth_payment_creates_expected_events(
order_placed_event,
payment_authorized_event,
order_confirmed_event,
- order_placed_email_sent_event,
) = order.events.all() # type: OrderEvent
# Ensure the correct order event was created
@@ -330,6 +347,13 @@ def test_create_order_preauth_payment_creates_expected_events(
assert "payment_id" in payment_authorized_event.parameters.keys()
assert "payment_gateway" in payment_authorized_event.parameters.keys()
+ expected_payload = {
+ "order": get_default_order_payload(order, checkout.redirect_url),
+ "recipient_email": order.get_customer_email(),
+ "site_name": "mirumee.com",
+ "domain": "mirumee.com",
+ }
+
# Ensure the correct order confirmed event was created
# should be order confirmed event
assert order_confirmed_event.type == OrderEvents.CONFIRMED
@@ -342,20 +366,9 @@ def test_create_order_preauth_payment_creates_expected_events(
# ensure the event parameters are empty
assert order_confirmed_event.parameters == {}
- # Ensure the correct email sent event was created
- # should be email sent event
- assert order_placed_email_sent_event.type == OrderEvents.EMAIL_SENT
- # ensure the user is none or valid
- assert order_placed_email_sent_event.user == checkout_user
- # ensure the mail event is related to order
- assert order_placed_email_sent_event.order is order
- # ensure a date was set
- assert order_placed_email_sent_event.date
- # ensure the correct parameters were set
- assert order_placed_email_sent_event.parameters == {
- "email": order.get_customer_email(),
- "email_type": OrderEventsEmails.ORDER_CONFIRMATION,
- }
+ mock_notify.assert_called_once_with(
+ NotifyEventType.ORDER_CONFIRMATION, expected_payload
+ )
# Ensure the correct customer event was created if the user was not anonymous
placement_event = customer_user.events.get() # type: CustomerEvent
@@ -368,8 +381,13 @@ def test_create_order_preauth_payment_creates_expected_events(
# mock_send_staff_order_confirmation.assert_called_once_with(order.pk)
[email protected]("saleor.plugins.manager.PluginsManager.notify")
def test_create_order_preauth_payment_creates_expected_events_anonymous_user(
- checkout_with_item, customer_user, shipping_method, payment_txn_preauth,
+ mock_notify,
+ checkout_with_item,
+ customer_user,
+ shipping_method,
+ payment_txn_preauth,
):
checkout = checkout_with_item
checkout_user = None
@@ -403,7 +421,6 @@ def test_create_order_preauth_payment_creates_expected_events_anonymous_user(
order_placed_event,
payment_captured_event,
order_confirmed_event,
- order_placed_email_sent_event,
) = order.events.all() # type: OrderEvent
# Ensure the correct order event was created
@@ -432,6 +449,12 @@ def test_create_order_preauth_payment_creates_expected_events_anonymous_user(
assert "payment_id" in payment_captured_event.parameters.keys()
assert "payment_gateway" in payment_captured_event.parameters.keys()
+ expected_payload = {
+ "order": get_default_order_payload(order, checkout.redirect_url),
+ "recipient_email": order.get_customer_email(),
+ "site_name": "mirumee.com",
+ "domain": "mirumee.com",
+ }
# Ensure the correct order confirmed event was created
# should be order confirmed event
assert order_confirmed_event.type == OrderEvents.CONFIRMED
@@ -444,20 +467,9 @@ def test_create_order_preauth_payment_creates_expected_events_anonymous_user(
# ensure the event parameters are empty
assert order_confirmed_event.parameters == {}
- # Ensure the correct email sent event was created
- # should be email sent event
- assert order_placed_email_sent_event.type == OrderEvents.EMAIL_SENT
- # ensure the user is none or valid
- assert order_placed_email_sent_event.user == checkout_user
- # ensure the mail event is related to order
- assert order_placed_email_sent_event.order is order
- # ensure a date was set
- assert order_placed_email_sent_event.date
- # ensure the correct parameters were set
- assert order_placed_email_sent_event.parameters == {
- "email": order.get_customer_email(),
- "email_type": OrderEventsEmails.ORDER_CONFIRMATION,
- }
+ mock_notify.assert_called_once_with(
+ NotifyEventType.ORDER_CONFIRMATION, expected_payload
+ )
# Check no event was created if the user was anonymous
assert not CustomerEvent.objects.exists() # should not have created any event
diff --git a/saleor/csv/events.py b/saleor/csv/events.py
index 8e1410fa45..1d9509acc3 100644
--- a/saleor/csv/events.py
+++ b/saleor/csv/events.py
@@ -54,13 +54,17 @@ def export_deleted_event(
)
-def export_file_sent_event(*, export_file: "ExportFile", user: UserType):
+def export_file_sent_event(*, export_file_id: int, user_id: int):
ExportEvent.objects.create(
- export_file=export_file, user=user, type=ExportEvents.EXPORTED_FILE_SENT
+ export_file_id=export_file_id,
+ user_id=user_id,
+ type=ExportEvents.EXPORTED_FILE_SENT,
)
-def export_failed_info_sent_event(*, export_file: "ExportFile", user: UserType):
+def export_failed_info_sent_event(*, export_file_id: int, user_id: int):
ExportEvent.objects.create(
- export_file=export_file, user=user, type=ExportEvents.EXPORT_FAILED_INFO_SENT
+ export_file_id=export_file_id,
+ user_id=user_id,
+ type=ExportEvents.EXPORT_FAILED_INFO_SENT,
)
diff --git a/saleor/csv/notifications.py b/saleor/csv/notifications.py
index 4e6cb169c9..02aa065c1e 100644
--- a/saleor/csv/notifications.py
+++ b/saleor/csv/notifications.py
@@ -4,7 +4,6 @@ from ..core.notifications import get_site_context
from ..core.notify_events import NotifyEventType
from ..core.utils import build_absolute_uri
from ..plugins.manager import get_plugins_manager
-from . import events
if TYPE_CHECKING:
from .models import ExportFile
@@ -18,6 +17,7 @@ def get_default_export_payload(export_file: "ExportFile") -> dict:
"user_id": user_id,
"user_email": user_email,
"app_id": app_id,
+ "id": export_file.id,
"status": export_file.status,
"message": export_file.message,
"created_at": export_file.created_at,
@@ -35,8 +35,7 @@ def send_export_download_link_notification(export_file: "ExportFile"):
}
manager = get_plugins_manager()
- manager.notify(NotifyEventType.CSV_PRODUCT_EXPORT_SUCCESS, payload) # type: ignore
- events.export_file_sent_event(export_file=export_file, user=export_file.user)
+ manager.notify(NotifyEventType.CSV_PRODUCT_EXPORT_SUCCESS, payload)
def send_export_failed_info(export_file: "ExportFile"):
@@ -47,5 +46,4 @@ def send_export_failed_info(export_file: "ExportFile"):
**get_site_context(),
}
manager = get_plugins_manager()
- manager.notify(NotifyEventType.CSV_EXPORT_FAILED, payload) # type: ignore
- events.export_failed_info_sent_event(export_file=export_file, user=export_file.user)
+ manager.notify(NotifyEventType.CSV_EXPORT_FAILED, payload)
diff --git a/saleor/csv/tests/test_notifications.py b/saleor/csv/tests/test_notifications.py
index 58036969ed..4e362103a0 100644
--- a/saleor/csv/tests/test_notifications.py
+++ b/saleor/csv/tests/test_notifications.py
@@ -6,8 +6,7 @@ from freezegun import freeze_time
from ...core.notifications import get_site_context
from ...core.notify_events import AdminNotifyEvent
from ...core.utils import build_absolute_uri
-from .. import ExportEvents, notifications
-from ..models import ExportEvent
+from .. import notifications
from ..notifications import get_default_export_payload
@@ -38,12 +37,6 @@ def test_send_export_download_link_notification(
AdminNotifyEvent.CSV_PRODUCT_EXPORT_SUCCESS, expected_payload
)
- assert ExportEvent.objects.filter(
- export_file=user_export_file,
- user=user_export_file.user,
- type=ExportEvents.EXPORTED_FILE_SENT,
- ).exists()
-
@freeze_time("2018-05-31 12:00:01")
@mock.patch("saleor.plugins.manager.PluginsManager.notify")
@@ -70,9 +63,3 @@ def test_send_export_failed_info(
mocked_notify.assert_called_once_with(
AdminNotifyEvent.CSV_EXPORT_FAILED, expected_payload
)
-
- assert ExportEvent.objects.filter(
- export_file=user_export_file,
- user=user_export_file.user,
- type=ExportEvents.EXPORT_FAILED_INFO_SENT,
- )
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.py
index 5f0bd29d34..01e6e16c98 100644
--- a/saleor/graphql/account/mutations/account.py
+++ b/saleor/graphql/account/mutations/account.py
@@ -456,9 +456,4 @@ class ConfirmEmailChange(BaseMutation):
notifications.send_user_change_email_notification(
old_email, user, info.context.plugins
)
- event_parameters = {"old_email": old_email, "new_email": new_email}
-
- account_events.customer_email_changed_event(
- user=user, parameters=event_parameters
- )
return ConfirmEmailChange(user=user)
diff --git a/saleor/graphql/invoice/mutations.py b/saleor/graphql/invoice/mutations.py
index 30db3ee4f3..2e4c895384 100644
--- a/saleor/graphql/invoice/mutations.py
+++ b/saleor/graphql/invoice/mutations.py
@@ -304,9 +304,4 @@ class InvoiceSendNotification(ModelMutation):
instance = cls.get_instance(info, **data)
cls.clean_instance(info, instance)
send_invoice(instance, info.context.user, info.context.plugins)
- order_events.invoice_sent_event(
- order=instance.order,
- user=info.context.user,
- email=instance.order.get_customer_email(),
- )
return InvoiceSendNotification(invoice=instance)
diff --git a/saleor/graphql/invoice/tests/test_send_invoice_notification.py b/saleor/graphql/invoice/tests/test_send_invoice_notification.py
index e9aa1a680b..63b5ae1e1f 100644
--- a/saleor/graphql/invoice/tests/test_send_invoice_notification.py
+++ b/saleor/graphql/invoice/tests/test_send_invoice_notification.py
@@ -5,7 +5,7 @@ import graphene
from ....core import JobStatus
from ....core.notify_events import NotifyEventType
from ....graphql.tests.utils import get_graphql_content
-from ....invoice.models import Invoice, InvoiceEvent, InvoiceEvents
+from ....invoice.models import Invoice
from ....invoice.notifications import get_invoice_payload
from ....order import OrderEvents
@@ -38,6 +38,7 @@ def test_invoice_send_notification(
)
content = get_graphql_content(response)
expected_payload = {
+ "requester_user_id": staff_api_client.user.id,
"invoice": get_invoice_payload(invoice),
"recipient_email": invoice.order.get_customer_email(),
"domain": "mirumee.com",
@@ -46,18 +47,6 @@ def test_invoice_send_notification(
mock_notify.assert_called_once_with(NotifyEventType.INVOICE_READY, expected_payload)
assert not content["data"]["invoiceSendNotification"]["invoiceErrors"]
- assert InvoiceEvent.objects.filter(
- type=InvoiceEvents.SENT,
- user=staff_api_client.user,
- invoice=invoice,
- parameters__email=order.user.email,
- ).exists()
- assert order.events.filter(
- type=OrderEvents.INVOICE_SENT,
- order=order,
- user=staff_api_client.user,
- parameters__email=order.user.email,
- ).exists()
@patch("saleor.plugins.manager.PluginsManager.notify")
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 6111d144e7..8757c51b7d 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -1,5 +1,6 @@
import uuid
from datetime import date, timedelta
+from unittest import mock
from unittest.mock import ANY, MagicMock, Mock, call, patch
import graphene
@@ -2986,8 +2987,13 @@ def test_order_cancel_as_app(
mock_cancel_order.assert_called_once_with(order=order, user=AnonymousUser())
[email protected]("saleor.plugins.manager.PluginsManager.notify")
def test_order_capture(
- staff_api_client, permission_manage_orders, payment_txn_preauth, staff_user
+ mocked_notify,
+ staff_api_client,
+ permission_manage_orders,
+ payment_txn_preauth,
+ staff_user,
):
order = payment_txn_preauth.order
query = """
@@ -3019,7 +3025,7 @@ def test_order_capture(
assert data["isPaid"]
assert data["totalCaptured"]["amount"] == float(amount)
- event_captured, event_order_fully_paid, event_email_sent = order.events.all()
+ event_captured, event_order_fully_paid = order.events.all()
assert event_captured.type == order_events.OrderEvents.PAYMENT_CAPTURED
assert event_captured.user == staff_user
@@ -3032,12 +3038,26 @@ def test_order_capture(
assert event_order_fully_paid.type == order_events.OrderEvents.ORDER_FULLY_PAID
assert event_order_fully_paid.user == staff_user
- assert event_email_sent.user == staff_user
- assert event_email_sent.parameters == {
- "email": order.user_email,
- "email_type": order_events.OrderEventsEmails.PAYMENT,
+ payment = Payment.objects.get()
+ expected_payment_payload = {
+ "order": get_default_order_payload(order),
+ "recipient_email": order.get_customer_email(),
+ "payment": {
+ "created": payment.created,
+ "modified": payment.modified,
+ "charge_status": payment.charge_status,
+ "total": payment.total,
+ "captured_amount": payment.captured_amount,
+ "currency": payment.currency,
+ },
+ "site_name": "mirumee.com",
+ "domain": "mirumee.com",
}
+ mocked_notify.assert_called_once_with(
+ NotifyEventType.ORDER_PAYMENT_CONFIRMATION, expected_payment_payload
+ )
+
MUTATION_MARK_ORDER_AS_PAID = """
mutation markPaid($id: ID!, $transaction: String) {
@@ -3246,11 +3266,6 @@ def test_order_refund(staff_api_client, permission_manage_orders, payment_txn_ca
).first()
assert refund_order_event.parameters["amount"] == str(amount)
- email_send_event = order.events.filter(
- type=order_events.OrderEvents.EMAIL_SENT
- ).first()
- assert email_send_event.parameters["email_type"]
-
@pytest.mark.parametrize(
"requires_amount, mutation_name",
diff --git a/saleor/invoice/events.py b/saleor/invoice/events.py
index 679eef3dc2..73b94e7aaa 100644
--- a/saleor/invoice/events.py
+++ b/saleor/invoice/events.py
@@ -59,12 +59,12 @@ def invoice_deleted_event(*, user: UserType, invoice_id: int) -> InvoiceEvent:
)
-def invoice_sent_event(*, user: UserType, invoice: Invoice) -> InvoiceEvent:
- if not _user_is_valid(user):
- user = None
+def notification_invoice_sent_event(
+ *, user_id: Optional[int], invoice_id: int, customer_email: str
+) -> InvoiceEvent:
return InvoiceEvent.objects.create(
type=InvoiceEvents.SENT,
- user=user,
- invoice=invoice,
- parameters={"email": invoice.order.get_customer_email()}, # type: ignore
+ user_id=user_id,
+ invoice_id=invoice_id,
+ parameters={"email": customer_email}, # type: ignore
)
diff --git a/saleor/invoice/notifications.py b/saleor/invoice/notifications.py
index 6198b1aec2..b7258fa7ad 100644
--- a/saleor/invoice/notifications.py
+++ b/saleor/invoice/notifications.py
@@ -1,7 +1,6 @@
from ..account.models import User
from ..core.notifications import get_site_context
from ..core.notify_events import NotifyEventType
-from ..invoice import events
from ..plugins.manager import PluginsManager
from .models import Invoice
@@ -11,6 +10,7 @@ def get_invoice_payload(invoice):
"id": invoice.id,
"number": invoice.number,
"download_url": invoice.url,
+ "order_id": invoice.order.id,
}
@@ -19,8 +19,8 @@ def send_invoice(invoice: "Invoice", staff_user: "User", manager: "PluginsManage
payload = {
"invoice": get_invoice_payload(invoice),
"recipient_email": invoice.order.get_customer_email(), # type: ignore
+ "requester_user_id": staff_user.id,
**get_site_context(),
}
manager.notify(NotifyEventType.INVOICE_READY, payload) # type: ignore
manager.invoice_sent(invoice, invoice.order.get_customer_email()) # type: ignore
- events.invoice_sent_event(user=staff_user, invoice=invoice)
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index edd9924c4a..1cfb9109cb 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -84,9 +84,6 @@ def handle_fully_paid_order(order: "Order", user: Optional["User"] = None):
events.order_fully_paid_event(order=order, user=user)
manager = get_plugins_manager()
if order.get_customer_email():
- events.email_sent_event(
- order=order, user=user, email_type=events.OrderEventsEmails.PAYMENT
- )
send_payment_confirmation(order, manager)
if utils.order_needs_automatic_fulfillment(order):
diff --git a/saleor/order/events.py b/saleor/order/events.py
index 07f97c6f0a..fb2e1cb40f 100644
--- a/saleor/order/events.py
+++ b/saleor/order/events.py
@@ -32,6 +32,20 @@ def _user_is_valid(user: UserType) -> bool:
return bool(user and not user.is_anonymous)
+def event_order_refunded_notification(
+ order_id: int, user_id: Optional[int], customer_email: str
+):
+ return OrderEvent.objects.create(
+ order_id=order_id,
+ type=OrderEvents.EMAIL_SENT,
+ parameters={
+ "email": customer_email,
+ "email_type": OrderEventsEmails.ORDER_REFUND,
+ },
+ user_id=user_id,
+ )
+
+
def event_order_confirmed_notification(
order_id: int, user_id: Optional[int], customer_email: str
):
@@ -46,26 +60,70 @@ def event_order_confirmed_notification(
)
-def email_sent_event(
- *,
- order: Order,
- user: Optional[UserType],
- email_type: str, # use "OrderEventsEmails" class
- user_pk: int = None,
-) -> OrderEvent:
+def event_order_cancelled_notification(
+ order_id: int, user_id: Optional[int], customer_email: str
+):
+ return OrderEvent.objects.create(
+ order_id=order_id,
+ type=OrderEvents.EMAIL_SENT,
+ parameters={
+ "email": customer_email,
+ "email_type": OrderEventsEmails.ORDER_CANCEL,
+ },
+ user_id=user_id,
+ )
- if user and not user.is_anonymous:
- kwargs: Dict[str, Union[User, int]] = {"user": user}
- elif user_pk:
- kwargs = {"user_id": user_pk}
- else:
- kwargs = {}
+def event_order_confirmation_notification(
+ order_id: int, user_id: Optional[int], customer_email: str
+):
return OrderEvent.objects.create(
- order=order,
+ order_id=order_id,
type=OrderEvents.EMAIL_SENT,
- parameters={"email": order.get_customer_email(), "email_type": email_type},
- **kwargs,
+ parameters={
+ "email": customer_email,
+ "email_type": OrderEventsEmails.ORDER_CONFIRMATION,
+ },
+ user_id=user_id,
+ )
+
+
+def event_fulfillment_confirmed_notification(
+ order_id: int, user_id: Optional[int], customer_email: str
+):
+ return OrderEvent.objects.create(
+ order_id=order_id,
+ type=OrderEvents.EMAIL_SENT,
+ parameters={
+ "email": customer_email,
+ "email_type": OrderEventsEmails.FULFILLMENT,
+ },
+ user_id=user_id,
+ )
+
+
+def event_fulfillment_digital_links_notification(
+ order_id: int, user_id: Optional[int], customer_email: str
+):
+ return OrderEvent.objects.create(
+ order_id=order_id,
+ type=OrderEvents.EMAIL_SENT,
+ parameters={
+ "email": customer_email,
+ "email_type": OrderEventsEmails.DIGITAL_LINKS,
+ },
+ user_id=user_id,
+ )
+
+
+def event_payment_confirmed_notification(
+ order_id: int, user_id: Optional[int], customer_email: str
+):
+ return OrderEvent.objects.create(
+ order_id=order_id,
+ type=OrderEvents.EMAIL_SENT,
+ parameters={"email": customer_email, "email_type": OrderEventsEmails.PAYMENT},
+ user_id=user_id,
)
@@ -102,13 +160,13 @@ def invoice_updated_event(
)
-def invoice_sent_event(
- *, order: Order, user: Optional[UserType], email: str,
+def event_invoice_sent_notification(
+ *, order_id: int, user_id: Optional[int], email: str
) -> OrderEvent:
return OrderEvent.objects.create(
- order=order,
+ order_id=order_id,
type=OrderEvents.INVOICE_SENT,
- user=user,
+ user_id=user_id,
parameters={"email": email},
)
diff --git a/saleor/order/notifications.py b/saleor/order/notifications.py
index 66a23cf023..0c69746f81 100644
--- a/saleor/order/notifications.py
+++ b/saleor/order/notifications.py
@@ -8,7 +8,6 @@ from ..core.notifications import get_site_context
from ..core.notify_events import NotifyEventType
from ..core.utils.url import prepare_url
from ..product.models import DigitalContentUrl
-from . import events
from .models import FulfillmentLine, Order, OrderLine
if TYPE_CHECKING:
@@ -89,6 +88,7 @@ ORDER_MODEL_FIELDS = [
"status",
"metadata",
"private_metadata",
+ "user_id",
]
@@ -158,7 +158,7 @@ def prepare_order_details_url(order: Order, redirect_url: str) -> str:
return prepare_url(params, redirect_url)
-def send_order_confirmation(order, redirect_url, user, manager):
+def send_order_confirmation(order, redirect_url, manager):
"""Send notification with order confirmation."""
payload = {
"order": get_default_order_payload(order, redirect_url),
@@ -166,12 +166,6 @@ def send_order_confirmation(order, redirect_url, user, manager):
**get_site_context(),
}
manager.notify(NotifyEventType.ORDER_CONFIRMATION, payload)
- events.email_sent_event(
- order=order,
- user=None,
- user_pk=user.pk if user else None,
- email_type=events.OrderEventsEmails.ORDER_CONFIRMATION,
- )
def send_order_confirmed(order, user, manager):
@@ -204,19 +198,9 @@ def send_staff_order_confirmation(order, redirect_url, manager):
def send_fulfillment_confirmation_to_customer(order, fulfillment, user, manager):
payload = get_default_fulfillment_payload(order, fulfillment)
+ payload["requester_user_id"] = user.id if user else None
manager.notify(NotifyEventType.ORDER_FULFILLMENT_CONFIRMATION, payload=payload)
- events.email_sent_event(
- order=order, user=user, email_type=events.OrderEventsEmails.FULFILLMENT
- )
-
- # If digital lines were sent in the fulfillment email,
- # trigger the event
- if any((line for line in order if line.variant.is_digital())):
- events.email_sent_event(
- order=order, user=user, email_type=events.OrderEventsEmails.DIGITAL_LINKS
- )
-
def send_fulfillment_update(order, fulfillment, manager):
payload = get_default_fulfillment_payload(order, fulfillment)
@@ -244,20 +228,19 @@ def send_payment_confirmation(order, manager):
def send_order_canceled_confirmation(order: "Order", user: Optional["User"], manager):
payload = {
+ "requester_user_id": user.id if user else None,
"order": get_default_order_payload(order),
"recipient_email": order.get_customer_email(),
**get_site_context(),
}
manager.notify(NotifyEventType.ORDER_CANCELED, payload)
- events.email_sent_event(
- order=order, user=user, email_type=events.OrderEventsEmails.ORDER_CANCEL
- )
def send_order_refunded_confirmation(
order: "Order", user: Optional["User"], amount: "Decimal", currency: str, manager
):
payload = {
+ "requester_user_id": user.id if user else None,
"order": get_default_order_payload(order),
"recipient_email": order.get_customer_email(),
"amount": amount,
@@ -265,6 +248,3 @@ def send_order_refunded_confirmation(
**get_site_context(),
}
manager.notify(NotifyEventType.ORDER_REFUND_CONFIRMATION, payload)
- events.email_sent_event(
- order=order, user=user, email_type=events.OrderEventsEmails.ORDER_REFUND
- )
diff --git a/saleor/order/tests/test_notifications.py b/saleor/order/tests/test_notifications.py
index 896a1c26c5..c8c275f374 100644
--- a/saleor/order/tests/test_notifications.py
+++ b/saleor/order/tests/test_notifications.py
@@ -188,7 +188,7 @@ def test_send_email_order_confirmation(mocked_notify, order, site_settings):
manager = get_plugins_manager()
redirect_url = "https://www.example.com"
- notifications.send_order_confirmation(order, redirect_url, order.user, manager)
+ notifications.send_order_confirmation(order, redirect_url, manager)
expected_payload = {
"order": get_default_order_payload(order, redirect_url),
@@ -259,7 +259,7 @@ def test_send_confirmation_emails_without_addresses_for_order(
redirect_url = "https://www.example.com"
manager = get_plugins_manager()
- notifications.send_order_confirmation(order, redirect_url, order.user, manager)
+ notifications.send_order_confirmation(order, redirect_url, manager)
expected_payload = {
"order": get_default_order_payload(order, redirect_url),
@@ -274,7 +274,9 @@ def test_send_confirmation_emails_without_addresses_for_order(
@mock.patch("saleor.plugins.manager.PluginsManager.notify")
-def test_send_fulfillment_confirmation(mocked_notify, fulfilled_order, site_settings):
+def test_send_fulfillment_confirmation(
+ mocked_notify, fulfilled_order, site_settings, staff_user
+):
fulfillment = fulfilled_order.fulfillments.first()
fulfillment.tracking_number = "https://www.example.com"
fulfillment.save()
@@ -283,12 +285,12 @@ def test_send_fulfillment_confirmation(mocked_notify, fulfilled_order, site_sett
notifications.send_fulfillment_confirmation_to_customer(
order=fulfilled_order,
fulfillment=fulfillment,
- user=fulfilled_order.user,
+ user=staff_user,
manager=manager,
)
expected_payload = get_default_fulfillment_payload(fulfilled_order, fulfillment)
-
+ expected_payload["requester_user_id"] = staff_user.id
mocked_notify.assert_called_once_with(
NotifyEventType.ORDER_FULFILLMENT_CONFIRMATION, payload=expected_payload
)
@@ -313,12 +315,12 @@ def test_send_fulfillment_update(mocked_notify, fulfilled_order, site_settings):
@mock.patch("saleor.plugins.manager.PluginsManager.notify")
-def test_send_email_order_canceled(mocked_notify, order, site_settings):
+def test_send_email_order_canceled(mocked_notify, order, site_settings, staff_user):
# given
manager = get_plugins_manager()
# when
- notifications.send_order_canceled_confirmation(order, order.user, manager)
+ notifications.send_order_canceled_confirmation(order, staff_user, manager)
# then
expected_payload = {
@@ -326,6 +328,7 @@ def test_send_email_order_canceled(mocked_notify, order, site_settings):
"recipient_email": order.get_customer_email(),
"site_name": "mirumee.com",
"domain": "mirumee.com",
+ "requester_user_id": staff_user.id,
}
mocked_notify.assert_called_once_with(
NotifyEventType.ORDER_CANCELED, expected_payload
@@ -333,18 +336,19 @@ def test_send_email_order_canceled(mocked_notify, order, site_settings):
@mock.patch("saleor.plugins.manager.PluginsManager.notify")
-def test_send_email_order_refunded(mocked_notify, order, site_settings):
+def test_send_email_order_refunded(mocked_notify, order, site_settings, staff_user):
# given
manager = get_plugins_manager()
amount = order.total.gross.amount
# when
notifications.send_order_refunded_confirmation(
- order, order.user, amount, order.currency, manager
+ order, staff_user, amount, order.currency, manager
)
# then
expected_payload = {
+ "requester_user_id": staff_user.id,
"order": get_default_order_payload(order),
"amount": amount,
"currency": order.currency,
diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.py
index 17bc6e3f41..175131bff4 100644
--- a/saleor/order/tests/test_order.py
+++ b/saleor/order/tests/test_order.py
@@ -20,7 +20,15 @@ from ...product.models import Collection
from ...warehouse.models import Stock
from ...warehouse.tests.utils import get_quantity_allocated_for_stock
from .. import OrderEvents, OrderStatus, models
-from ..events import OrderEvent, OrderEventsEmails, email_sent_event
+from ..events import (
+ OrderEventsEmails,
+ event_fulfillment_confirmed_notification,
+ event_fulfillment_digital_links_notification,
+ event_order_cancelled_notification,
+ event_order_confirmation_notification,
+ event_order_refunded_notification,
+ event_payment_confirmed_notification,
+)
from ..models import Order
from ..notifications import (
get_default_fulfillment_payload,
@@ -777,53 +785,27 @@ def test_send_fulfillment_order_lines_mails(
send_fulfillment_confirmation_to_customer(
order=order, fulfillment=fulfillment, user=staff_user, manager=manager
)
- events = OrderEvent.objects.all()
-
expected_payload = get_default_fulfillment_payload(order, fulfillment)
+ expected_payload["requester_user_id"] = staff_user.id
mocked_notify.assert_called_once_with(
"order_fulfillment_confirmation", payload=expected_payload
)
- # Ensure the standard fulfillment event was triggered
- assert events[0].user == staff_user
- assert events[0].parameters == {
- "email": order.user_email,
- "email_type": OrderEventsEmails.FULFILLMENT,
- }
-
- if has_digital:
- assert len(events) == 2
- assert events[1].user == staff_user
- assert events[1].parameters == {
- "email": order.user_email,
- "email_type": OrderEventsEmails.DIGITAL_LINKS,
- }
- else:
- assert len(events) == 1
-
-def test_email_sent_event_with_user_pk(order):
- user = order.user
- email_type = OrderEventsEmails.PAYMENT
- email_sent_event(order=order, user=None, email_type=email_type, user_pk=user.pk)
- events = order.events.all()
- assert len(events) == 1
- event = events[0]
- assert event
- assert event.type == OrderEvents.EMAIL_SENT
- assert event.user == user
- assert event.order is order
- assert event.date
- assert event.parameters == {
- "email": order.get_customer_email(),
- "email_type": email_type,
- }
-
-
-def test_email_sent_event_with_user(order):
+ "event_fun, expected_event_type",
+ [
+ (event_order_cancelled_notification, OrderEventsEmails.ORDER_CANCEL),
+ (event_order_confirmation_notification, OrderEventsEmails.ORDER_CONFIRMATION),
+ (event_fulfillment_confirmed_notification, OrderEventsEmails.FULFILLMENT),
+ (event_fulfillment_digital_links_notification, OrderEventsEmails.DIGITAL_LINKS),
+ (event_payment_confirmed_notification, OrderEventsEmails.PAYMENT),
+ (event_order_refunded_notification, OrderEventsEmails.ORDER_REFUND),
+ ],
+)
+def test_email_sent_event_with_user(order, event_fun, expected_event_type):
user = order.user
- email_type = OrderEventsEmails.PAYMENT
- email_sent_event(order=order, user=user, email_type=email_type)
+ event_fun(order_id=order.id, user_id=user.pk, customer_email=order.user_email)
events = order.events.all()
assert len(events) == 1
event = events[0]
@@ -834,13 +816,23 @@ def test_email_sent_event_with_user(order):
assert event.date
assert event.parameters == {
"email": order.get_customer_email(),
- "email_type": email_type,
+ "email_type": expected_event_type,
}
-def test_email_sent_event_without_user_and_user_pk(order):
- email_type = OrderEventsEmails.PAYMENT
- email_sent_event(order=order, user=None, email_type=email_type)
+ "event_fun, expected_event_type",
+ [
+ (event_order_cancelled_notification, OrderEventsEmails.ORDER_CANCEL),
+ (event_order_confirmation_notification, OrderEventsEmails.ORDER_CONFIRMATION),
+ (event_fulfillment_confirmed_notification, OrderEventsEmails.FULFILLMENT),
+ (event_fulfillment_digital_links_notification, OrderEventsEmails.DIGITAL_LINKS),
+ (event_payment_confirmed_notification, OrderEventsEmails.PAYMENT),
+ (event_order_refunded_notification, OrderEventsEmails.ORDER_REFUND),
+ ],
+)
+def test_email_sent_event_without_user_pk(order, event_fun, expected_event_type):
+ event_fun(order_id=order.id, user_id=None, customer_email=order.user_email)
events = order.events.all()
assert len(events) == 1
event = events[0]
@@ -851,5 +843,5 @@ def test_email_sent_event_without_user_and_user_pk(order):
assert event.date
assert event.parameters == {
"email": order.get_customer_email(),
- "email_type": email_type,
+ "email_type": expected_event_type,
}
diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.py
index 24b95a5b13..07be0a0e85 100644
--- a/saleor/order/tests/test_order_actions.py
+++ b/saleor/order/tests/test_order_actions.py
@@ -9,7 +9,7 @@ from ...payment.models import Payment
from ...product.models import DigitalContent
from ...product.tests.utils import create_image
from ...warehouse.models import Allocation, Stock
-from .. import FulfillmentStatus, OrderEvents, OrderEventsEmails, OrderStatus
+from .. import FulfillmentStatus, OrderEvents, OrderStatus
from ..actions import (
automatically_fulfill_digital_lines,
cancel_fulfillment,
@@ -85,26 +85,10 @@ def test_handle_fully_paid_order_digital_lines(
handle_fully_paid_order(order)
fulfillment = order.fulfillments.first()
- (
- event_order_paid,
- event_email_sent,
- event_order_fulfilled,
- event_digital_links,
- ) = order.events.all()
+ event_order_paid = order.events.get()
assert event_order_paid.type == OrderEvents.ORDER_FULLY_PAID
- assert event_email_sent.type == OrderEvents.EMAIL_SENT
- assert event_order_fulfilled.type == OrderEvents.EMAIL_SENT
- assert event_digital_links.type == OrderEvents.EMAIL_SENT
-
- assert (
- event_order_fulfilled.parameters["email_type"] == OrderEventsEmails.FULFILLMENT
- )
- assert (
- event_digital_links.parameters["email_type"] == OrderEventsEmails.DIGITAL_LINKS
- )
-
mock_send_payment_confirmation.assert_called_once_with(order, manager)
send_fulfillment_confirmation_to_customer.assert_called_once_with(
order, fulfillment, user=order.user, manager=manager
@@ -121,15 +105,9 @@ def test_handle_fully_paid_order(
):
order.payments.add(Payment.objects.create())
handle_fully_paid_order(order)
- event_order_paid, event_email_sent = order.events.all()
+ event_order_paid = order.events.get()
assert event_order_paid.type == OrderEvents.ORDER_FULLY_PAID
- assert event_email_sent.type == OrderEvents.EMAIL_SENT
- assert event_email_sent.parameters == {
- "email": order.get_customer_email(),
- "email_type": OrderEventsEmails.PAYMENT,
- }
-
mock_send_payment_confirmation.assert_called_once_with(
order, mocked_get_plugins_manager()
)
diff --git a/saleor/plugins/admin_email/tasks.py b/saleor/plugins/admin_email/tasks.py
index 4779f03747..08fc7b852a 100644
--- a/saleor/plugins/admin_email/tasks.py
+++ b/saleor/plugins/admin_email/tasks.py
@@ -1,4 +1,5 @@
from ...celeryconf import app
+from ...csv.events import export_failed_info_sent_event, export_file_sent_event
from ..email_common import (
EmailConfig,
get_email_subject,
@@ -52,6 +53,9 @@ def send_email_with_link_to_download_file_task(
template_str=email_template_str,
context=payload,
)
+ export_file_sent_event(
+ export_file_id=payload["export"]["id"], user_id=payload["export"].get("user_id")
+ )
@app.task
@@ -74,6 +78,9 @@ def send_export_failed_email_task(recipient_email: str, payload: dict, config: d
template_str=email_template_str,
context=payload,
)
+ export_failed_info_sent_event(
+ export_file_id=payload["export"]["id"], user_id=payload["export"].get("user_id")
+ )
@app.task
diff --git a/saleor/plugins/admin_email/tests/test_notify_events.py b/saleor/plugins/admin_email/tests/test_notify_events.py
index 474d6342a4..48e92dd450 100644
--- a/saleor/plugins/admin_email/tests/test_notify_events.py
+++ b/saleor/plugins/admin_email/tests/test_notify_events.py
@@ -1,7 +1,6 @@
from unittest import mock
-from saleor.order.notifications import get_default_order_payload
-
+from ....order.notifications import get_default_order_payload
from ..notify_events import (
send_csv_export_failed,
send_csv_product_export_success,
diff --git a/saleor/plugins/admin_email/tests/test_tasks.py b/saleor/plugins/admin_email/tests/test_tasks.py
index 2625d22caf..d2124a8985 100644
--- a/saleor/plugins/admin_email/tests/test_tasks.py
+++ b/saleor/plugins/admin_email/tests/test_tasks.py
@@ -1,6 +1,8 @@
from unittest import mock
from ....account.notifications import get_default_user_payload
+from ....csv import ExportEvents
+from ....csv.models import ExportEvent
from ....csv.notifications import get_default_export_payload
from ....order.notifications import get_default_order_payload
from ...email_common import EmailConfig
@@ -86,6 +88,11 @@ def test_send_email_with_link_to_download_file_task_default_template(
# confirm that mail has correct structure and email was sent
assert mocked_send_mail.called
+ assert ExportEvent.objects.filter(
+ export_file=user_export_file,
+ user=user_export_file.user,
+ type=ExportEvents.EXPORTED_FILE_SENT,
+ ).exists()
@mock.patch("saleor.plugins.admin_email.tasks.send_email")
@@ -121,6 +128,11 @@ def test_send_email_with_link_to_download_file_task_custom_template(
subject=expected_subject,
template_str=expected_template_str,
)
+ assert ExportEvent.objects.filter(
+ export_file=user_export_file,
+ user=user_export_file.user,
+ type=ExportEvents.EXPORTED_FILE_SENT,
+ ).exists()
@mock.patch("saleor.plugins.email_common.send_mail")
@@ -139,6 +151,11 @@ def test_send_export_failed_email_task_default_template(
# confirm that mail has correct structure and email was sent
assert mocked_send_mail.called
+ assert ExportEvent.objects.filter(
+ export_file=user_export_file,
+ user=user_export_file.user,
+ type=ExportEvents.EXPORT_FAILED_INFO_SENT,
+ )
@mock.patch("saleor.plugins.admin_email.tasks.send_email")
@@ -169,6 +186,11 @@ def test_send_export_failed_email_task_custom_template(
subject=expected_subject,
template_str=expected_template_str,
)
+ assert ExportEvent.objects.filter(
+ export_file=user_export_file,
+ user=user_export_file.user,
+ type=ExportEvents.EXPORT_FAILED_INFO_SENT,
+ )
@mock.patch("saleor.plugins.email_common.send_mail")
diff --git a/saleor/plugins/user_email/tasks.py b/saleor/plugins/user_email/tasks.py
index fb6508ce5c..b844389f25 100644
--- a/saleor/plugins/user_email/tasks.py
+++ b/saleor/plugins/user_email/tasks.py
@@ -1,5 +1,6 @@
from ...account import events as account_events
from ...celeryconf import app
+from ...invoice import events as invoice_events
from ...order import events as order_events
from ..email_common import (
EmailConfig,
@@ -95,6 +96,7 @@ def send_request_email_change_email_task(recipient_email, payload, config):
@app.task
def send_user_change_email_notification_task(recipient_email, payload, config):
+ user_id = payload.get("user", {}).get("id")
email_config = EmailConfig(**config)
email_template_str = get_email_template_or_default(
@@ -116,6 +118,14 @@ def send_user_change_email_notification_task(recipient_email, payload, config):
subject=subject,
template_str=email_template_str,
)
+ event_parameters = {
+ "old_email": payload["old_email"],
+ "new_email": payload["new_email"],
+ }
+
+ account_events.customer_email_changed_event(
+ user_id=user_id, parameters=event_parameters
+ )
@app.task
@@ -192,6 +202,16 @@ def send_invoice_email_task(recipient_email, payload, config):
subject=subject,
template_str=email_template_str,
)
+ invoice_events.notification_invoice_sent_event(
+ user_id=payload["requester_user_id"],
+ invoice_id=payload["invoice"]["id"],
+ customer_email=payload["recipient_email"],
+ )
+ order_events.event_invoice_sent_notification(
+ order_id=payload["invoice"]["order_id"],
+ user_id=payload["requester_user_id"],
+ email=payload["recipient_email"],
+ )
@app.task
@@ -218,6 +238,11 @@ def send_order_confirmation_email_task(recipient_email, payload, config):
subject=subject,
template_str=email_template_str,
)
+ order_events.event_order_confirmation_notification(
+ order_id=payload["order"]["id"],
+ user_id=payload["order"].get("user_id"),
+ customer_email=recipient_email,
+ )
@app.task
@@ -243,6 +268,18 @@ def send_fulfillment_confirmation_email_task(recipient_email, payload, config):
subject=subject,
template_str=email_template_str,
)
+ order_events.event_fulfillment_confirmed_notification(
+ order_id=payload["order"]["id"],
+ user_id=payload["requester_user_id"],
+ customer_email=recipient_email,
+ )
+
+ if payload.get("digital_lines"):
+ order_events.event_fulfillment_digital_links_notification(
+ order_id=payload["order"]["id"],
+ user_id=payload["requester_user_id"],
+ customer_email=recipient_email,
+ )
@app.task
@@ -291,6 +328,11 @@ def send_payment_confirmation_email_task(recipient_email, payload, config):
subject=subject,
template_str=email_template_str,
)
+ order_events.event_payment_confirmed_notification(
+ order_id=payload["order"]["id"],
+ user_id=payload["order"].get("user_id"),
+ customer_email=recipient_email,
+ )
@app.task
@@ -316,6 +358,11 @@ def send_order_canceled_email_task(recipient_email, payload, config):
subject=subject,
template_str=email_template_str,
)
+ order_events.event_order_cancelled_notification(
+ order_id=payload["order"]["id"],
+ user_id=payload["requester_user_id"],
+ customer_email=recipient_email,
+ )
@app.task
@@ -341,6 +388,11 @@ def send_order_refund_email_task(recipient_email, payload, config):
subject=subject,
template_str=email_template_str,
)
+ order_events.event_order_refunded_notification(
+ order_id=payload["order"]["id"],
+ user_id=payload["requester_user_id"],
+ customer_email=recipient_email,
+ )
@app.task
diff --git a/saleor/plugins/user_email/tests/test_tasks.py b/saleor/plugins/user_email/tests/test_tasks.py
index cd94e8e07c..8f964f593b 100644
--- a/saleor/plugins/user_email/tests/test_tasks.py
+++ b/saleor/plugins/user_email/tests/test_tasks.py
@@ -1,13 +1,15 @@
from unittest import mock
-from saleor.account.notifications import get_default_user_payload
-from saleor.order import OrderEvents, OrderEventsEmails
-from saleor.order.notifications import (
+from ....account.notifications import get_default_user_payload
+from ....invoice import InvoiceEvents
+from ....invoice.models import Invoice, InvoiceEvent
+from ....order import OrderEvents, OrderEventsEmails
+from ....order.notifications import (
get_default_fulfillment_payload,
get_default_order_payload,
)
-from saleor.plugins.email_common import EmailConfig
-from saleor.plugins.user_email.tasks import (
+from ...email_common import EmailConfig
+from ...user_email.tasks import (
send_account_confirmation_email_task,
send_account_delete_confirmation_email_task,
send_fulfillment_confirmation_email_task,
@@ -209,6 +211,8 @@ def test_send_user_change_email_notification_task_default_template(
"recipient_email": recipient_email,
"site_name": "Saleor",
"domain": "localhost:8000",
+ "old_email": "[email protected]",
+ "new_email": "[email protected]",
}
send_user_change_email_notification_task(
@@ -235,6 +239,8 @@ def test_send_user_change_email_notification_task_custom_template(
"recipient_email": recipient_email,
"site_name": "Saleor",
"domain": "localhost:8000",
+ "old_email": "[email protected]",
+ "new_email": "[email protected]",
}
send_user_change_email_notification_task(
@@ -365,30 +371,46 @@ def test_send_set_user_password_email_task_custom_template(
@mock.patch("saleor.plugins.email_common.send_mail")
def test_send_invoice_email_task_default_template(
- mocked_send_mail, user_email_dict_config,
+ mocked_send_mail, user_email_dict_config, staff_user, order,
):
+ invoice = Invoice.objects.create(order=order)
recipient_email = "[email protected]"
payload = {
"invoice": {
- "id": 1,
+ "id": invoice.id,
+ "order_id": order.id,
"number": 999,
"download_url": "http://localhost:8000/download",
},
"recipient_email": recipient_email,
"site_name": "Saleor",
"domain": "localhost:8000",
+ "requester_user_id": staff_user.id,
}
send_invoice_email_task(recipient_email, payload, user_email_dict_config)
# confirm that mail has correct structure and email was sent
assert mocked_send_mail.called
+ assert InvoiceEvent.objects.filter(
+ type=InvoiceEvents.SENT,
+ user=staff_user.id,
+ invoice=invoice,
+ parameters__email=recipient_email,
+ ).exists()
+ assert order.events.filter(
+ type=OrderEvents.INVOICE_SENT,
+ order=order,
+ user=staff_user.id,
+ parameters__email=recipient_email,
+ ).exists()
@mock.patch("saleor.plugins.user_email.tasks.send_email")
def test_send_invoice_email_task_custom_template(
- mocked_send_email, user_email_dict_config, user_email_plugin,
+ mocked_send_email, user_email_dict_config, user_email_plugin, staff_user, order
):
+ invoice = Invoice.objects.create(order=order)
expected_template_str = "<html><body>Template body</body></html>"
expected_subject = "Test Email Subject"
user_email_plugin(
@@ -398,13 +420,15 @@ def test_send_invoice_email_task_custom_template(
recipient_email = "[email protected]"
payload = {
"invoice": {
- "id": 1,
+ "id": invoice.id,
"number": 999,
+ "order_id": order.id,
"download_url": "http://localhost:8000/download",
},
"recipient_email": recipient_email,
"site_name": "Saleor",
"domain": "localhost:8000",
+ "requester_user_id": staff_user.id,
}
send_invoice_email_task(recipient_email, payload, user_email_dict_config)
@@ -417,6 +441,18 @@ def test_send_invoice_email_task_custom_template(
subject=expected_subject,
template_str=expected_template_str,
)
+ assert InvoiceEvent.objects.filter(
+ type=InvoiceEvents.SENT,
+ user=staff_user.id,
+ invoice=invoice,
+ parameters__email=recipient_email,
+ ).exists()
+ assert order.events.filter(
+ type=OrderEvents.INVOICE_SENT,
+ order=order,
+ user=staff_user.id,
+ parameters__email=recipient_email,
+ ).exists()
@mock.patch("saleor.plugins.email_common.send_mail")
@@ -469,9 +505,10 @@ def test_send_order_confirmation_email_task_custom_template(
@mock.patch("saleor.plugins.email_common.send_mail")
def test_send_fulfillment_confirmation_email_task_default_template(
- mocked_send_mail, user_email_dict_config, order, fulfillment
+ mocked_send_mail, user_email_dict_config, order, fulfillment, staff_user
):
payload = get_default_fulfillment_payload(order, fulfillment)
+ payload["requester_user_id"] = staff_user.pk
send_fulfillment_confirmation_email_task(
payload["recipient_email"], payload, user_email_dict_config
@@ -480,10 +517,22 @@ def test_send_fulfillment_confirmation_email_task_default_template(
# confirm that mail has correct structure and email was sent
assert mocked_send_mail.called
+ event_email_sent = order.events.get()
+ assert event_email_sent.user == staff_user
+ assert event_email_sent.parameters == {
+ "email": order.user_email,
+ "email_type": OrderEventsEmails.FULFILLMENT,
+ }
+
@mock.patch("saleor.plugins.user_email.tasks.send_email")
def test_send_fulfillment_confirmation_email_task_custom_template(
- mocked_send_email, user_email_dict_config, user_email_plugin, order, fulfillment
+ mocked_send_email,
+ user_email_dict_config,
+ user_email_plugin,
+ order,
+ fulfillment,
+ staff_user,
):
expected_template_str = "<html><body>Template body</body></html>"
expected_subject = "Test Email Subject"
@@ -492,7 +541,10 @@ def test_send_fulfillment_confirmation_email_task_custom_template(
fulfillment_confirmation_subject=expected_subject,
)
payload = get_default_fulfillment_payload(order, fulfillment)
+ payload["requester_user_id"] = staff_user.pk
+ payload["digital_lines"] = [{"fulfillmentLine": {"id": 1}}]
recipient_email = payload["recipient_email"]
+
send_fulfillment_confirmation_email_task(
recipient_email, payload, user_email_dict_config
)
@@ -506,6 +558,18 @@ def test_send_fulfillment_confirmation_email_task_custom_template(
template_str=expected_template_str,
)
+ event_email_sent, event_digital_email_sent = order.events.all().order_by("pk")
+ assert event_email_sent.user == staff_user
+ assert event_email_sent.parameters == {
+ "email": order.user_email,
+ "email_type": OrderEventsEmails.FULFILLMENT,
+ }
+ assert event_digital_email_sent.user == staff_user
+ assert event_digital_email_sent.parameters == {
+ "email": order.user_email,
+ "email_type": OrderEventsEmails.DIGITAL_LINKS,
+ }
+
@mock.patch("saleor.plugins.email_common.send_mail")
def test_send_fulfillment_update_email_task_default_template(
@@ -523,7 +587,12 @@ def test_send_fulfillment_update_email_task_default_template(
@mock.patch("saleor.plugins.user_email.tasks.send_email")
def test_send_fulfillment_update_email_task_custom_template(
- mocked_send_email, user_email_dict_config, user_email_plugin, order, fulfillment
+ mocked_send_email,
+ user_email_dict_config,
+ user_email_plugin,
+ order,
+ fulfillment,
+ staff_user,
):
expected_template_str = "<html><body>Template body</body></html>"
expected_subject = "Test Email Subject"
@@ -572,6 +641,12 @@ def test_send_payment_confirmation_email_task_default_template(
# confirm that mail has correct structure and email was sent
assert mocked_send_mail.called
+ event_email_sent = order.events.get()
+ assert event_email_sent.parameters == {
+ "email": recipient_email,
+ "email_type": OrderEventsEmails.PAYMENT,
+ }
+
@mock.patch("saleor.plugins.user_email.tasks.send_email")
def test_send_payment_confirmation_email_task_custom_template(
@@ -614,7 +689,7 @@ def test_send_payment_confirmation_email_task_custom_template(
@mock.patch("saleor.plugins.email_common.send_mail")
def test_send_order_canceled_email_task_default_template(
- mocked_send_mail, user_email_dict_config, order
+ mocked_send_mail, user_email_dict_config, order, staff_user
):
recipient_email = "[email protected]"
payload = {
@@ -622,6 +697,7 @@ def test_send_order_canceled_email_task_default_template(
"recipient_email": recipient_email,
"site_name": "Saleor",
"domain": "localhost:8000",
+ "requester_user_id": staff_user.pk,
}
send_order_canceled_email_task(recipient_email, payload, user_email_dict_config)
@@ -632,7 +708,7 @@ def test_send_order_canceled_email_task_default_template(
@mock.patch("saleor.plugins.user_email.tasks.send_email")
def test_send_order_canceled_email_task_custom_template(
- mocked_send_email, user_email_dict_config, user_email_plugin, order
+ mocked_send_email, user_email_dict_config, user_email_plugin, order, staff_user
):
expected_template_str = "<html><body>Template body</body></html>"
expected_subject = "Test Email Subject"
@@ -646,6 +722,7 @@ def test_send_order_canceled_email_task_custom_template(
"recipient_email": recipient_email,
"site_name": "Saleor",
"domain": "localhost:8000",
+ "requester_user_id": staff_user.pk,
}
send_order_canceled_email_task(recipient_email, payload, user_email_dict_config)
@@ -661,7 +738,7 @@ def test_send_order_canceled_email_task_custom_template(
@mock.patch("saleor.plugins.email_common.send_mail")
def test_send_order_refund_email_task_default_template(
- mocked_send_mail, user_email_dict_config, order
+ mocked_send_mail, user_email_dict_config, order, staff_user
):
recipient_email = "[email protected]"
payload = {
@@ -671,6 +748,7 @@ def test_send_order_refund_email_task_default_template(
"currency": order.currency,
"site_name": "Saleor",
"domain": "localhost:8000",
+ "requester_user_id": staff_user.pk,
}
send_order_refund_email_task(recipient_email, payload, user_email_dict_config)
@@ -678,10 +756,16 @@ def test_send_order_refund_email_task_default_template(
# confirm that mail has correct structure and email was sent
assert mocked_send_mail.called
+ event_email_sent = order.events.get()
+ assert event_email_sent.parameters == {
+ "email": recipient_email,
+ "email_type": OrderEventsEmails.ORDER_REFUND,
+ }
+
@mock.patch("saleor.plugins.user_email.tasks.send_email")
def test_send_order_refund_email_task_custom_template(
- mocked_send_email, user_email_dict_config, user_email_plugin, order
+ mocked_send_email, user_email_dict_config, user_email_plugin, order, staff_user
):
expected_template_str = "<html><body>Template body</body></html>"
expected_subject = "Test Email Subject"
@@ -697,6 +781,7 @@ def test_send_order_refund_email_task_custom_template(
"currency": order.currency,
"site_name": "Saleor",
"domain": "localhost:8000",
+ "requester_user_id": staff_user.pk,
}
send_order_refund_email_task(recipient_email, payload, user_email_dict_config)
@@ -708,6 +793,11 @@ def test_send_order_refund_email_task_custom_template(
subject=expected_subject,
template_str=expected_template_str,
)
+ event_email_sent = order.events.get()
+ assert event_email_sent.parameters == {
+ "email": recipient_email,
+ "email_type": OrderEventsEmails.ORDER_REFUND,
+ }
@mock.patch("saleor.plugins.email_common.send_mail")