Skip to content

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

Explicit error when validation number and url

Tomasz Szymanskid08997714562
diff --git a/saleor/graphql/invoice/mutations.py b/saleor/graphql/invoice/mutations.py
index 67bdff01bd..b86ddcdbfb 100644
--- a/saleor/graphql/invoice/mutations.py
+++ b/saleor/graphql/invoice/mutations.py
@@ -211,15 +211,22 @@ class UpdateInvoice(ModelMutation):
def clean_input(cls, info, instance, data):
number = instance.number or data["input"].get("number")
url = instance.external_url or data["input"].get("url")
- if not number or not url:
- raise ValidationError(
- {
- "invoice": ValidationError(
- "URL and number need to be set after update operation.",
- code=InvoiceErrorCode.URL_OR_NUMBER_NOT_SET,
- )
- }
+
+ validation_errors = {}
+ if not number:
+ validation_errors["number"] = ValidationError(
+ "Number need to be set after update operation.",
+ code=InvoiceErrorCode.NUMBER_NOT_SET,
)
+ if not url:
+ validation_errors["url"] = ValidationError(
+ "URL need to be set after update operation.",
+ code=InvoiceErrorCode.URL_NOT_SET,
+ )
+
+ if validation_errors:
+ raise ValidationError(validation_errors)
+
return data["input"]
@classmethod
@@ -251,8 +258,10 @@ class SendInvoiceEmail(ModelMutation):
if instance.status != JobStatus.SUCCESS:
error_code = InvoiceErrorCode.NOT_READY
- elif not instance.external_url or not instance.number:
- error_code = InvoiceErrorCode.URL_OR_NUMBER_NOT_SET
+ 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(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 123d1d99fa..b71f71c886 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1978,7 +1978,8 @@ type InvoiceError {
enum InvoiceErrorCode {
REQUIRED
NOT_READY
- URL_OR_NUMBER_NOT_SET
+ URL_NOT_SET
+ NUMBER_NOT_SET
NOT_FOUND
INVALID_STATUS
}
diff --git a/saleor/invoice/error_codes.py b/saleor/invoice/error_codes.py
index 7497bd0cc7..65e209c58e 100644
--- a/saleor/invoice/error_codes.py
+++ b/saleor/invoice/error_codes.py
@@ -4,6 +4,7 @@ from enum import Enum
class InvoiceErrorCode(Enum):
REQUIRED = "required"
NOT_READY = "not_ready"
- URL_OR_NUMBER_NOT_SET = "url_or_number_not_set"
+ URL_NOT_SET = "url_not_set"
+ NUMBER_NOT_SET = "number_not_set"
NOT_FOUND = "not_found"
INVALID_STATUS = "invalid_status"
diff --git a/saleor/plugins/invoicing/plugin.py b/saleor/plugins/invoicing/plugin.py
index 0a28551e76..2092f3da65 100644
--- a/saleor/plugins/invoicing/plugin.py
+++ b/saleor/plugins/invoicing/plugin.py
@@ -32,13 +32,6 @@ class InvoicingPlugin(BasePlugin):
)
invoice.status = JobStatus.SUCCESS
invoice.save(
- update_fields=[
- "created",
- "number",
- "url",
- "invoice_file",
- "status",
- "updated_at",
- ]
+ update_fields=["created", "number", "invoice_file", "status", "updated_at"]
)
return invoice
diff --git a/saleor/plugins/invoicing/utils.py b/saleor/plugins/invoicing/utils.py
index 075fa48071..356c26db86 100644
--- a/saleor/plugins/invoicing/utils.py
+++ b/saleor/plugins/invoicing/utils.py
@@ -32,7 +32,7 @@ def generate_invoice_number():
try:
last_number = parse_invoice_number(last_invoice)
return make_full_invoice_number(last_number + 1)
- except (IndexError, ValueError):
+ except (IndexError, ValueError, AttributeError):
return make_full_invoice_number()
diff --git a/tests/api/test_invoice.py b/tests/api/test_invoice.py
index e8d9b56cc9..d747f4d5bd 100644
--- a/tests/api/test_invoice.py
+++ b/tests/api/test_invoice.py
@@ -339,8 +339,8 @@ def test_update_invoice_missing_number(
content = get_graphql_content(response)
invoice.refresh_from_db()
error = content["data"]["updateInvoice"]["invoiceErrors"][0]
- assert error["code"] == InvoiceErrorCode.URL_OR_NUMBER_NOT_SET.name
- assert error["field"] == "invoice"
+ assert error["code"] == InvoiceErrorCode.NUMBER_NOT_SET.name
+ assert error["field"] == "number"
assert invoice.url is None
assert invoice.status == JobStatus.PENDING
@@ -520,8 +520,5 @@ def test_send_not_ready_invoice(
)
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}
- ]
+ assert errors == [{"field": None, "code": InvoiceErrorCode.URL_NOT_SET.name}]
email_mock.assert_not_called()