Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add CheckoutCustomerNoteUpdate mutation (#16315)
Peter Adam27b3ef4942f2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f3894c74b1..e42097959d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -11,6 +11,8 @@ All notable, unreleased changes to this project will be documented in this file.### GraphQL API+- Add `CheckoutCustomerNoteUpdate` mutation - #16315 by @pitkes22+### Webhooks### Other changesdiff --git a/saleor/graphql/checkout/mutations/__init__.py b/saleor/graphql/checkout/mutations/__init__.pyindex 218dd238c9..8927927f8d 100644--- a/saleor/graphql/checkout/mutations/__init__.py+++ b/saleor/graphql/checkout/mutations/__init__.py@@ -5,6 +5,7 @@ from .checkout_create import CheckoutCreatefrom .checkout_create_from_order import CheckoutCreateFromOrderfrom .checkout_customer_attach import CheckoutCustomerAttachfrom .checkout_customer_detach import CheckoutCustomerDetach+from .checkout_customer_note_update import CheckoutCustomerNoteUpdatefrom .checkout_delivery_method_update import CheckoutDeliveryMethodUpdatefrom .checkout_email_update import CheckoutEmailUpdatefrom .checkout_language_code_update import CheckoutLanguageCodeUpdate@@ -25,6 +26,7 @@ __all__ = ["CheckoutCreateFromOrder","CheckoutCustomerAttach","CheckoutCustomerDetach",+ "CheckoutCustomerNoteUpdate","CheckoutDeliveryMethodUpdate","CheckoutEmailUpdate","CheckoutLanguageCodeUpdate",diff --git a/saleor/graphql/checkout/mutations/checkout_customer_note_update.py b/saleor/graphql/checkout/mutations/checkout_customer_note_update.pynew file mode 100644index 0000000000..b41da463a5--- /dev/null+++ b/saleor/graphql/checkout/mutations/checkout_customer_note_update.py@@ -0,0 +1,57 @@+import graphene++from ....webhook.event_types import WebhookEventAsyncType+from ...core import ResolveInfo+from ...core.descriptions import ADDED_IN_321+from ...core.doc_category import DOC_CATEGORY_CHECKOUT+from ...core.mutations import BaseMutation+from ...core.types import CheckoutError+from ...core.utils import WebhookEventInfo+from ...plugins.dataloaders import get_plugin_manager_promise+from ..types import Checkout+++class CheckoutCustomerNoteUpdate(BaseMutation):+ checkout = graphene.Field(Checkout, description="An updated checkout.")++ class Arguments:+ id = graphene.ID(+ description="The checkout's ID.",+ required=True,+ )+ customer_note = graphene.String(+ required=True, description="New customer note content."+ )++ class Meta:+ description = (+ "Updates customer note in the existing checkout object." + ADDED_IN_321+ )+ doc_category = DOC_CATEGORY_CHECKOUT+ error_type_class = CheckoutError+ error_type_field = "checkout_errors"+ webhook_events_info = [+ WebhookEventInfo(+ type=WebhookEventAsyncType.CHECKOUT_UPDATED,+ description="A checkout was updated.",+ )+ ]++ @classmethod+ def perform_mutation( # type: ignore[override]+ cls,+ _root,+ info: ResolveInfo,+ /,+ *,+ customer_note,+ id=None,+ ):+ checkout = cls.get_node_or_error(info, id, only_type=Checkout)++ checkout.note = customer_note+ cls.clean_instance(info, checkout)+ checkout.save(update_fields=["note", "last_change"])+ manager = get_plugin_manager_promise(info.context).get()+ cls.call_event(manager.checkout_updated, checkout)+ return CheckoutCustomerNoteUpdate(checkout=checkout)diff --git a/saleor/graphql/checkout/schema.py b/saleor/graphql/checkout/schema.pyindex 8afa13654d..f3d8063fd6 100644--- a/saleor/graphql/checkout/schema.py+++ b/saleor/graphql/checkout/schema.py@@ -26,6 +26,7 @@ from .mutations import (CheckoutCreateFromOrder,CheckoutCustomerAttach,CheckoutCustomerDetach,+ CheckoutCustomerNoteUpdate,CheckoutDeliveryMethodUpdate,CheckoutEmailUpdate,CheckoutLanguageCodeUpdate,@@ -122,6 +123,7 @@ class CheckoutMutations(graphene.ObjectType):checkout_create_from_order = CheckoutCreateFromOrder.Field()checkout_customer_attach = CheckoutCustomerAttach.Field()checkout_customer_detach = CheckoutCustomerDetach.Field()+ checkout_customer_note_update = CheckoutCustomerNoteUpdate.Field()checkout_email_update = CheckoutEmailUpdate.Field()checkout_line_delete = CheckoutLineDelete.Field(deprecation_reason=(diff --git a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.pyindex 6eefbd6eb4..30dbfeb613 100644--- a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py+++ b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py@@ -1794,3 +1794,34 @@ def test_checkout_gift_cards(# thenassert response.status_code == 200++[email protected]_queries(autouse=False)+def test_checkout_customer_note_update(+ api_client, checkout_with_variants, count_queries+):+ query = (+ FRAGMENT_CHECKOUT+ + """+ mutation UpdateCheckoutCustomerNote(+ $id: ID!, $customerNote: String!+ ) {+ checkoutCustomerNoteUpdate(id: $id, customerNote: $customerNote) {+ checkout {+ ...Checkout+ }+ errors {+ field+ message+ }+ }+ }+ """+ )+ variables = {+ "id": to_global_id_or_none(checkout_with_variants),+ "customerNote": "New note text",+ }+ response = get_graphql_content(api_client.post_graphql(query, variables))+ assert not response["data"]["checkoutCustomerNoteUpdate"]["errors"]diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_customer_note_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_customer_note_update.pynew file mode 100644index 0000000000..5edb04a9b2--- /dev/null+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_customer_note_update.py@@ -0,0 +1,68 @@+from ....core.utils import to_global_id_or_none+from ....tests.utils import get_graphql_content++CHECKOUT_CUSTOMER_NOTE_UPDATE_MUTATION = """+ mutation checkoutCustomerNoteUpdate($id: ID!, $customerNote: String!) {+ checkoutCustomerNoteUpdate(id: $id, customerNote: $customerNote) {+ checkout {+ id,+ note+ },+ errors {+ field,+ message+ }+ errors {+ field,+ message+ code+ }+ }+ }+"""+++def test_checkout_customer_note_update(user_api_client, checkout_with_item):+ # given+ checkout = checkout_with_item+ checkout.note = ""+ checkout.save(update_fields=["note"])+ previous_last_change = checkout.last_change++ customer_note = "New customer note value"+ variables = {"id": to_global_id_or_none(checkout), "customerNote": customer_note}++ # when+ response = user_api_client.post_graphql(+ CHECKOUT_CUSTOMER_NOTE_UPDATE_MUTATION, variables+ )++ # then+ content = get_graphql_content(response)+ data = content["data"]["checkoutCustomerNoteUpdate"]+ assert not data["errors"]+ checkout.refresh_from_db()+ assert checkout.note == customer_note+ assert checkout.last_change != previous_last_change+++def test_with_active_problems_flow(api_client, checkout_with_problems):+ # given+ channel = checkout_with_problems.channel+ channel.use_legacy_error_flow_for_checkout = False+ channel.save(update_fields=["use_legacy_error_flow_for_checkout"])++ variables = {+ "id": to_global_id_or_none(checkout_with_problems),+ "customerNote": "New customer note value",+ }++ # when+ response = api_client.post_graphql(+ CHECKOUT_CUSTOMER_NOTE_UPDATE_MUTATION,+ variables,+ )++ # then+ content = get_graphql_content(response)+ assert not content["data"]["checkoutCustomerNoteUpdate"]["errors"]diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex d30962d701..a14524560d 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -19036,6 +19036,22 @@ type Mutation {token: UUID): CheckoutCustomerDetach @doc(category: "Checkout") @webhookEventsInfo(asyncEvents: [CHECKOUT_UPDATED], syncEvents: [])+ """+ Updates customer note in the existing checkout object.++ Added in Saleor 3.21.++ Triggers the following webhook events:+ - CHECKOUT_UPDATED (async): A checkout was updated.+ """+ checkoutCustomerNoteUpdate(+ """New customer note content."""+ customerNote: String!++ """The checkout's ID."""+ id: ID!+ ): CheckoutCustomerNoteUpdate @doc(category: "Checkout") @webhookEventsInfo(asyncEvents: [CHECKOUT_UPDATED], syncEvents: [])+"""Updates email address in the existing checkout object.@@ -29452,6 +29468,21 @@ type CheckoutCustomerDetach @doc(category: "Checkout") @webhookEventsInfo(asyncEerrors: [CheckoutError!]!}+"""+Updates customer note in the existing checkout object.++Added in Saleor 3.21.++Triggers the following webhook events:+- CHECKOUT_UPDATED (async): A checkout was updated.+"""+type CheckoutCustomerNoteUpdate @doc(category: "Checkout") @webhookEventsInfo(asyncEvents: [CHECKOUT_UPDATED], syncEvents: []) {+ """An updated checkout."""+ checkout: Checkout+ checkoutErrors: [CheckoutError!]! @deprecated(reason: "This field will be removed in Saleor 4.0. Use `errors` field instead.")+ errors: [CheckoutError!]!+}+"""Updates email address in the existing checkout object.