Skip to content

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

Trigger checkout_updated for Checkout meta mutations

Marcin Gębala1768b2494476
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ebf63a3c51..3f95619fbb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -99,6 +99,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Copy metadata fields when creating reissue - #7358 by @IKarbowiak
- Fix invoice generation - #7376 by @tomaszszymanski129
- Allow defining only one field in translations - #7363 by @IKarbowiak
+- Trigger `checkout_updated` hook for checkout meta mutations - #7392 by @maarcingebala
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --git a/saleor/graphql/meta/extra_methods.py b/saleor/graphql/meta/extra_methods.py
index 84876954f7..da1325543e 100644
--- a/saleor/graphql/meta/extra_methods.py
+++ b/saleor/graphql/meta/extra_methods.py
@@ -1,6 +1,10 @@
from ...product.models import Product, ProductVariant
+def extra_checkout_actions(instance, info, **data):
+ info.context.plugins.checkout_updated(instance)
+
+
def extra_product_actions(instance, info, **data):
info.context.plugins.product_updated(instance)
@@ -14,6 +18,7 @@ def extra_user_actions(instance, info, **data):
MODEL_EXTRA_METHODS = {
+ "Checkout": extra_checkout_actions,
"Product": extra_product_actions,
"ProductVariant": extra_variant_actions,
"User": extra_user_actions,
diff --git a/saleor/graphql/meta/mutations.py b/saleor/graphql/meta/mutations.py
index a29eae4215..fd382141ba 100644
--- a/saleor/graphql/meta/mutations.py
+++ b/saleor/graphql/meta/mutations.py
@@ -106,9 +106,12 @@ class BaseMetadataMutation(BaseMutation):
return cls.handle_errors(e)
if not cls.check_permissions(info.context, permissions):
raise PermissionDenied()
- result = super().mutate(root, info, **data)
- if not result.errors:
- cls.perform_model_extra_actions(root, info, **data)
+ try:
+ result = super().mutate(root, info, **data)
+ if not result.errors:
+ cls.perform_model_extra_actions(root, info, **data)
+ except ValidationError as e:
+ return cls.handle_errors(e)
return result
@classmethod
diff --git a/saleor/graphql/meta/tests/test_meta_mutations.py b/saleor/graphql/meta/tests/test_meta_mutations.py
index 03929c65de..2643a6ad79 100644
--- a/saleor/graphql/meta/tests/test_meta_mutations.py
+++ b/saleor/graphql/meta/tests/test_meta_mutations.py
@@ -4,6 +4,7 @@ from unittest.mock import patch
import graphene
import pytest
+from django.core.exceptions import ValidationError
from ....core.error_codes import MetadataErrorCode
from ....core.models import ModelWithMetadata
@@ -123,6 +124,22 @@ def item_contains_multiple_proper_public_metadata(
)
+@patch("saleor.plugins.manager.PluginsManager.checkout_updated")
+def test_base_metadata_mutation_handles_errors_from_extra_action(
+ mock_checkout_updated, api_client, checkout
+):
+ error_field = "field"
+ error_msg = "boom"
+ mock_checkout_updated.side_effect = ValidationError({error_field: error_msg})
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ response = execute_update_public_metadata_for_item(
+ api_client, None, checkout_id, "Checkout"
+ )
+ errors = response["data"]["updateMetadata"]["errors"]
+ assert errors[0]["field"] == error_field
+ assert errors[0]["message"] == error_msg
+
+
def test_add_public_metadata_for_customer_as_staff(
staff_api_client, permission_manage_users, customer_user
):
@@ -323,6 +340,18 @@ def test_add_public_metadata_for_checkout(api_client, checkout):
)
+@patch("saleor.plugins.manager.PluginsManager.checkout_updated")
+def test_add_metadata_for_checkout_triggers_checkout_updated_hook(
+ mock_checkout_updated, api_client, checkout
+):
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ response = execute_update_public_metadata_for_item(
+ api_client, None, checkout_id, "Checkout"
+ )
+ assert response["data"]["updateMetadata"]["errors"] == []
+ mock_checkout_updated.assert_called_once_with(checkout)
+
+
def test_add_public_metadata_for_order(api_client, order):
# given
order_id = graphene.Node.to_global_id("Order", order.pk)