Skip to content

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

Vdlidation refactor

Tomasz Szymanski5552624a557f
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index a25baaa34b..0e193ba2aa 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -687,23 +687,17 @@ class SendInvoiceEmail(ModelMutation):
@classmethod
def clean_instance(cls, info, instance):
- validation_errors = {}
+ error_code = None
+
if instance.status != InvoiceStatus.READY:
- validation_errors["invoice"] = ValidationError(
- "Invoice needs to be ready in order to send it.",
- code=InvoiceErrorCode.INVALID,
+ error_code = InvoiceErrorCode.NOT_READY
+ elif not instance.url or not instance.number:
+ error_code = InvoiceErrorCode.URL_OR_NUMBER_NOT_SET
+
+ if error_code:
+ raise ValidationError(
+ "Provided invoice is not ready to be sent.", code=error_code
)
- for field in ("url", "number"):
- if not getattr(instance, field):
- validation_errors[field] = ValidationError(
- (
- f"Provided invoice needs to have {field} "
- "assigned in order to be sent."
- ),
- code=InvoiceErrorCode.REQUIRED,
- )
- if validation_errors:
- raise ValidationError(validation_errors)
@classmethod
def perform_mutation(cls, _root, info, **data):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 01f3e35fb5..dcdfc173df 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1881,7 +1881,7 @@ type InvoiceError {
enum InvoiceErrorCode {
REQUIRED
- INVALID
+ NOT_READY
URL_OR_NUMBER_NOT_SET
NOT_FOUND
}
diff --git a/saleor/order/error_codes.py b/saleor/order/error_codes.py
index c782b95d1e..088558c3dd 100644
--- a/saleor/order/error_codes.py
+++ b/saleor/order/error_codes.py
@@ -26,6 +26,6 @@ class OrderErrorCode(Enum):
class InvoiceErrorCode(Enum):
REQUIRED = "required"
- INVALID = "invalid"
+ NOT_READY = "not_ready"
URL_OR_NUMBER_NOT_SET = "url_or_number_not_set"
NOT_FOUND = "not_found"
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index 47be5a2070..9f5beada95 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -3173,7 +3173,25 @@ def test_send_pending_invoice(
)
content = get_graphql_content(response)
errors = content["data"]["sendInvoiceEmail"]["invoiceErrors"]
- assert errors[0] == {"field": "invoice", "code": InvoiceErrorCode.INVALID.name}
- assert errors[1] == {"field": "url", "code": InvoiceErrorCode.REQUIRED.name}
- assert errors[2] == {"field": "number", "code": InvoiceErrorCode.REQUIRED.name}
+ assert errors == [{"field": None, "code": InvoiceErrorCode.NOT_READY.name}]
+ email_mock.assert_not_called()
+
+
[email protected]("saleor.order.emails.send_invoice.delay")
+def test_send_not_ready_invoice(
+ email_mock, staff_api_client, permission_manage_orders, order
+):
+ invoice = Invoice.objects.create(
+ order=order, number=None, url=None, status=InvoiceStatus.READY
+ )
+ 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)
+ errors = content["data"]["sendInvoiceEmail"]["invoiceErrors"]
+ [{"field": None, "code": "URL_OR_NUMBER_NOT_SET"}]
+ assert errors == [
+ {"field": None, "code": InvoiceErrorCode.URL_OR_NUMBER_NOT_SET.name}
+ ]
email_mock.assert_not_called()