Skip to content

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

Add CUSTOMER_DELETED webhook event (#10060)

SzymJd37c29148a92
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bc33e1451c..6d4f155b31 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add webhooks `STAFF_CREATED`, `STAFF_UPDATED` and `STAFF_DELETED` - #9949 by @SzymJ
- Add webhooks `ATTRIBUTE_CREATED`, `ATTRIBUTE_UPDATED` and `ATTRIBUTE_DELETED` - #9991 by @SzymJ
- Add webhooks `ATTRIBUTE_VALUE_CREATED`, `ATTRIBUTE_VALUE_UPDATED` and `ATTRIBUTE_VALUE_DELETED` - #10035 by @SzymJ
+- Add webhook `CUSTOMER_DELETED` - #10060 by @SzymJ
- Fix returning errors in subscription webhooks payloads - #9905 by @SzymJ
# 3.4.0
diff --git a/saleor/graphql/account/bulk_mutations.py b/saleor/graphql/account/bulk_mutations.py
index 5d333a8a73..eedba450e4 100644
--- a/saleor/graphql/account/bulk_mutations.py
+++ b/saleor/graphql/account/bulk_mutations.py
@@ -37,6 +37,13 @@ class CustomerBulkDelete(CustomerDeleteMixin, UserBulkDelete):
cls.post_process(info, count)
return count, errors
+ @classmethod
+ def bulk_action(cls, info, queryset):
+ instances = list(queryset)
+ queryset.delete()
+ for instance in instances:
+ info.context.plugins.customer_deleted(instance)
+
class StaffBulkDelete(StaffDeleteMixin, UserBulkDelete):
class Meta:
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index 0ed5a9284a..83b8e23686 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -174,6 +174,10 @@ class CustomerDelete(CustomerDeleteMixin, UserDelete):
cls.post_process(info)
return results
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ info.context.plugins.customer_deleted(instance)
+
class StaffCreate(ModelMutation):
class Arguments:
diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.py
index 222d60e9d8..214ab60a78 100644
--- a/saleor/graphql/account/tests/test_account.py
+++ b/saleor/graphql/account/tests/test_account.py
@@ -36,7 +36,11 @@ from ....order import OrderStatus
from ....order.models import FulfillmentStatus, Order
from ....product.tests.utils import create_image
from ....webhook.event_types import WebhookEventAsyncType
-from ....webhook.payloads import generate_meta, generate_requestor
+from ....webhook.payloads import (
+ generate_customer_payload,
+ generate_meta,
+ generate_requestor,
+)
from ...core.utils import str_to_enum, to_global_id_or_none
from ...tests.utils import (
assert_graphql_error_with_message,
@@ -2231,6 +2235,44 @@ def test_customer_delete(
delete_versatile_image_mock.assert_called_once_with(customer_user.avatar)
+@freeze_time("2018-05-31 12:00:01")
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_customer_delete_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ customer_user,
+ permission_manage_users,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+ variables = {"id": customer_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ CUSTOMER_DELETE_MUTATION, variables, permissions=[permission_manage_users]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["customerDelete"]
+
+ # then
+ assert data["errors"] == []
+ assert data["user"]["id"] == customer_id
+ mocked_webhook_trigger.assert_called_once_with(
+ generate_customer_payload(customer_user, staff_api_client.user),
+ WebhookEventAsyncType.CUSTOMER_DELETED,
+ [any_webhook],
+ customer_user,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+
@patch("saleor.account.signals.delete_versatile_image")
@patch("saleor.graphql.account.utils.account_events.customer_deleted_event")
def test_customer_delete_by_app(
diff --git a/saleor/graphql/account/tests/test_bulk_delete.py b/saleor/graphql/account/tests/test_bulk_delete.py
index f2fc6e725e..2be94ce824 100644
--- a/saleor/graphql/account/tests/test_bulk_delete.py
+++ b/saleor/graphql/account/tests/test_bulk_delete.py
@@ -58,6 +58,37 @@ def test_delete_customers(
)
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_delete_customers_trigger_webhooks(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ staff_user,
+ user_list,
+ permission_manage_users,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ variables = {
+ "ids": [graphene.Node.to_global_id("User", user.id) for user in user_list]
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ CUSTOMER_BULK_DELETE_MUTATION, variables, permissions=[permission_manage_users]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ assert content["data"]["customerBulkDelete"]["count"] == 2
+ assert mocked_webhook_trigger.call_count == 2
+
+
@patch("saleor.graphql.account.utils.account_events.customer_deleted_event")
def test_delete_customers_by_app(
mocked_deletion_event,
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 020f64b7fa..153e205f64 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1464,6 +1464,9 @@ enum WebhookEventTypeEnum {
"""A customer account is updated."""
CUSTOMER_UPDATED
+ """A customer account is deleted."""
+ CUSTOMER_DELETED
+
"""A new collection is created."""
COLLECTION_CREATED
@@ -1756,6 +1759,9 @@ enum WebhookEventTypeAsyncEnum {
"""A customer account is updated."""
CUSTOMER_UPDATED
+ """A customer account is deleted."""
+ CUSTOMER_DELETED
+
"""A new collection is created."""
COLLECTION_CREATED
@@ -2385,6 +2391,7 @@ enum WebhookSampleEventTypeEnum {
INVOICE_SENT
CUSTOMER_CREATED
CUSTOMER_UPDATED
+ CUSTOMER_DELETED
COLLECTION_CREATED
COLLECTION_UPDATED
COLLECTION_DELETED
diff --git a/saleor/graphql/webhook/enums.py b/saleor/graphql/webhook/enums.py
index 920c6d7c58..d7c6ee26d3 100644
--- a/saleor/graphql/webhook/enums.py
+++ b/saleor/graphql/webhook/enums.py
@@ -50,6 +50,7 @@ WEBHOOK_EVENT_DESCRIPTION = {
WebhookEventAsyncType.COLLECTION_DELETED: "A collection is deleted.",
WebhookEventAsyncType.CUSTOMER_CREATED: "A new customer account is created.",
WebhookEventAsyncType.CUSTOMER_UPDATED: "A customer account is updated.",
+ WebhookEventAsyncType.CUSTOMER_DELETED: "A customer account is deleted.",
WebhookEventAsyncType.GIFT_CARD_CREATED: "A new gift card created.",
WebhookEventAsyncType.GIFT_CARD_UPDATED: "A gift card is updated.",
WebhookEventAsyncType.GIFT_CARD_DELETED: "A gift card is deleted.",
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 005a2e86af..4810946b47 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -424,6 +424,12 @@ class BasePlugin:
# created.
customer_created: Callable[["User", Any], Any]
+ # Trigger when user is deleted.
+ #
+ # Overwrite this method if you need to trigger specific logic after a user is
+ # deleted.
+ customer_deleted: Callable[["User", Any], Any]
+
# Trigger when user is updated.
#
# Overwrite this method if you need to trigger specific logic after a user is
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 9736e96289..42b547a748 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -570,6 +570,10 @@ class PluginsManager(PaymentInterface):
default_value = None
return self.__run_method_on_plugins("customer_created", default_value, customer)
+ def customer_deleted(self, customer: "User"):
+ default_value = None
+ return self.__run_method_on_plugins("customer_deleted", default_value, customer)
+
def customer_updated(self, customer: "User"):
default_value = None
return self.__run_method_on_plugins("customer_updated", default_value, customer)
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index c6ed1af92e..d5aa43a022 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -604,6 +604,16 @@ class WebhookPlugin(BasePlugin):
customer_data, event_type, webhooks, customer, self.requestor
)
+ def customer_deleted(self, customer: "User", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ event_type = WebhookEventAsyncType.CUSTOMER_DELETED
+ if webhooks := get_webhooks_for_event(event_type):
+ customer_data = generate_customer_payload(customer, self.requestor)
+ trigger_webhooks_async(
+ customer_data, event_type, webhooks, customer, self.requestor
+ )
+
def collection_created(self, collection: "Collection", previous_value: Any) -> Any:
if not self.active:
return previous_value
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
index 070487cbf7..416e169a9e 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
@@ -483,6 +483,13 @@ def subscription_customer_updated_webhook(subscription_webhook):
)
+def subscription_customer_deleted_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.CUSTOMER_DELETED, WebhookEventAsyncType.CUSTOMER_DELETED
+ )
+
+
@pytest.fixture
def subscription_collection_created_webhook(subscription_webhook):
return subscription_webhook(
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
index 346083d863..a39cdcba9e 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -954,6 +954,23 @@ CUSTOMER_UPDATED = (
"""
)
+
+CUSTOMER_DELETED = (
+ fragments.CUSTOMER_DETAILS
+ + """
+ subscription{
+ event{
+ ...on CustomerDeleted{
+ user{
+ ...CustomerDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+
COLLECTION_CREATED = (
fragments.COLLECTION
+ """
@@ -969,6 +986,7 @@ COLLECTION_CREATED = (
"""
)
+
COLLECTION_UPDATED = (
fragments.COLLECTION
+ """
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
index bf27f17e3a..2591a06c53 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
@@ -1229,6 +1229,27 @@ def test_customer_updated(customer_user, subscription_customer_updated_webhook):
assert deliveries[0].webhook == webhooks[0]
+def test_customer_deleted(customer_user, subscription_customer_created_webhook):
+ # given
+ customer_user_id = customer_user.id
+ customer_user.delete()
+ customer_user.id = customer_user_id
+
+ webhooks = [subscription_customer_created_webhook]
+ event_type = WebhookEventAsyncType.CUSTOMER_CREATED
+ expected_payload = json.dumps(generate_customer_payload(customer_user))
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(
+ event_type, customer_user, webhooks
+ )
+
+ # then
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
def test_collection_created(
collection_with_products, subscription_collection_created_webhook
):
diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.py
index d5445229b1..89d4208f83 100644
--- a/saleor/webhook/event_types.py
+++ b/saleor/webhook/event_types.py
@@ -81,6 +81,7 @@ class WebhookEventAsyncType:
CUSTOMER_CREATED = "customer_created"
CUSTOMER_UPDATED = "customer_updated"
+ CUSTOMER_DELETED = "customer_deleted"
COLLECTION_CREATED = "collection_created"
COLLECTION_UPDATED = "collection_updated"
@@ -186,6 +187,7 @@ class WebhookEventAsyncType:
INVOICE_SENT: "Invoice sent",
CUSTOMER_CREATED: "Customer created",
CUSTOMER_UPDATED: "Customer updated",
+ CUSTOMER_DELETED: "Customer deleted",
COLLECTION_CREATED: "Collection created",
COLLECTION_UPDATED: "Collection updated",
COLLECTION_DELETED: "Collection deleted",
@@ -278,6 +280,7 @@ class WebhookEventAsyncType:
(INVOICE_SENT, DISPLAY_LABELS[INVOICE_SENT]),
(CUSTOMER_CREATED, DISPLAY_LABELS[CUSTOMER_CREATED]),
(CUSTOMER_UPDATED, DISPLAY_LABELS[CUSTOMER_UPDATED]),
+ (CUSTOMER_DELETED, DISPLAY_LABELS[CUSTOMER_DELETED]),
(COLLECTION_CREATED, DISPLAY_LABELS[COLLECTION_CREATED]),
(COLLECTION_UPDATED, DISPLAY_LABELS[COLLECTION_UPDATED]),
(COLLECTION_DELETED, DISPLAY_LABELS[COLLECTION_DELETED]),
@@ -371,6 +374,7 @@ class WebhookEventAsyncType:
INVOICE_SENT: OrderPermissions.MANAGE_ORDERS,
CUSTOMER_CREATED: AccountPermissions.MANAGE_USERS,
CUSTOMER_UPDATED: AccountPermissions.MANAGE_USERS,
+ CUSTOMER_DELETED: AccountPermissions.MANAGE_USERS,
COLLECTION_CREATED: ProductPermissions.MANAGE_PRODUCTS,
COLLECTION_UPDATED: ProductPermissions.MANAGE_PRODUCTS,
COLLECTION_DELETED: ProductPermissions.MANAGE_PRODUCTS,
@@ -544,6 +548,7 @@ SUBSCRIBABLE_EVENTS = [
WebhookEventAsyncType.FULFILLMENT_CANCELED,
WebhookEventAsyncType.CUSTOMER_CREATED,
WebhookEventAsyncType.CUSTOMER_UPDATED,
+ WebhookEventAsyncType.CUSTOMER_DELETED,
WebhookEventAsyncType.COLLECTION_CREATED,
WebhookEventAsyncType.COLLECTION_UPDATED,
WebhookEventAsyncType.COLLECTION_DELETED,