Skip to content

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

Add customer updated webhook (#6898)

Piotr Grundase089e93419bf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 70c5d9cdfb..e4d656e9c5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add query contains only schema validation - #6827 by @fowczarek
- Add introspection caching - #6871 by @fowczarek
- Refactor plugins manager(add missing tracing, optimize imports, drop plugins manager from settings) - #6890 by @fowczarek
+- Add CUSTOMER_UPDATED webhook, add addresses field to customer CUSTOMER_CREATED webhook - #6898 by @piotrgrundas
- Add missing span in PluginManager - #6900 by @fowczarek
### Breaking
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 5665f4e791..4d4099e47a 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -513,6 +513,7 @@ def create_fake_user(save=True):
pass
user = User(
+ id=fake.numerify(),
first_name=address.first_name,
last_name=address.last_name,
email=email,
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.py
index 9fc3a3932b..4555fbb54c 100644
--- a/saleor/graphql/account/mutations/account.py
+++ b/saleor/graphql/account/mutations/account.py
@@ -274,6 +274,7 @@ class AccountAddressCreate(ModelMutation, I18nMixin):
super().save(info, instance, cleaned_input)
user = info.context.user
instance.user_addresses.add(user)
+ info.context.plugins.customer_updated(user)
class AccountAddressUpdate(BaseAddressUpdate):
@@ -331,6 +332,7 @@ class AccountSetDefaultAddress(BaseMutation):
address_type = AddressType.SHIPPING
utils.change_user_default_address(user, address, address_type)
+ info.context.plugins.customer_updated(user)
return cls(user=user)
@@ -455,4 +457,5 @@ class ConfirmEmailChange(BaseMutation):
account_events.customer_email_changed_event(
user=user, parameters=event_parameters
)
+ info.context.plugins.customer_updated(user)
return ConfirmEmailChange(user=user)
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index 6f73bcc7ee..bc208dc931 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -277,6 +277,7 @@ class BaseAddressUpdate(ModelMutation, I18nMixin):
cls.clean_instance(info, address)
cls.save(info, address, cleaned_input)
cls._save_m2m(info, address, cleaned_input)
+ info.context.plugins.customer_updated(user)
address = info.context.plugins.change_user_address(address, None, user)
success_response = cls.success_response(address)
success_response.user = user
@@ -334,6 +335,7 @@ class BaseAddressDelete(ModelDeleteMutation):
response = cls.success_response(instance)
response.user = user
+ info.context.plugins.customer_updated(user)
return response
@@ -436,6 +438,8 @@ class BaseCustomerCreate(ModelMutation, I18nMixin):
if is_creation:
info.context.plugins.customer_created(customer=instance)
account_events.customer_account_created_event(user=instance)
+ else:
+ info.context.plugins.customer_updated(instance)
if cleaned_input.get("redirect_url"):
send_set_password_email_with_url(
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index 7e68fd3655..5df72668fa 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -511,6 +511,7 @@ class AddressSetDefault(BaseMutation):
address_type = AddressType.SHIPPING
utils.change_user_default_address(user, address, address_type)
+ info.context.plugins.customer_updated(user)
return cls(user=user)
diff --git a/saleor/graphql/meta/extra_methods.py b/saleor/graphql/meta/extra_methods.py
index 4967d68651..444d5e0a54 100644
--- a/saleor/graphql/meta/extra_methods.py
+++ b/saleor/graphql/meta/extra_methods.py
@@ -2,4 +2,8 @@ def extra_product_actions(instance, info, **data):
info.context.plugins.product_updated(instance)
-MODEL_EXTRA_METHODS = {"Product": extra_product_actions}
+def extra_user_actions(instance, info, **data):
+ info.context.plugins.customer_updated(instance)
+
+
+MODEL_EXTRA_METHODS = {"Product": extra_product_actions, "User": extra_user_actions}
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 4855977566..e82c2e7947 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -5827,6 +5827,7 @@ enum WebhookEventTypeEnum {
INVOICE_DELETED
INVOICE_SENT
CUSTOMER_CREATED
+ CUSTOMER_UPDATED
PRODUCT_CREATED
PRODUCT_UPDATED
PRODUCT_DELETED
@@ -5849,6 +5850,7 @@ enum WebhookSampleEventTypeEnum {
INVOICE_DELETED
INVOICE_SENT
CUSTOMER_CREATED
+ CUSTOMER_UPDATED
PRODUCT_CREATED
PRODUCT_UPDATED
PRODUCT_DELETED
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 1eafb7c3eb..3df736c2d3 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -413,6 +413,14 @@ class BasePlugin:
"""
return NotImplemented
+ def customer_updated(self, customer: "User", previous_value: Any) -> Any:
+ """Trigger when user is updated.
+
+ Overwrite this method if you need to trigger specific logic after a user is
+ updated.
+ """
+ return NotImplemented
+
def product_created(self, product: "Product", previous_value: Any) -> Any:
"""Trigger when product is created.
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 05a9595c48..08c2fa27b1 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -389,6 +389,10 @@ class PluginsManager(PaymentInterface):
default_value = None
return self.__run_method_on_plugins("customer_created", default_value, customer)
+ def customer_updated(self, customer: "User"):
+ default_value = None
+ return self.__run_method_on_plugins("customer_updated", default_value, customer)
+
def product_created(self, product: "Product"):
default_value = None
return self.__run_method_on_plugins("product_created", default_value, product)
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index 196c42619e..bbc1f9105d 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -110,6 +110,14 @@ class WebhookPlugin(BasePlugin):
WebhookEventType.CUSTOMER_CREATED, customer_data
)
+ def customer_updated(self, customer: "User", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ customer_data = generate_customer_payload(customer)
+ trigger_webhooks_for_event.delay(
+ WebhookEventType.CUSTOMER_UPDATED, customer_data
+ )
+
def product_created(self, product: "Product", previous_value: Any) -> Any:
if not self.active:
return previous_value
diff --git a/saleor/plugins/webhook/tests/test_webhook.py b/saleor/plugins/webhook/tests/test_webhook.py
index 19f6416c56..0e4aea7e5e 100644
--- a/saleor/plugins/webhook/tests/test_webhook.py
+++ b/saleor/plugins/webhook/tests/test_webhook.py
@@ -115,6 +115,18 @@ def test_customer_created(mocked_webhook_trigger, settings, customer_user):
)
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")
+def test_customer_updated(mocked_webhook_trigger, settings, customer_user):
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ manager = get_plugins_manager()
+ manager.customer_updated(customer_user)
+
+ expected_data = generate_customer_payload(customer_user)
+ mocked_webhook_trigger.assert_called_once_with(
+ WebhookEventType.CUSTOMER_UPDATED, expected_data
+ )
+
+
@mock.patch("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")
def test_order_fully_paid(mocked_webhook_trigger, settings, order_with_lines):
settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.py
index fc1048c92b..14d649edaf 100644
--- a/saleor/webhook/event_types.py
+++ b/saleor/webhook/event_types.py
@@ -23,6 +23,7 @@ class WebhookEventType:
FULFILLMENT_CREATED = "fulfillment_created"
CUSTOMER_CREATED = "customer_created"
+ CUSTOMER_UPDATED = "customer_updated"
PRODUCT_CREATED = "product_created"
PRODUCT_UPDATED = "product_updated"
@@ -47,6 +48,7 @@ class WebhookEventType:
INVOICE_DELETED: "Invoice deleted",
INVOICE_SENT: "Invoice sent",
CUSTOMER_CREATED: "Customer created",
+ CUSTOMER_UPDATED: "Customer updated",
PRODUCT_CREATED: "Product created",
PRODUCT_UPDATED: "Product updated",
PRODUCT_DELETED: "Product deleted",
@@ -70,6 +72,7 @@ class WebhookEventType:
(INVOICE_DELETED, DISPLAY_LABELS[INVOICE_DELETED]),
(INVOICE_SENT, DISPLAY_LABELS[INVOICE_SENT]),
(CUSTOMER_CREATED, DISPLAY_LABELS[CUSTOMER_CREATED]),
+ (CUSTOMER_UPDATED, DISPLAY_LABELS[CUSTOMER_UPDATED]),
(PRODUCT_CREATED, DISPLAY_LABELS[PRODUCT_CREATED]),
(PRODUCT_UPDATED, DISPLAY_LABELS[PRODUCT_UPDATED]),
(PRODUCT_DELETED, DISPLAY_LABELS[PRODUCT_DELETED]),
@@ -92,6 +95,7 @@ class WebhookEventType:
INVOICE_DELETED: OrderPermissions.MANAGE_ORDERS,
INVOICE_SENT: OrderPermissions.MANAGE_ORDERS,
CUSTOMER_CREATED: AccountPermissions.MANAGE_USERS,
+ CUSTOMER_UPDATED: AccountPermissions.MANAGE_USERS,
PRODUCT_CREATED: ProductPermissions.MANAGE_PRODUCTS,
PRODUCT_UPDATED: ProductPermissions.MANAGE_PRODUCTS,
PRODUCT_DELETED: ProductPermissions.MANAGE_PRODUCTS,
diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.py
index 234cac7f2e..979eee750e 100644
--- a/saleor/webhook/payloads.py
+++ b/saleor/webhook/payloads.py
@@ -196,6 +196,10 @@ def generate_customer_payload(customer: "User"):
lambda c: c.default_shipping_address,
ADDRESS_FIELDS,
),
+ "addresses": (
+ lambda c: c.addresses.all(),
+ ADDRESS_FIELDS,
+ ),
},
)
return data
@@ -369,7 +373,9 @@ def generate_sample_payload(event_name: str) -> Optional[dict]:
WebhookEventType.PAGE_DELETED,
WebhookEventType.PAGE_UPDATED,
]
- if event_name == WebhookEventType.CUSTOMER_CREATED:
+ user_events = [WebhookEventType.CUSTOMER_CREATED, WebhookEventType.CUSTOMER_UPDATED]
+
+ if event_name in user_events:
user = generate_fake_user()
payload = generate_customer_payload(user)
elif event_name == WebhookEventType.PRODUCT_CREATED: