Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Mutation modifications and tests for no order email when sending
Tomasz Szymanski378b1d283ffa
diff --git a/saleor/graphql/invoice/mutations.py b/saleor/graphql/invoice/mutations.pyindex 255a93f1b4..3655c74388 100644--- a/saleor/graphql/invoice/mutations.py+++ b/saleor/graphql/invoice/mutations.py@@ -254,19 +254,30 @@ class SendInvoiceEmail(ModelMutation):@classmethoddef clean_instance(cls, info, instance):- error_code = None-+ validation_errors = {}if instance.status != JobStatus.SUCCESS:- error_code = InvoiceErrorCode.NOT_READY- elif not instance.external_url:- error_code = InvoiceErrorCode.URL_NOT_SET- elif not instance.number:- error_code = InvoiceErrorCode.NUMBER_NOT_SET-- if error_code:- raise ValidationError(- "Provided invoice is not ready to be sent.", code=error_code+ validation_errors["invoice"] = ValidationError(+ "Provided invoice is not ready to be sent.",+ code=InvoiceErrorCode.NOT_READY,+ )+ if not instance.url:+ validation_errors["url"] = ValidationError(+ "Provided invoice needs to have an URL.",+ code=InvoiceErrorCode.URL_NOT_SET,)+ if not instance.number:+ validation_errors["number"] = ValidationError(+ "Provided invoice needs to have an invoice number.",+ code=InvoiceErrorCode.NUMBER_NOT_SET,+ )+ if not instance.order.get_email():+ validation_errors["order"] = ValidationError(+ "Provided invoice order needs an email address.",+ code=InvoiceErrorCode.EMAIL_NOT_SET,+ )++ if validation_errors:+ raise ValidationError(validation_errors)@classmethoddef perform_mutation(cls, _root, info, **data):diff --git a/saleor/graphql/invoice/tests/test_invoice.py b/saleor/graphql/invoice/tests/test_invoice.pyindex 32c5bfa3ed..1096d323a9 100644--- a/saleor/graphql/invoice/tests/test_invoice.py+++ b/saleor/graphql/invoice/tests/test_invoice.py@@ -494,8 +494,8 @@ def test_send_invoice(email_mock, staff_api_client, permission_manage_orders, or).exists()-@patch("saleor.invoice.emails.send_invoice.delay")-def test_send_pending_invoice(+@patch("saleor.invoice.emails.send_templated_mail")+def test_send_invoice_pending(email_mock, staff_api_client, permission_manage_orders, order):invoice = Invoice.objects.create(@@ -507,12 +507,16 @@ def test_send_pending_invoice()content = get_graphql_content(response)errors = content["data"]["sendInvoiceEmail"]["invoiceErrors"]- assert errors == [{"field": None, "code": InvoiceErrorCode.NOT_READY.name}]+ assert errors == [+ {"field": "invoice", "code": "NOT_READY"},+ {"field": "url", "code": "URL_NOT_SET"},+ {"field": "number", "code": "NUMBER_NOT_SET"},+ ]email_mock.assert_not_called()-@patch("saleor.invoice.emails.send_invoice.delay")-def test_send_not_ready_invoice(+@patch("saleor.invoice.emails.send_templated_mail")+def test_send_invoice_without_url_and_number(email_mock, staff_api_client, permission_manage_orders, order):invoice = Invoice.objects.create(@@ -524,5 +528,31 @@ def test_send_not_ready_invoice()content = get_graphql_content(response)errors = content["data"]["sendInvoiceEmail"]["invoiceErrors"]- assert errors == [{"field": None, "code": InvoiceErrorCode.URL_NOT_SET.name}]+ assert errors == [+ {"field": "url", "code": "URL_NOT_SET"},+ {"field": "number", "code": "NUMBER_NOT_SET"},+ ]email_mock.assert_not_called()+++@patch("saleor.invoice.emails.send_templated_mail")+@patch("saleor.order.models.Order.get_email")+def test_send_invoice_without_email(+ order_mock, email_mock, staff_api_client, permission_manage_orders, order+):+ order_mock.return_value = None+ invoice = Invoice.objects.create(+ order=order,+ number="01/12/2020/TEST",+ url="http://www.example.com",+ status=JobStatus.SUCCESS,+ )+ variables = {"id": graphene.Node.to_global_id("Invoice", invoice.pk)}+ response = staff_api_client.post_graphql(+ SEND_INVOICE_MUTATION, variables, permissions=[permission_manage_orders]+ )+ content = get_graphql_content(response)+ email_mock.assert_not_called()+ assert order_mock.called+ errors = content["data"]["sendInvoiceEmail"]["invoiceErrors"]+ assert errors == [{"field": "order", "code": "EMAIL_NOT_SET"}]diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 6270389444..5feacb4c5d 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1980,6 +1980,7 @@ enum InvoiceErrorCode {REQUIREDNOT_READYURL_NOT_SET+ EMAIL_NOT_SETNUMBER_NOT_SETNOT_FOUNDINVALID_STATUSdiff --git a/saleor/invoice/emails.py b/saleor/invoice/emails.pyindex a888c5acfb..3cc5c09d82 100644--- a/saleor/invoice/emails.py+++ b/saleor/invoice/emails.py@@ -10,14 +10,13 @@ from .models import Invoicedef collect_invoice_data_for_email(invoice, template):"""Collect the required data for sending emails."""- recipient_email = invoice.order.user_email or invoice.order.user.emailsend_kwargs, email_context = get_email_context()email_context["number"] = invoice.numberemail_context["download_url"] = invoice.urlreturn {- "recipient_list": [recipient_email],+ "recipient_list": [invoice.order.get_email()],"template_name": template,"context": email_context,**send_kwargs,@@ -32,4 +31,4 @@ def send_invoice(invoice_pk, staff_user_pk):send_templated_mail(**email_data)events.invoice_sent_event(user=User.objects.get(pk=staff_user_pk), invoice=invoice)manager = get_plugins_manager()- manager.invoice_sent(invoice, invoice.order.user_email or invoice.order.user.email)+ manager.invoice_sent(invoice, invoice.order.get_email())diff --git a/saleor/invoice/error_codes.py b/saleor/invoice/error_codes.pyindex 65e209c58e..e7f057abff 100644--- a/saleor/invoice/error_codes.py+++ b/saleor/invoice/error_codes.py@@ -5,6 +5,7 @@ class InvoiceErrorCode(Enum):REQUIRED = "required"NOT_READY = "not_ready"URL_NOT_SET = "url_not_set"+ EMAIL_NOT_SET = "email_not_set"NUMBER_NOT_SET = "number_not_set"NOT_FOUND = "not_found"INVALID_STATUS = "invalid_status"diff --git a/saleor/order/models.py b/saleor/order/models.pyindex 7634aa5674..e5f1c4c62c 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -317,6 +317,9 @@ class Order(ModelWithMetadata):def total_balance(self):return self.total_captured - self.total.gross+ def get_email(self):+ return self.user_email or (self.user.email if self.user else None)+def get_total_weight(self):return self.weight