Skip to content

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

Add checkoutDelete mutation (#19126)

Jayesh Patelcf9b59513d0a
diff --git a/saleor/graphql/checkout/mutations/__init__.py b/saleor/graphql/checkout/mutations/__init__.py
index 8927927f8d..c4615bbd78 100644
--- a/saleor/graphql/checkout/mutations/__init__.py
+++ b/saleor/graphql/checkout/mutations/__init__.py
@@ -6,6 +6,7 @@ from .checkout_create_from_order import CheckoutCreateFromOrder
from .checkout_customer_attach import CheckoutCustomerAttach
from .checkout_customer_detach import CheckoutCustomerDetach
from .checkout_customer_note_update import CheckoutCustomerNoteUpdate
+from .checkout_delete import CheckoutDelete
from .checkout_delivery_method_update import CheckoutDeliveryMethodUpdate
from .checkout_email_update import CheckoutEmailUpdate
from .checkout_language_code_update import CheckoutLanguageCodeUpdate
@@ -27,6 +28,7 @@ __all__ = [
"CheckoutCustomerAttach",
"CheckoutCustomerDetach",
"CheckoutCustomerNoteUpdate",
+ "CheckoutDelete",
"CheckoutDeliveryMethodUpdate",
"CheckoutEmailUpdate",
"CheckoutLanguageCodeUpdate",
diff --git a/saleor/graphql/checkout/mutations/checkout_delete.py b/saleor/graphql/checkout/mutations/checkout_delete.py
new file mode 100644
index 0000000000..f9e413476a
--- /dev/null
+++ b/saleor/graphql/checkout/mutations/checkout_delete.py
@@ -0,0 +1,58 @@
+import graphene
+from django.core.exceptions import ValidationError
+
+from ....checkout.error_codes import CheckoutErrorCode
+from ....checkout.models import Checkout as CheckoutModel
+from ....checkout.utils import delete_checkouts
+from ....permission.enums import CheckoutPermissions
+from ...core import ResolveInfo
+from ...core.descriptions import ADDED_IN_323
+from ...core.doc_category import DOC_CATEGORY_CHECKOUT
+from ...core.enums import CheckoutErrorCode as CheckoutErrorCodeEnum
+from ...core.mutations import BaseMutation
+from ...core.types import Error
+from ..types import Checkout
+
+
+class CheckoutDeleteError(Error):
+ code = CheckoutErrorCodeEnum(description="The error code.", required=True)
+
+ class Meta:
+ doc_category = DOC_CATEGORY_CHECKOUT
+
+
+class CheckoutDelete(BaseMutation):
+ class Arguments:
+ id = graphene.ID(
+ description="The checkout's ID." + ADDED_IN_323,
+ required=True,
+ )
+
+ class Meta:
+ description = "Deletes a checkout." + ADDED_IN_323
+ doc_category = DOC_CATEGORY_CHECKOUT
+ permissions = (CheckoutPermissions.MANAGE_CHECKOUTS,)
+ error_type_class = CheckoutDeleteError
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls, _root, info: ResolveInfo, /, *, id: str
+ ):
+ checkout = cls.get_node_or_error(
+ info,
+ id,
+ only_type=Checkout,
+ qs=CheckoutModel.objects,
+ code=CheckoutErrorCode.NOT_FOUND.value,
+ )
+ if checkout.payment_transactions.exists():
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ "Cannot delete checkout with attached transactions.",
+ code=CheckoutErrorCode.INVALID.value,
+ )
+ }
+ )
+ delete_checkouts([checkout.pk])
+ return CheckoutDelete()
diff --git a/saleor/graphql/checkout/schema.py b/saleor/graphql/checkout/schema.py
index 246554438c..b22ba1fc88 100644
--- a/saleor/graphql/checkout/schema.py
+++ b/saleor/graphql/checkout/schema.py
@@ -26,6 +26,7 @@ from .mutations import (
CheckoutCustomerAttach,
CheckoutCustomerDetach,
CheckoutCustomerNoteUpdate,
+ CheckoutDelete,
CheckoutDeliveryMethodUpdate,
CheckoutEmailUpdate,
CheckoutLanguageCodeUpdate,
@@ -132,6 +133,7 @@ class CheckoutMutations(graphene.ObjectType):
checkout_customer_attach = CheckoutCustomerAttach.Field()
checkout_customer_detach = CheckoutCustomerDetach.Field()
checkout_customer_note_update = CheckoutCustomerNoteUpdate.Field()
+ checkout_delete = CheckoutDelete.Field()
checkout_email_update = CheckoutEmailUpdate.Field()
checkout_line_delete = CheckoutLineDelete.Field(
deprecation_reason="Use `checkoutLinesDelete` instead."
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_delete.py b/saleor/graphql/checkout/tests/mutations/test_checkout_delete.py
new file mode 100644
index 0000000000..3c59f0755e
--- /dev/null
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_delete.py
@@ -0,0 +1,199 @@
+import graphene
+import pytest
+
+from .....checkout.error_codes import CheckoutErrorCode
+from .....checkout.models import Checkout
+from .....payment.models import TransactionItem
+from ....core.utils import to_global_id_or_none
+from ....tests.utils import assert_no_permission, get_graphql_content
+
+MUTATION_CHECKOUT_DELETE = """
+ mutation checkoutDelete($id: ID!) {
+ checkoutDelete(id: $id) {
+ errors {
+ message
+ code
+ field
+ }
+ }
+ }
+"""
+
+
+def test_checkout_delete_by_staff(
+ staff_api_client, checkout_with_items, permission_manage_checkouts
+):
+ # given
+ checkout = checkout_with_items
+ checkout_id = to_global_id_or_none(checkout)
+ variables = {"id": checkout_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ MUTATION_CHECKOUT_DELETE,
+ variables,
+ permissions=[permission_manage_checkouts],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutDelete"]
+ assert data["errors"] == []
+ assert not Checkout.objects.filter(pk=checkout.pk).exists()
+
+
+def test_checkout_delete_by_app(
+ app_api_client, checkout_with_items, permission_manage_checkouts
+):
+ # given
+ checkout = checkout_with_items
+ checkout_id = to_global_id_or_none(checkout)
+ variables = {"id": checkout_id}
+
+ # when
+ response = app_api_client.post_graphql(
+ MUTATION_CHECKOUT_DELETE,
+ variables,
+ permissions=[permission_manage_checkouts],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutDelete"]
+ assert data["errors"] == []
+ assert not Checkout.objects.filter(pk=checkout.pk).exists()
+
+
[email protected]("client", ["api_client", "user_api_client"])
+def test_checkout_delete_without_permission_is_denied(
+ client, request, checkout_with_items
+):
+ # given
+ api_client = request.getfixturevalue(client)
+ checkout = checkout_with_items
+ checkout_id = to_global_id_or_none(checkout)
+ variables = {"id": checkout_id}
+
+ # when
+ response = api_client.post_graphql(MUTATION_CHECKOUT_DELETE, variables)
+
+ # then
+ assert_no_permission(response)
+ assert Checkout.objects.filter(pk=checkout.pk).exists()
+
+
+def test_checkout_delete_by_staff_without_permission_is_denied(
+ staff_api_client, checkout_with_items
+):
+ # given
+ checkout = checkout_with_items
+ checkout_id = to_global_id_or_none(checkout)
+ variables = {"id": checkout_id}
+
+ # when
+ response = staff_api_client.post_graphql(MUTATION_CHECKOUT_DELETE, variables)
+
+ # then
+ assert_no_permission(response)
+ assert Checkout.objects.filter(pk=checkout.pk).exists()
+
+
+def test_checkout_delete_by_app_without_permission_is_denied(
+ app_api_client, checkout_with_items
+):
+ # given
+ checkout = checkout_with_items
+ checkout_id = to_global_id_or_none(checkout)
+ variables = {"id": checkout_id}
+
+ # when
+ response = app_api_client.post_graphql(MUTATION_CHECKOUT_DELETE, variables)
+
+ # then
+ assert_no_permission(response)
+ assert Checkout.objects.filter(pk=checkout.pk).exists()
+
+
+def test_checkout_delete_invalid_checkout_id(
+ staff_api_client, checkout_with_items, permission_manage_checkouts
+):
+ # given
+ checkout = checkout_with_items
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ checkout.delete()
+ variables = {"id": checkout_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ MUTATION_CHECKOUT_DELETE,
+ variables,
+ permissions=[permission_manage_checkouts],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ errors = content["data"]["checkoutDelete"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == CheckoutErrorCode.NOT_FOUND.name
+ assert errors[0]["message"] == f"Couldn't resolve to a node: {checkout_id}"
+
+
+def test_checkout_delete_with_id_of_different_type(
+ staff_api_client,
+ checkout_with_items,
+ customer_user,
+ permission_manage_checkouts,
+):
+ # given
+ checkout = checkout_with_items
+ fake_checkout_id = graphene.Node.to_global_id("User", customer_user.pk)
+ variables = {"id": fake_checkout_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ MUTATION_CHECKOUT_DELETE,
+ variables,
+ permissions=[permission_manage_checkouts],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ errors = content["data"]["checkoutDelete"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == CheckoutErrorCode.GRAPHQL_ERROR.name
+ assert errors[0]["message"] == (
+ f"Invalid ID: {fake_checkout_id}. Expected: Checkout, received: User."
+ )
+ assert Checkout.objects.filter(pk=checkout.pk).exists()
+
+
+def test_checkout_delete_with_attached_transaction(
+ staff_api_client,
+ checkout_with_items,
+ permission_manage_checkouts,
+ transaction_item_generator,
+):
+ # given
+ checkout = checkout_with_items
+ checkout_id = to_global_id_or_none(checkout)
+ transaction = transaction_item_generator(checkout_id=checkout.pk)
+ variables = {"id": checkout_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ MUTATION_CHECKOUT_DELETE,
+ variables,
+ permissions=[permission_manage_checkouts],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ errors = content["data"]["checkoutDelete"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == CheckoutErrorCode.INVALID.name
+ assert errors[0]["message"] == "Cannot delete checkout with attached transactions."
+ assert Checkout.objects.filter(pk=checkout.pk).exists()
+ assert TransactionItem.objects.filter(pk=transaction.pk).exists()
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index b56b6960ea..7936064a87 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -20777,6 +20777,22 @@ type Mutation {
id: ID!
): CheckoutCustomerNoteUpdate @doc(category: "Checkout") @webhookEventsInfo(asyncEvents: [CHECKOUT_UPDATED], syncEvents: [])
+ """
+ Deletes a checkout.
+
+ Added in Saleor 3.23.
+
+ Requires one of the following permissions: MANAGE_CHECKOUTS.
+ """
+ checkoutDelete(
+ """
+ The checkout's ID.
+
+ Added in Saleor 3.23.
+ """
+ id: ID!
+ ): CheckoutDelete @doc(category: "Checkout")
+
"""
Updates email address in the existing checkout object.
@@ -30791,6 +30807,30 @@ type CheckoutCustomerNoteUpdate @doc(category: "Checkout") @webhookEventsInfo(as
errors: [CheckoutError!]!
}
+"""
+Deletes a checkout.
+
+Added in Saleor 3.23.
+
+Requires one of the following permissions: MANAGE_CHECKOUTS.
+"""
+type CheckoutDelete @doc(category: "Checkout") {
+ errors: [CheckoutDeleteError!]!
+}
+
+type CheckoutDeleteError @doc(category: "Checkout") {
+ """
+ Name of a field that caused the error. A value of `null` indicates that the error isn't associated with a particular field.
+ """
+ field: String
+
+ """The error message."""
+ message: String
+
+ """The error code."""
+ code: CheckoutErrorCode!
+}
+
"""
Updates email address in the existing checkout object.