Skip to content

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

Implement digital fulfillment email event

NyanKiyoshi17b6aca7f2cb
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index ac6876c8f1..b3dc17a23b 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -19,7 +19,7 @@ from ...events.models import OrderEvent
from ...events.types import OrderEvents, OrderEventsEmails
from ...order import OrderStatus
from ...order.emails import (
- send_fulfillment_confirmation, send_fulfillment_update,
+ send_fulfillment_confirmation_to_customer, send_fulfillment_update,
send_order_confirmation)
from ...order.models import Fulfillment, FulfillmentLine, Order
from ...order.utils import update_order_prices, update_order_status
@@ -620,10 +620,8 @@ def fulfill_order_lines(request, order_pk):
quantities=quantities, order_lines=order_lines)]
if form.cleaned_data.get('send_mail'):
- send_fulfillment_confirmation.delay(order.pk, fulfillment.pk)
- events.append(OrderEvent.email_sent_event(
- order=order, source=request.user,
- email_type=OrderEventsEmails.SHIPPING))
+ events += send_fulfillment_confirmation_to_customer(
+ order, fulfillment, request.user)
OrderEvent.objects.bulk_create(events)
else:
diff --git a/saleor/events/models/OrderEvent.py b/saleor/events/models/OrderEvent.py
index e96006b0f3..229bac3819 100644
--- a/saleor/events/models/OrderEvent.py
+++ b/saleor/events/models/OrderEvent.py
@@ -1,4 +1,4 @@
-from typing import Dict, List, Union
+from typing import Dict, List, Optional, Union
from django.conf import settings
from django.contrib.auth.base_user import AbstractBaseUser
@@ -51,7 +51,7 @@ class OrderEvent(models.Model):
def email_sent_event(
cls, *,
order: Order, email_type: OrderEventsEmails,
- source: Union[User, None]) -> models.Model:
+ source: Optional[User]) -> models.Model:
if source is not None and source.is_anonymous:
source = None
@@ -141,7 +141,7 @@ class OrderEvent(models.Model):
@staticmethod
def _get_payment_data(
- amount: Union[Money, None], payment: Payment) -> Dict:
+ amount: Optional[Money], payment: Payment) -> Dict:
return {
'parameters': {
'amount': amount,
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.py
index de9822b821..aebb8a8476 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -3,9 +3,8 @@ from django.core.exceptions import ValidationError
from django.utils.translation import npgettext_lazy, pgettext_lazy
from ....events.models import OrderEvent
-from ....events.types import OrderEventsEmails
from ....order import models
-from ....order.emails import send_fulfillment_confirmation
+from ....order.emails import send_fulfillment_confirmation_to_customer
from ....order.utils import (
cancel_fulfillment, fulfill_order_line, update_order_status)
from ...core.mutations import BaseMutation
@@ -13,13 +12,6 @@ from ...order.types import Fulfillment, Order
from ..types import OrderLine
-def send_fulfillment_confirmation_to_customer(order, fulfillment, user):
- send_fulfillment_confirmation.delay(order.pk, fulfillment.pk)
- OrderEvent.email_sent_event(
- order=order, email_type=OrderEventsEmails.FULFILLMENT,
- source=user).save()
-
-
class FulfillmentLineInput(graphene.InputObjectType):
order_line_id = graphene.ID(
description='The ID of the order line.', name='orderLineId')
@@ -113,12 +105,15 @@ class FulfillmentCreate(BaseMutation):
fulfillment.lines.bulk_create(fulfillment_lines)
update_order_status(order)
- OrderEvent.fulfillment_fulfilled_items_event(
+ events = [OrderEvent.fulfillment_fulfilled_items_event(
order=order, source=user,
- quantities=quantities, order_lines=order_lines).save()
+ quantities=quantities, order_lines=order_lines)]
+
if cleaned_input.get('notify_customer', True):
- send_fulfillment_confirmation_to_customer(
+ events += send_fulfillment_confirmation_to_customer(
order, fulfillment, user)
+
+ OrderEvent.objects.bulk_create(events)
return fulfillment
@classmethod
diff --git a/saleor/order/emails.py b/saleor/order/emails.py
index 590829b7fb..19213059c2 100644
--- a/saleor/order/emails.py
+++ b/saleor/order/emails.py
@@ -5,6 +5,8 @@ from templated_email import send_templated_mail
from ..core.emails import get_email_base_context
from ..core.utils import build_absolute_uri
+from ..events.models import OrderEvent
+from ..events.types import OrderEventsEmails
from ..seo.schema.email import get_order_confirmation_markup
from .models import Fulfillment, Order
@@ -65,6 +67,23 @@ def send_fulfillment_confirmation(order_pk, fulfillment_pk):
send_templated_mail(**email_data)
+def send_fulfillment_confirmation_to_customer(order, fulfillment, user):
+ send_fulfillment_confirmation.delay(order.pk, fulfillment.pk)
+
+ events = [OrderEvent.email_sent_event(
+ order=order, source=user,
+ email_type=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.append(OrderEvent.email_sent_event(
+ order=order, source=user,
+ email_type=OrderEventsEmails.DIGITAL_LINKS))
+
+ return events
+
+
@shared_task
def send_fulfillment_update(order_pk, fulfillment_pk):
email_data = collect_data_for_fullfillment_email(
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 81f1e32c5f..7b70ff7e01 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -13,7 +13,7 @@ from ..account.models import Address, User
from ..checkout.models import Checkout
from ..core import analytics
from ..events.models import OrderEvent
-from ..events.types import OrderEvents, OrderEventsEmails
+from ..events.types import OrderEventsEmails
from ..order import utils as order_utils
from ..order.emails import send_payment_confirmation
from ..order.models import Order
diff --git a/tests/api/test_fulfillment.py b/tests/api/test_fulfillment.py
index d85c6cb27d..a6f46b05e7 100644
--- a/tests/api/test_fulfillment.py
+++ b/tests/api/test_fulfillment.py
@@ -3,7 +3,7 @@ from unittest.mock import patch
import graphene
import pytest
-from saleor.events.types import OrderEvents, OrderEventsEmails
+from saleor.events.types import OrderEvents
from saleor.order.models import FulfillmentStatus
from tests.api.utils import get_graphql_content
@@ -35,11 +35,10 @@ CREATE_FULFILLMENT_QUERY = """
"""
-@patch(
- 'saleor.graphql.order.mutations.fulfillments.'
- 'send_fulfillment_confirmation')
+@patch('saleor.graphql.order.mutations.fulfillments.'
+ 'send_fulfillment_confirmation_to_customer')
def test_create_fulfillment(
- mock_send_fulfillment_confirmation, staff_api_client, order_with_lines,
+ mock_email_fulfillment, staff_api_client, order_with_lines,
staff_user, permission_manage_orders):
order = order_with_lines
query = CREATE_FULFILLMENT_QUERY
@@ -47,7 +46,6 @@ def test_create_fulfillment(
order_line = order.lines.first()
order_line_id = graphene.Node.to_global_id('OrderLine', order_line.id)
tracking = 'Flames tracking'
- assert not order.events.all()
variables = {
'order': order_id,
'lines': [{'orderLineId': order_line_id, 'quantity': 1}],
@@ -61,26 +59,13 @@ def test_create_fulfillment(
assert data['trackingNumber'] == tracking
assert len(data['lines']) == 1
- event_fulfillment, event_email_sent = order.events.all()
- assert event_fulfillment.type == (
- OrderEvents.FULFILLMENT_FULFILLED_ITEMS.value)
- assert event_fulfillment.parameters == {'fulfilled_items': [
- {'item': str(order_line), 'quantity': 1}]}
- assert event_fulfillment.user == staff_user
-
- assert event_email_sent.type == OrderEvents.EMAIL_SENT.value
- assert event_email_sent.user == staff_user
- assert event_email_sent.parameters == {
- 'email': order.user_email,
- 'email_type': OrderEventsEmails.FULFILLMENT.value}
-
- assert mock_send_fulfillment_confirmation.delay.called
+ assert mock_email_fulfillment.call_count == 1
@patch(
'saleor.graphql.order.mutations.fulfillments.'
- 'send_fulfillment_confirmation')
-def test_create_fulfillment_with_emtpy_quantity(
+ 'send_fulfillment_confirmation_to_customer')
+def test_create_fulfillment_with_empty_quantity(
mock_send_fulfillment_confirmation, staff_api_client, order_with_lines,
staff_user, permission_manage_orders):
order = order_with_lines
@@ -106,7 +91,7 @@ def test_create_fulfillment_with_emtpy_quantity(
assert data['fulfillmentOrder'] == 1
assert data['status'] == FulfillmentStatus.FULFILLED.upper()
- assert mock_send_fulfillment_confirmation.delay.called
+ assert mock_send_fulfillment_confirmation.called
@pytest.mark.parametrize(
@@ -229,11 +214,10 @@ def test_cancel_fulfillment(
assert event_cancel_fulfillment.user == staff_user
-@patch(
- 'saleor.graphql.order.mutations.fulfillments.'
- 'send_fulfillment_confirmation')
+@patch('saleor.graphql.order.mutations.fulfillments.'
+ 'send_fulfillment_confirmation_to_customer')
def test_create_digital_fulfillment(
- mock_send_fulfillment_confirmation, digital_content, staff_api_client,
+ mock_email_fulfillment, digital_content, staff_api_client,
order_with_lines, staff_user, permission_manage_orders):
order = order_with_lines
query = CREATE_FULFILLMENT_QUERY
@@ -247,7 +231,6 @@ def test_create_digital_fulfillment(
second_line_id = graphene.Node.to_global_id('OrderLine', second_line.id)
tracking = 'Flames tracking'
- assert not order.events.all()
variables = {
'order': order_id,
'lines': [
@@ -258,21 +241,4 @@ def test_create_digital_fulfillment(
query, variables, permissions=[permission_manage_orders])
get_graphql_content(response)
- event_fulfillment, event_email_sent = order.events.all()
- assert event_fulfillment.type == (
- OrderEvents.FULFILLMENT_FULFILLED_ITEMS.value)
- assert event_fulfillment.parameters == {'fulfilled_items': [
- {'item': str(order_line), 'quantity': 1},
- {'item': str(second_line), 'quantity': 1}]}
- assert event_fulfillment.user == staff_user
-
- assert event_email_sent.type == OrderEvents.EMAIL_SENT.value
- assert event_email_sent.user == staff_user
- assert event_email_sent.parameters == {
- 'email': order.user_email,
- 'email_type': OrderEventsEmails.FULFILLMENT.value}
-
- digital_content.refresh_from_db()
- assert digital_content.urls.count() == 1
- assert digital_content.urls.all()[0].line == order_line
- assert mock_send_fulfillment_confirmation.delay.called
+ assert mock_email_fulfillment.call_count == 1
diff --git a/tests/conftest.py b/tests/conftest.py
index bfe29ab3d4..2b7edef5b4 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -978,6 +978,8 @@ def digital_content(category, media_root):
product=product, sku='SKU_554', cost_price=Money(1, 'USD'), quantity=5,
quantity_allocated=3)
+ assert product_variant.is_digital()
+
image_file, image_name = create_image()
d_content = DigitalContent.objects.create(
content_file=image_file, product_variant=product_variant,
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index 5ffac20384..b95e52a513 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -1,5 +1,6 @@
import json
from decimal import Decimal
+from unittest.mock import patch
import pytest
from django.urls import reverse
@@ -16,6 +17,7 @@ from saleor.discount.utils import increase_voucher_usage
from saleor.events.models import OrderEvent
from saleor.events.types import OrderEvents, OrderEventsEmails
from saleor.order import FulfillmentStatus, OrderStatus
+from saleor.order.emails import send_fulfillment_confirmation_to_customer
from saleor.order.models import Order, OrderLine
from saleor.order.utils import add_variant_to_order, change_order_line_quantity
from saleor.payment import ChargeStatus, TransactionKind
@@ -978,7 +980,58 @@ def test_view_mark_order_as_paid(admin_client, order_with_lines):
type=OrderEvents.ORDER_MARKED_AS_PAID.value).exists()
-def test_view_fulfill_order_lines(admin_client, order_with_lines):
+@patch('saleor.order.utils.emails.send_fulfillment_confirmation')
[email protected]('has_standard,has_digital', (
+ (True, True),
+ (True, False),
+ (False, True)))
+def test_send_fulfillment_order_lines_mails(
+ mocked_send_fulfillment_confirmation,
+ staff_user,
+ fulfilled_order, fulfillment, digital_content,
+ has_standard, has_digital):
+
+ order = fulfilled_order
+ assert order.lines.count() == 2
+
+ if not has_standard:
+ line = order.lines.all()[0]
+ line.variant = digital_content.product_variant
+ assert line.is_digital
+ line.save()
+
+ if has_digital:
+ line = order.lines.all()[1]
+ line.variant = digital_content.product_variant
+ assert line.is_digital
+ line.save()
+
+ events = send_fulfillment_confirmation_to_customer(
+ order=order, fulfillment=fulfillment, user=staff_user)
+
+ mocked_send_fulfillment_confirmation.delay.assert_called_once_with(
+ order.pk, fulfillment.pk)
+
+ # 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.value}
+
+ 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.value}
+ else:
+ assert len(events) == 1
+
+
+@patch('saleor.dashboard.order.views.'
+ 'send_fulfillment_confirmation_to_customer')
+def test_view_fulfill_order_lines(
+ mock_email_fulfillment, admin_client, order_with_lines):
url = reverse(
'dashboard:fulfill-order-lines',
kwargs={'order_pk': order_with_lines.pk})
@@ -1001,6 +1054,7 @@ def test_view_fulfill_order_lines(admin_client, order_with_lines):
order_with_lines.refresh_from_db()
for line in order_with_lines.lines.all():
assert line.quantity_unfulfilled == 0
+ assert mock_email_fulfillment.call_count == 1
def test_view_fulfill_order_lines_with_empty_quantity(admin_client, order_with_lines):