Skip to content

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

Clear gift cards notifications

IKarbowiakc0242fda91d7
diff --git a/saleor/checkout/tests/test_checkout_complete.py b/saleor/checkout/tests/test_checkout_complete.py
index ad1c525978..f91a250680 100644
--- a/saleor/checkout/tests/test_checkout_complete.py
+++ b/saleor/checkout/tests/test_checkout_complete.py
@@ -767,8 +767,10 @@ def test_create_order_with_many_gift_cards(
)
[email protected]("saleor.giftcard.utils.send_gift_card_notification")
@pytest.mark.parametrize("is_anonymous_user", (True, False))
def test_create_order_gift_card_bought(
+ send_notification_mock,
checkout_with_gift_card_items,
customer_user,
shipping_method,
@@ -825,13 +827,22 @@ def test_create_order_gift_card_bought(
).unit_price_gross
)
assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
- assert GiftCardEvent.objects.filter(
- gift_card=gift_card, type=GiftCardEvents.SENT_TO_CUSTOMER
+ send_notification_mock.assert_called_once_with(
+ checkout_user,
+ None,
+ checkout_user,
+ order.user_email,
+ gift_card,
+ manager,
+ order.channel.slug,
+ resending=False,
)
[email protected]("saleor.giftcard.utils.send_gift_card_notification")
@pytest.mark.parametrize("is_anonymous_user", (True, False))
def test_create_order_gift_card_bought_only_shippable_gift_card(
+ send_notification_mock,
checkout,
shippable_gift_card_product,
customer_user,
@@ -884,6 +895,7 @@ def test_create_order_gift_card_bought_only_shippable_gift_card(
assert order.total.gross == total_gross
assert not GiftCard.objects.all()
+ send_notification_mock.assert_not_called()
@pytest.mark.parametrize("is_anonymous_user", (True, False))
diff --git a/saleor/giftcard/notifications.py b/saleor/giftcard/notifications.py
index 034ff19a84..6c39fcfbfe 100644
--- a/saleor/giftcard/notifications.py
+++ b/saleor/giftcard/notifications.py
@@ -1,29 +1,43 @@
+from typing import TYPE_CHECKING
+
+from ..account.notifications import get_default_user_payload
from ..core.notifications import get_site_context
from ..core.notify_events import NotifyEventType
+if TYPE_CHECKING:
+ from .models import GiftCard
+
-def send_gift_card_notification(user, app, email, gift_card, manager, channel_slug):
+def send_gift_card_notification(
+ requester_user,
+ app,
+ customer_user,
+ email,
+ gift_card,
+ manager,
+ channel_slug,
+ *,
+ resending
+):
"""Trigger sending a gift card notification for the given recipient."""
payload = {
- "requester": {
- "user_id": user.id if user else None,
- "email": user.email if user else None,
- "app_id": app.id if app else None,
- "app_name": app.name if app else None,
- },
- "recipient": {
- "first_name": user.first_name if user else None,
- "last_name": user.last_name if user else None,
- },
+ "gift_card": get_default_gift_card_payload(gift_card),
+ "user": get_default_user_payload(customer_user) if customer_user else None,
+ "requester_user_id": requester_user.id if requester_user else None,
+ "requester_app_id": app.id if app else None,
"recipient_email": email,
- "gift_card": {
- "id": gift_card.id,
- "code": gift_card.code,
- "balance": gift_card.current_balance_amount,
- "currency": gift_card.currency,
- },
+ "resending": resending,
**get_site_context(),
}
manager.notify(
NotifyEventType.SEND_GIFT_CARD, payload=payload, channel_slug=channel_slug
)
+
+
+def get_default_gift_card_payload(gift_card: "GiftCard"):
+ return {
+ "id": gift_card.id,
+ "code": gift_card.code,
+ "balance": gift_card.current_balance_amount,
+ "currency": gift_card.currency,
+ }
diff --git a/saleor/giftcard/tests/test_notifications.py b/saleor/giftcard/tests/test_notifications.py
new file mode 100644
index 0000000000..37132d4873
--- /dev/null
+++ b/saleor/giftcard/tests/test_notifications.py
@@ -0,0 +1,51 @@
+from unittest import mock
+
+from ...account.notifications import get_default_user_payload
+from ...core.notify_events import NotifyEventType
+from ...plugins.manager import get_plugins_manager
+from ..notifications import get_default_gift_card_payload, send_gift_card_notification
+
+
+def test_get_default_gift_card_payload(gift_card):
+ payload = get_default_gift_card_payload(gift_card)
+ assert payload == {
+ "id": gift_card.id,
+ "code": gift_card.code,
+ "balance": gift_card.current_balance_amount,
+ "currency": gift_card.currency,
+ }
+
+
[email protected]("saleor.plugins.manager.PluginsManager.notify")
+def test_send_gift_card_notification(
+ mocked_notify, staff_user, customer_user, gift_card, channel_USD
+):
+ manager = get_plugins_manager()
+ resending = False
+ send_gift_card_notification(
+ staff_user,
+ None,
+ customer_user,
+ customer_user.email,
+ gift_card,
+ manager,
+ channel_USD.slug,
+ resending=resending,
+ )
+
+ expected_payload = {
+ "gift_card": get_default_gift_card_payload(gift_card),
+ "user": get_default_user_payload(customer_user) if customer_user else None,
+ "requester_user_id": staff_user.id,
+ "requester_app_id": None,
+ "recipient_email": customer_user.email,
+ "resending": resending,
+ "site_name": "mirumee.com",
+ "domain": "mirumee.com",
+ }
+
+ mocked_notify.assert_called_once_with(
+ NotifyEventType.SEND_GIFT_CARD,
+ payload=expected_payload,
+ channel_slug=channel_USD.slug,
+ )
diff --git a/saleor/giftcard/tests/test_utils.py b/saleor/giftcard/tests/test_utils.py
index 52c8cf048f..d2e32c84fe 100644
--- a/saleor/giftcard/tests/test_utils.py
+++ b/saleor/giftcard/tests/test_utils.py
@@ -230,13 +230,6 @@ def test_gift_cards_create(
assert non_shippable_gift_card.created_by_email == user_email
assert non_shippable_gift_card.expiry_date is None
- sent_to_customer_event = GiftCardEvent.objects.get(
- gift_card=non_shippable_gift_card, type=GiftCardEvents.SENT_TO_CUSTOMER
- )
- assert sent_to_customer_event.user == staff_user
- assert sent_to_customer_event.app is None
- assert sent_to_customer_event.parameters == {"email": user_email}
-
shippable_event = GiftCardEvent.objects.get(
gift_card=non_shippable_gift_card, type=GiftCardEvents.BOUGHT
)
@@ -247,10 +240,12 @@ def test_gift_cards_create(
send_notification_mock.assert_called_once_with(
staff_user,
None,
+ order.user,
user_email,
non_shippable_gift_card,
manager,
order.channel.slug,
+ resending=False,
)
@@ -298,13 +293,6 @@ def test_gift_cards_create_expiry_date_set(
assert non_shippable_gift_card.created_by_email == user_email
assert non_shippable_gift_card.expiry_date
- sent_to_customer_event = GiftCardEvent.objects.get(
- gift_card=non_shippable_gift_card, type=GiftCardEvents.SENT_TO_CUSTOMER
- )
- assert sent_to_customer_event.user == staff_user
- assert sent_to_customer_event.app is None
- assert sent_to_customer_event.parameters == {"email": user_email}
-
shippable_event = GiftCardEvent.objects.get(
gift_card=non_shippable_gift_card, type=GiftCardEvents.BOUGHT
)
@@ -318,10 +306,12 @@ def test_gift_cards_create_expiry_date_set(
send_notification_mock.assert_called_once_with(
staff_user,
None,
+ order.user,
user_email,
non_shippable_gift_card,
manager,
order.channel.slug,
+ resending=False,
)
@@ -354,11 +344,7 @@ def test_gift_cards_create_multiple_quantity(
assert gift_card.current_balance == price
assert GiftCardEvent.objects.filter(type=GiftCardEvents.BOUGHT).count() == quantity
- assert (
- GiftCardEvent.objects.filter(type=GiftCardEvents.SENT_TO_CUSTOMER).count()
- == quantity
- )
- assert send_notification_mock.call_count == 3
+ assert send_notification_mock.call_count == quantity
def test_get_gift_card_lines(
diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.py
index 478187c0da..043d2e830a 100644
--- a/saleor/giftcard/utils.py
+++ b/saleor/giftcard/utils.py
@@ -201,7 +201,13 @@ def gift_cards_create(
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, channel_slug
+ non_shippable_gift_cards,
+ user_email,
+ requestor_user,
+ app,
+ customer_user,
+ manager,
+ channel_slug,
)
return gift_cards
@@ -222,12 +228,18 @@ def send_gift_cards_to_customer(
user_email: str,
requestor_user: Optional["User"],
app: Optional["App"],
+ customer_user: Optional["User"],
manager: "PluginsManager",
channel_slug: str,
):
for gift_card in gift_cards:
send_gift_card_notification(
- requestor_user, app, user_email, gift_card, manager, channel_slug
+ requestor_user,
+ app,
+ customer_user,
+ user_email,
+ gift_card,
+ manager,
+ channel_slug,
+ resending=False,
)
-
- events.gift_cards_sent(gift_cards, requestor_user, app, user_email)
diff --git a/saleor/graphql/checkout/tests/test_checkout_complete.py b/saleor/graphql/checkout/tests/test_checkout_complete.py
index 8b65679fbc..1c60ab4697 100644
--- a/saleor/graphql/checkout/tests/test_checkout_complete.py
+++ b/saleor/graphql/checkout/tests/test_checkout_complete.py
@@ -278,10 +278,13 @@ def test_checkout_complete(
order_confirmed_mock.assert_called_once_with(order)
+@patch("saleor.giftcard.utils.send_gift_card_notification")
@patch("saleor.plugins.manager.PluginsManager.order_confirmed")
def test_checkout_complete_gift_card_bought(
order_confirmed_mock,
+ send_notification_mock,
site_settings,
+ customer_user,
user_api_client,
checkout_with_gift_card_items,
payment_dummy,
@@ -294,6 +297,7 @@ def test_checkout_complete_gift_card_bought(
checkout.billing_address = address
checkout.store_value_in_metadata(items={"accepted": "true"})
checkout.store_value_in_private_metadata(items={"accepted": "false"})
+ checkout.user = customer_user
checkout.save()
manager = get_plugins_manager()
@@ -330,8 +334,15 @@ def test_checkout_complete_gift_card_bought(
gift_card = GiftCard.objects.get()
assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
- assert GiftCardEvent.objects.filter(
- gift_card=gift_card, type=GiftCardEvents.SENT_TO_CUSTOMER
+ send_notification_mock.assert_called_once_with(
+ customer_user,
+ None,
+ customer_user,
+ customer_user.email,
+ gift_card,
+ ANY,
+ checkout.channel.slug,
+ resending=False,
)
order_confirmed_mock.assert_called_once_with(order)
assert Fulfillment.objects.count() == 1
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index 99b53bfcd7..02494b98e5 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -4,6 +4,7 @@ import graphene
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
+from ...account.models import User
from ...core.permissions import GiftcardPermissions
from ...core.utils.promo_code import generate_promo_code
from ...core.utils.validators import date_passed, user_is_valid
@@ -121,6 +122,7 @@ class GiftCardCreate(ModelMutation):
)
}
)
+ cleaned_input["customer_user"] = User.objects.filter(email=email).first()
return cleaned_input
@@ -187,21 +189,15 @@ class GiftCardCreate(ModelMutation):
app=info.context.app,
)
if email := cleaned_input.get("user_email"):
- user = cleaned_input.get("created_by")
- app = cleaned_input.get("app")
send_gift_card_notification(
- user,
- app,
+ cleaned_input.get("created_by"),
+ cleaned_input.get("app"),
+ cleaned_input["customer_user"],
email,
instance,
info.context.plugins,
channel_slug=cleaned_input["channel"],
- )
- events.gift_card_sent(
- gift_card_id=instance.id,
- user_id=user.id if user else None,
- app_id=app.id if app else None,
- email=email,
+ resending=False,
)
@@ -365,38 +361,40 @@ class GiftCardResend(BaseMutation):
}
)
+ return data
+
@classmethod
def get_target_email(cls, data, gift_card):
return (
data.get("email") or gift_card.used_by_email or gift_card.created_by_email
)
+ @classmethod
+ def get_customer_user(cls, email):
+ return User.objects.filter(email=email).first()
+
@classmethod
def perform_mutation(cls, _root, info, **data):
data = data.get("input")
- cls.clean_input(data)
+ data = cls.clean_input(data)
gift_card_id = data["id"]
gift_card = cls.get_node_or_error(
info, gift_card_id, field="gift_card_id", only_type=GiftCard
)
target_email = cls.get_target_email(data, gift_card)
+ customer_user = cls.get_customer_user(target_email)
user = info.context.user
if not user_is_valid(user):
user = None
- app = info.context.app
send_gift_card_notification(
user,
- app,
+ info.context.app,
+ customer_user,
target_email,
gift_card,
info.context.plugins,
channel_slug=data.get("channel"),
- )
- events.gift_card_resent(
- gift_card_id=gift_card.id,
- user_id=user.id if user else None,
- app_id=app.id if app else None,
- email=target_email,
+ resending=True,
)
return GiftCardResend(gift_card=gift_card)
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 255cb425ac..b3b7f72230 100644
--- a/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py
+++ b/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py
@@ -1,7 +1,9 @@
from datetime import date, timedelta
+from unittest import mock
from .....giftcard import GiftCardEvents
from .....giftcard.error_codes import GiftCardErrorCode
+from .....giftcard.models import GiftCard
from ....tests.utils import assert_no_permission, get_graphql_content
CREATE_GIFT_CARD_MUTATION = """
@@ -76,7 +78,9 @@ CREATE_GIFT_CARD_MUTATION = """
"""
[email protected]("saleor.graphql.giftcard.mutations.send_gift_card_notification")
def test_create_never_expiry_gift_card(
+ send_notification_mock,
staff_api_client,
customer_user,
channel_USD,
@@ -133,8 +137,8 @@ def test_create_never_expiry_gift_card(
assert data["initialBalance"]["amount"] == initial_balance
assert data["currentBalance"]["amount"] == initial_balance
- assert len(data["events"]) == 2
- created_event, sent_event = data["events"]
+ assert len(data["events"]) == 1
+ created_event = data["events"][0]
assert created_event["type"] == GiftCardEvents.ISSUED.upper()
assert created_event["user"]["email"] == staff_api_client.user.email
@@ -146,12 +150,22 @@ def test_create_never_expiry_gift_card(
assert not created_event["balance"]["oldInitialBalance"]
assert not created_event["balance"]["oldCurrentBalance"]
- assert sent_event["type"] == GiftCardEvents.SENT_TO_CUSTOMER.upper()
- assert sent_event["user"]["email"] == staff_api_client.user.email
- assert not created_event["app"]
+ gift_card = GiftCard.objects.get()
+ send_notification_mock.assert_called_once_with(
+ staff_api_client.user,
+ None,
+ customer_user,
+ customer_user.email,
+ gift_card,
+ mock.ANY,
+ channel_slug=channel_USD.slug,
+ resending=False,
+ )
[email protected]("saleor.graphql.giftcard.mutations.send_gift_card_notification")
def test_create_gift_card_by_app(
+ send_notification_mock,
app_api_client,
customer_user,
permission_manage_gift_card,
@@ -214,6 +228,8 @@ def test_create_gift_card_by_app(
assert not created_event["balance"]["oldInitialBalance"]
assert not created_event["balance"]["oldCurrentBalance"]
+ send_notification_mock.assert_not_called()
+
def test_create_gift_card_by_customer(api_client, customer_user, channel_USD):
# given
@@ -457,7 +473,9 @@ def test_create_gift_card_with_zero_balance_amount(
assert errors[0]["code"] == GiftCardErrorCode.INVALID.name
[email protected]("saleor.graphql.giftcard.mutations.send_gift_card_notification")
def test_create_gift_card_with_expiry_date(
+ send_notification_mock,
staff_api_client,
customer_user,
channel_USD,
@@ -506,8 +524,8 @@ def test_create_gift_card_with_expiry_date(
assert data["displayCode"]
assert data["expiryDate"] == date_value.isoformat()
- assert len(data["events"]) == 2
- created_event, sent_event = data["events"]
+ assert len(data["events"]) == 1
+ created_event = data["events"][0]
assert created_event["type"] == GiftCardEvents.ISSUED.upper()
assert created_event["user"]["email"] == staff_api_client.user.email
@@ -519,9 +537,17 @@ def test_create_gift_card_with_expiry_date(
assert not created_event["balance"]["oldInitialBalance"]
assert not created_event["balance"]["oldCurrentBalance"]
- assert sent_event["type"] == GiftCardEvents.SENT_TO_CUSTOMER.upper()
- assert sent_event["user"]["email"] == staff_api_client.user.email
- assert not created_event["app"]
+ gift_card = GiftCard.objects.get()
+ send_notification_mock.assert_called_once_with(
+ staff_api_client.user,
+ None,
+ customer_user,
+ customer_user.email,
+ gift_card,
+ mock.ANY,
+ channel_slug=channel_USD.slug,
+ resending=False,
+ )
def test_create_gift_card_with_expiry_date_type_date_in_past(
diff --git a/saleor/graphql/giftcard/tests/mutations/test_gift_card_resend.py b/saleor/graphql/giftcard/tests/mutations/test_gift_card_resend.py
index e6008d60ef..72fecb37fd 100644
--- a/saleor/graphql/giftcard/tests/mutations/test_gift_card_resend.py
+++ b/saleor/graphql/giftcard/tests/mutations/test_gift_card_resend.py
@@ -2,7 +2,6 @@ from unittest import mock
import graphene
-from .....giftcard import GiftCardEvents
from .....giftcard.error_codes import GiftCardErrorCode
from .....giftcard.models import GiftCardEvent
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -44,9 +43,9 @@ GIFT_CARD_RESEND_MUTATION = """
"""
[email protected]("saleor.plugins.manager.PluginsManager.notify")
[email protected]("saleor.graphql.giftcard.mutations.send_gift_card_notification")
def test_resend_gift_card(
- notify_mock,
+ send_notification_mock,
staff_api_client,
gift_card,
channel_USD,
@@ -81,21 +80,23 @@ def test_resend_gift_card(
errors = content["data"]["giftCardResend"]["errors"]
assert not errors
- events = data["events"]
- assert len(events) == 1
- event = events[0]
- assert event["type"] == GiftCardEvents.RESENT.upper()
- assert event["user"]["email"] == staff_api_client.user.email
-
- db_event = GiftCardEvent.objects.get()
- assert db_event.parameters["email"] == email
-
- assert notify_mock.call_count == 1
+ assert data
+
+ send_notification_mock.assert_called_once_with(
+ staff_api_client.user,
+ None,
+ None,
+ email,
+ gift_card,
+ mock.ANY,
+ channel_slug=channel_USD.slug,
+ resending=True,
+ )
[email protected]("saleor.plugins.manager.PluginsManager.notify")
[email protected]("saleor.graphql.giftcard.mutations.send_gift_card_notification")
def test_resend_gift_card_as_app(
- notify_mock,
+ send_notification_mock,
app_api_client,
gift_card,
channel_USD,
@@ -127,23 +128,23 @@ def test_resend_gift_card_as_app(
data = content["data"]["giftCardResend"]["giftCard"]
errors = content["data"]["giftCardResend"]["errors"]
+ assert data
assert not errors
- events = data["events"]
- assert len(events) == 1
- event = events[0]
- assert event["type"] == GiftCardEvents.RESENT.upper()
- assert event["user"] is None
- assert event["app"]["name"] == app_api_client.app.name
-
- db_event = GiftCardEvent.objects.get()
- assert db_event.parameters["email"] == gift_card.created_by_email
-
- assert notify_mock.call_count == 1
+ send_notification_mock.assert_called_once_with(
+ None,
+ app_api_client.app,
+ gift_card.created_by,
+ gift_card.created_by_email,
+ gift_card,
+ mock.ANY,
+ channel_slug=channel_USD.slug,
+ resending=True,
+ )
[email protected]("saleor.plugins.manager.PluginsManager.notify")
[email protected]("saleor.graphql.giftcard.mutations.send_gift_card_notification")
def test_update_gift_card_no_permission(
- notify_mock,
+ send_notification_mock,
staff_api_client,
gift_card,
channel_USD,
@@ -166,12 +167,12 @@ def test_update_gift_card_no_permission(
assert not GiftCardEvent.objects.exists()
assert_no_permission(response)
- notify_mock.assert_not_called()
+ send_notification_mock.assert_not_called()
[email protected]("saleor.plugins.manager.PluginsManager.notify")
[email protected]("saleor.graphql.giftcard.mutations.send_gift_card_notification")
def test_resend_gift_card_malformed_email(
- notify_mock,
+ send_notification_mock,
staff_api_client,
gift_card,
permission_manage_gift_card,
@@ -211,4 +212,4 @@ def test_resend_gift_card_malformed_email(
assert error["field"] == "email"
assert error["code"] == GiftCardErrorCode.INVALID.name
- notify_mock.assert_not_called()
+ send_notification_mock.assert_not_called()
diff --git a/saleor/graphql/order/tests/test_fulfillment.py b/saleor/graphql/order/tests/test_fulfillment.py
index bceed7c790..79eb42c20e 100644
--- a/saleor/graphql/order/tests/test_fulfillment.py
+++ b/saleor/graphql/order/tests/test_fulfillment.py
@@ -324,9 +324,6 @@ def test_order_fulfill_with_gift_cards(
assert GiftCardEvent.objects.filter(
gift_card=non_shippable_gift_card, type=GiftCardEvents.BOUGHT
)
- assert GiftCardEvent.objects.filter(
- gift_card=non_shippable_gift_card, type=GiftCardEvents.SENT_TO_CUSTOMER
- )
fulfillment_lines_for_warehouses = {
str(warehouse.pk): [
@@ -347,10 +344,12 @@ def test_order_fulfill_with_gift_cards(
mock_send_notification.assert_called_once_with(
staff_user,
None,
+ order.user,
order.user_email,
non_shippable_gift_card,
ANY,
order.channel.slug,
+ resending=False,
)
diff --git a/saleor/plugins/sendgrid/tasks.py b/saleor/plugins/sendgrid/tasks.py
index 061f7e0831..5763508547 100644
--- a/saleor/plugins/sendgrid/tasks.py
+++ b/saleor/plugins/sendgrid/tasks.py
@@ -5,7 +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 ...giftcard import events as gift_card_events
from ...invoice import events as invoice_events
from ...order import events as order_events
from . import SendgridConfiguration
@@ -303,12 +303,16 @@ 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["requester"]["user_id"],
- payload["requester"]["app_id"],
- payload["recipient_email"],
- )
+ email_data = {
+ "gift_card_id": payload["gift_card"]["id"],
+ "user_id": payload["requester_user_id"],
+ "app_id": payload["requester_app_id"],
+ "email": payload["recipient_email"],
+ }
+ if payload["resending"] is True:
+ gift_card_events.gift_card_resent(**email_data)
+ else:
+ gift_card_events.gift_card_sent(**email_data)
@app.task(
diff --git a/saleor/plugins/sendgrid/tests/test_tasks.py b/saleor/plugins/sendgrid/tests/test_tasks.py
index d65a8125c6..465c5e678f 100644
--- a/saleor/plugins/sendgrid/tests/test_tasks.py
+++ b/saleor/plugins/sendgrid/tests/test_tasks.py
@@ -736,16 +736,11 @@ def test_send_gift_card_email_task_by_user(
recipient_email = "[email protected]"
payload = {
- "requester": {
- "user_id": staff_user.id,
- "email": staff_user.email,
- "app_id": None,
- "app_name": None,
- },
- "recipient": {
- "first_name": staff_user.first_name,
- "last_name": staff_user.last_name,
- },
+ "user": get_default_user_payload(staff_user),
+ "requester_user_id": staff_user.id,
+ "requester_app_id": None,
+ "recipient_email": recipient_email,
+ "resending": False,
"recipient_email": recipient_email,
"gift_card": {
"id": gift_card.id,
@@ -779,6 +774,52 @@ def test_send_gift_card_email_task_by_user(
assert not gift_card_event.app
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_gift_card_email_task_by_user_resending(
+ mocked_send_email, staff_user, gift_card, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+ recipient_email = "[email protected]"
+
+ payload = {
+ "user": get_default_user_payload(staff_user),
+ "requester_user_id": staff_user.id,
+ "requester_app_id": None,
+ "recipient_email": recipient_email,
+ "resending": True,
+ "recipient_email": recipient_email,
+ "gift_card": {
+ "id": gift_card.id,
+ "code": gift_card.code,
+ "balance": gift_card.current_balance_amount,
+ "currency": gift_card.currency,
+ },
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ send_gift_card_template_id=template_id,
+ sender_name="Sender Name",
+ sender_address="[email protected]",
+ )
+
+ send_gift_card_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+
+ gift_card_event = GiftCardEvent.objects.get()
+ assert gift_card_event.type == GiftCardEvents.RESENT
+ 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, gift_card, sendgrid_email_plugin
@@ -787,14 +828,19 @@ def test_send_gift_card_email_task_by_app(
recipient_email = "[email protected]"
payload = {
- "requester": {
- "user_id": None,
- "email": None,
- "app_id": app.id,
- "app_name": app.name,
+ "user": None,
+ "requester_user_id": None,
+ "requester_app_id": app.id,
+ "recipient_email": recipient_email,
+ "resending": False,
+ "recipient_email": recipient_email,
+ "gift_card": {
+ "id": gift_card.id,
+ "code": gift_card.code,
+ "balance": gift_card.current_balance_amount,
+ "currency": gift_card.currency,
},
"recipient_email": recipient_email,
- "gift_card": {"id": gift_card.id, "code": gift_card.code},
}
plugin = sendgrid_email_plugin(
diff --git a/saleor/plugins/user_email/default_email_templates/gift_card.html b/saleor/plugins/user_email/default_email_templates/gift_card.html
index 24a935be1b..4d72b3cd91 100644
--- a/saleor/plugins/user_email/default_email_templates/gift_card.html
+++ b/saleor/plugins/user_email/default_email_templates/gift_card.html
@@ -119,8 +119,8 @@
<div
style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:16px;line-height:1;text-align:left;color:#000000;">
- {{#if recipient.first_name}}
- Hi, {{ recipient.first_name }} {{ recipient.last_name }}<br>
+ {{#if user.first_name}}
+ Hi, {{ user.first_name }} {{ user.last_name }}<br>
{{else}}
Hi, {{ recipient_email }}
{{/if}}
diff --git a/saleor/plugins/user_email/tasks.py b/saleor/plugins/user_email/tasks.py
index b467f6af1b..82dcdc01f6 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 ...giftcard import events as gift_card_events
from ...invoice import events as invoice_events
from ...order import events as order_events
from ..email_common import EmailConfig, send_email
@@ -123,6 +124,16 @@ def send_gift_card_email_task(recipient_email, payload, config, subject, templat
subject=subject,
template_str=template,
)
+ email_data = {
+ "gift_card_id": payload["gift_card"]["id"],
+ "user_id": payload["requester_user_id"],
+ "app_id": payload["requester_app_id"],
+ "email": payload["recipient_email"],
+ }
+ if payload["resending"] is True:
+ gift_card_events.gift_card_resent(**email_data)
+ else:
+ gift_card_events.gift_card_sent(**email_data)
@app.task(compression="zlib")
diff --git a/saleor/plugins/user_email/tests/test_tasks.py b/saleor/plugins/user_email/tests/test_tasks.py
index 45cb9c947c..bc114137eb 100644
--- a/saleor/plugins/user_email/tests/test_tasks.py
+++ b/saleor/plugins/user_email/tests/test_tasks.py
@@ -1,6 +1,8 @@
from unittest import mock
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
@@ -14,6 +16,7 @@ from ...user_email.tasks import (
send_account_delete_confirmation_email_task,
send_fulfillment_confirmation_email_task,
send_fulfillment_update_email_task,
+ send_gift_card_email_task,
send_invoice_email_task,
send_order_canceled_email_task,
send_order_confirmation_email_task,
@@ -1150,3 +1153,165 @@ def test_send_order_confirmed_email_task_custom_template(
parameters__email=recipient_email,
parameters__email_type=OrderEventsEmails.CONFIRMED,
).exists()
+
+
[email protected]("saleor.plugins.user_email.tasks.send_email")
+def test_send_gift_card_email_task_by_user(
+ mocked_send_email, user_email_dict_config, staff_user, gift_card, user_email_plugin
+):
+ expected_template_str = "<html><body>Template body</body></html>"
+ expected_subject = "Test Email Subject"
+ recipient_email = "[email protected]"
+
+ payload = {
+ "user": get_default_user_payload(staff_user),
+ "requester_user_id": staff_user.id,
+ "requester_app_id": None,
+ "recipient_email": recipient_email,
+ "resending": False,
+ "recipient_email": recipient_email,
+ "gift_card": {
+ "id": gift_card.id,
+ "code": gift_card.code,
+ "balance": gift_card.current_balance_amount,
+ "currency": gift_card.currency,
+ },
+ }
+
+ user_email_plugin(
+ order_confirmed_template=expected_template_str,
+ order_confirmed_subject=expected_subject,
+ )
+
+ send_gift_card_email_task(
+ recipient_email,
+ payload,
+ user_email_dict_config,
+ expected_subject,
+ expected_template_str,
+ )
+
+ email_config = EmailConfig(**user_email_dict_config)
+ mocked_send_email.assert_called_with(
+ config=email_config,
+ recipient_list=[recipient_email],
+ context=payload,
+ subject=expected_subject,
+ template_str=expected_template_str,
+ )
+
+ 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
+
+
[email protected]("saleor.plugins.user_email.tasks.send_email")
+def test_send_gift_card_email_task_by_user_resending(
+ mocked_send_email, user_email_dict_config, staff_user, gift_card, user_email_plugin
+):
+ expected_template_str = "<html><body>Template body</body></html>"
+ expected_subject = "Test Email Subject"
+ recipient_email = "[email protected]"
+
+ payload = {
+ "user": get_default_user_payload(staff_user),
+ "requester_user_id": staff_user.id,
+ "requester_app_id": None,
+ "recipient_email": recipient_email,
+ "resending": True,
+ "recipient_email": recipient_email,
+ "gift_card": {
+ "id": gift_card.id,
+ "code": gift_card.code,
+ "balance": gift_card.current_balance_amount,
+ "currency": gift_card.currency,
+ },
+ }
+
+ user_email_plugin(
+ order_confirmed_template=expected_template_str,
+ order_confirmed_subject=expected_subject,
+ )
+
+ send_gift_card_email_task(
+ recipient_email,
+ payload,
+ user_email_dict_config,
+ expected_subject,
+ expected_template_str,
+ )
+
+ email_config = EmailConfig(**user_email_dict_config)
+ mocked_send_email.assert_called_with(
+ config=email_config,
+ recipient_list=[recipient_email],
+ context=payload,
+ subject=expected_subject,
+ template_str=expected_template_str,
+ )
+
+ gift_card_event = GiftCardEvent.objects.get()
+ assert gift_card_event.type == GiftCardEvents.RESENT
+ assert gift_card_event.parameters == {
+ "email": recipient_email,
+ }
+ assert gift_card_event.user == staff_user
+ assert not gift_card_event.app
+
+
[email protected]("saleor.plugins.user_email.tasks.send_email")
+def test_send_gift_card_email_task_by_app(
+ mocked_send_email, user_email_dict_config, app, gift_card, user_email_plugin
+):
+ expected_template_str = "<html><body>Template body</body></html>"
+ expected_subject = "Test Email Subject"
+ recipient_email = "[email protected]"
+ payload = {
+ "user": None,
+ "requester_user_id": None,
+ "requester_app_id": app.id,
+ "recipient_email": recipient_email,
+ "resending": False,
+ "recipient_email": recipient_email,
+ "gift_card": {
+ "id": gift_card.id,
+ "code": gift_card.code,
+ "balance": gift_card.current_balance_amount,
+ "currency": gift_card.currency,
+ },
+ "recipient_email": recipient_email,
+ }
+
+ user_email_plugin(
+ order_confirmed_template=expected_template_str,
+ order_confirmed_subject=expected_subject,
+ )
+
+ send_gift_card_email_task(
+ recipient_email,
+ payload,
+ user_email_dict_config,
+ expected_subject,
+ expected_template_str,
+ )
+
+ email_config = EmailConfig(**user_email_dict_config)
+ mocked_send_email.assert_called_with(
+ config=email_config,
+ recipient_list=[recipient_email],
+ context=payload,
+ subject=expected_subject,
+ template_str=expected_template_str,
+ )
+
+ 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