Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add `FULFILLMENT_TRACKING_NUMBER_UPDATED` webhook (#13708)
Artur9258154b8a0b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f624bcda23..3719e46fae 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -26,6 +26,9 @@ in 3.17. Use `PaymentSettings.defaultTransactionFlowStrategy` instead.- Add `GIFT_CARD_EXPORT_COMPLETED` webhook - #13765, by @Air-t- Event sent when CSV export is completed.+- Add `FULFILLMENT_TRACKING_NUMBER_UPDATED` webhook - #13708, by @Air-t+ - Called after `fulfillmentUpdateTracking` or `orderFulfill` mutation if tracking number is updated.+### Other changes- Fix error in variant available stock calculation - 13593 by @awaisdar001diff --git a/saleor/graphql/order/mutations/fulfillment_update_tracking.py b/saleor/graphql/order/mutations/fulfillment_update_tracking.pyindex 795e712e4e..21cb2f27ba 100644--- a/saleor/graphql/order/mutations/fulfillment_update_tracking.py+++ b/saleor/graphql/order/mutations/fulfillment_update_tracking.py@@ -6,11 +6,13 @@ from ....account.models import Userfrom ....order.actions import fulfillment_tracking_updatedfrom ....order.notifications import send_fulfillment_updatefrom ....permission.enums import OrderPermissions+from ....webhook.event_types import WebhookEventAsyncTypefrom ...app.dataloaders import get_app_promisefrom ...core import ResolveInfofrom ...core.doc_category import DOC_CATEGORY_ORDERSfrom ...core.mutations import BaseMutationfrom ...core.types import OrderError+from ...core.utils import WebhookEventInfofrom ...plugins.dataloaders import get_plugin_manager_promisefrom ..types import Fulfillment, Orderfrom .order_fulfill import FulfillmentUpdateTrackingInput@@ -36,6 +38,12 @@ class FulfillmentUpdateTracking(BaseMutation):permissions = (OrderPermissions.MANAGE_ORDERS,)error_type_class = OrderErrorerror_type_field = "order_errors"+ webhook_events_info = [+ WebhookEventInfo(+ type=WebhookEventAsyncType.FULFILLMENT_TRACKING_NUMBER_UPDATED,+ description="Fulfillment tracking number is updated.",+ )+ ]@classmethoddef perform_mutation( # type: ignore[override]diff --git a/saleor/graphql/order/mutations/order_fulfill.py b/saleor/graphql/order/mutations/order_fulfill.pyindex a5e16f9eb5..a365e8a424 100644--- a/saleor/graphql/order/mutations/order_fulfill.py+++ b/saleor/graphql/order/mutations/order_fulfill.py@@ -115,6 +115,10 @@ class OrderFulfill(BaseMutation):type=WebhookEventAsyncType.ORDER_FULFILLED,description="Order is fulfilled.",),+ WebhookEventInfo(+ type=WebhookEventAsyncType.FULFILLMENT_TRACKING_NUMBER_UPDATED,+ description="Sent when fulfillment tracking number is updated.",+ ),WebhookEventInfo(type=WebhookEventAsyncType.FULFILLMENT_APPROVED,description="A fulfillment is approved.",diff --git a/saleor/graphql/order/tests/mutations/test_fulfillment_update_tracking.py b/saleor/graphql/order/tests/mutations/test_fulfillment_update_tracking.pyindex 04cf401e3b..192a0e66fe 100644--- a/saleor/graphql/order/tests/mutations/test_fulfillment_update_tracking.py+++ b/saleor/graphql/order/tests/mutations/test_fulfillment_update_tracking.py@@ -1,7 +1,9 @@from unittest.mock import ANY, patchimport graphene+import pytest+from .....webhook.event_types import WebhookEventAsyncTypefrom ....tests.utils import assert_no_permission, get_graphql_contentFULFILLMENT_UPDATE_TRACKING_QUERY = """@@ -90,11 +92,13 @@ def test_fulfillment_update_tracking_by_app(send_fulfillment_update_mock.assert_not_called()+@patch("saleor.plugins.manager.PluginsManager.tracking_number_updated")@patch("saleor.graphql.order.mutations.fulfillment_update_tracking.send_fulfillment_update")def test_fulfillment_update_tracking_send_notification_true(send_fulfillment_update_mock,+ mocked_tracking_number_updated_event,staff_api_client,fulfillment,permission_group_manage_orders,@@ -113,11 +117,14 @@ def test_fulfillment_update_tracking_send_notification_true(send_fulfillment_update_mock.assert_called_once_with(fulfillment.order, fulfillment, ANY)+ mocked_tracking_number_updated_event.assert_called_once_with(fulfillment)+@patch("saleor.plugins.manager.PluginsManager.tracking_number_updated")@patch("saleor.order.notifications.send_fulfillment_update")def test_fulfillment_update_tracking_send_notification_false(send_fulfillment_update_mock,+ mocked_tracking_number_updated_event,staff_api_client,fulfillment,permission_group_manage_orders,@@ -134,3 +141,35 @@ def test_fulfillment_update_tracking_send_notification_false(data = content["data"]["orderFulfillmentUpdateTracking"]["fulfillment"]assert data["trackingNumber"] == trackingsend_fulfillment_update_mock.assert_not_called()+ mocked_tracking_number_updated_event.assert_called_once_with(fulfillment)++[email protected]("notify_customer", [True, False])+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")+def test_fulfillment_tracking_number_updated_event_triggered(+ mocked_webhooks,+ notify_customer,+ permission_group_manage_orders,+ fulfillment,+ settings,+ subscription_fulfillment_tracking_number_updated,+ staff_api_client,+):+ # given+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ permission_group_manage_orders.user_set.add(staff_api_client.user)+ fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.id)+ variables = {+ "id": fulfillment_id,+ "tracking": "tracking",+ "notifyCustomer": notify_customer,+ }++ # when+ staff_api_client.post_graphql(FULFILLMENT_UPDATE_TRACKING_QUERY, variables)++ # then+ mocked_webhooks.assert_called_once()+ assert mocked_webhooks.call_args[0][1] == (+ WebhookEventAsyncType.FULFILLMENT_TRACKING_NUMBER_UPDATED+ )diff --git a/saleor/graphql/order/tests/mutations/test_order_fulfill.py b/saleor/graphql/order/tests/mutations/test_order_fulfill.pyindex fbc14e3f67..753fba9783 100644--- a/saleor/graphql/order/tests/mutations/test_order_fulfill.py+++ b/saleor/graphql/order/tests/mutations/test_order_fulfill.py@@ -1,4 +1,4 @@-from unittest.mock import ANY, MagicMock, patch+from unittest.mock import ANY, patchimport grapheneimport pytest@@ -73,9 +73,11 @@ def test_order_fulfill_with_out_of_stock_webhook(@pytest.mark.parametrize("fulfillment_auto_approve", [True, False])+@patch("saleor.plugins.manager.PluginsManager.tracking_number_updated")@patch("saleor.graphql.order.mutations.order_fulfill.create_fulfillments")def test_order_fulfill(mock_create_fulfillments,+ mocked_fulfillment_tracking_number_updated_event,fulfillment_auto_approve,staff_api_client,staff_user,@@ -133,6 +135,7 @@ def test_order_fulfill(approved=fulfillment_auto_approve,tracking_number="",)+ mocked_fulfillment_tracking_number_updated_event.assert_not_called()def test_order_fulfill_no_channel_access(@@ -1487,12 +1490,11 @@ def test_create_digital_fulfillment(assert mock_email_fulfillment.call_count == 1-@patch("saleor.core.utils.events.transaction")@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")-def test_order_fulfill_fulfillment_created_event_triggered(+def test_order_fulfill_tracking_number_updated_event_triggered(mocked_webhooks,- mocked_transactions,any_webhook,+ subscription_fulfillment_tracking_number_updated,subscription_fulfillment_created_webhook,staff_api_client,staff_user,@@ -1503,7 +1505,6 @@ def test_order_fulfill_fulfillment_created_event_triggered(settings,):# given- mocked_transactions.get_connection.return_value = MagicMock(in_atomic_block=False)settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]permission_group_manage_orders.user_set.add(staff_api_client.user)site_settings.fulfillment_auto_approve = True@@ -1534,9 +1535,15 @@ def test_order_fulfill_fulfillment_created_event_triggered(}# whenstaff_api_client.post_graphql(query, variables)+ flush_post_commit_hooks()# then- mocked_webhooks.assert_called_once()- assert mocked_webhooks.call_args[0][1] == (- WebhookEventAsyncType.FULFILLMENT_CREATED+ assert mocked_webhooks.call_count == 2+ mocked_tracking_updated = mocked_webhooks.call_args_list[0]+ mocked_fulfillment_created = mocked_webhooks.call_args_list[1]++ assert (+ mocked_tracking_updated[0][1]+ == WebhookEventAsyncType.FULFILLMENT_TRACKING_NUMBER_UPDATED)+ assert mocked_fulfillment_created[0][1] == WebhookEventAsyncType.FULFILLMENT_CREATEDdiff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 69e78aa910..4e1bfceb5f 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1939,6 +1939,7 @@ enum WebhookEventTypeEnum @doc(category: "Webhooks") {Added in Saleor 3.8."""FULFILLMENT_METADATA_UPDATED+ FULFILLMENT_TRACKING_NUMBER_UPDATED"""A draft order is created."""DRAFT_ORDER_CREATED@@ -2612,6 +2613,7 @@ enum WebhookEventTypeAsyncEnum @doc(category: "Webhooks") {Added in Saleor 3.8."""FULFILLMENT_METADATA_UPDATED+ FULFILLMENT_TRACKING_NUMBER_UPDATED"""A draft order is created."""DRAFT_ORDER_CREATED@@ -3477,6 +3479,7 @@ enum WebhookSampleEventTypeEnum @doc(category: "Webhooks") {FULFILLMENT_CANCELEDFULFILLMENT_APPROVEDFULFILLMENT_METADATA_UPDATED+ FULFILLMENT_TRACKING_NUMBER_UPDATEDDRAFT_ORDER_CREATEDDRAFT_ORDER_UPDATEDDRAFT_ORDER_DELETED@@ -15935,6 +15938,7 @@ type Mutation {Triggers the following webhook events:- FULFILLMENT_CREATED (async): A new fulfillment is created.- ORDER_FULFILLED (async): Order is fulfilled.+ - FULFILLMENT_TRACKING_NUMBER_UPDATED (async): Sent when fulfillment tracking number is updated.- FULFILLMENT_APPROVED (async): A fulfillment is approved."""orderFulfill(@@ -15943,7 +15947,7 @@ type Mutation {"""ID of the order to be fulfilled."""order: ID- ): OrderFulfill @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_CREATED, ORDER_FULFILLED, FULFILLMENT_APPROVED], syncEvents: [])+ ): OrderFulfill @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_CREATED, ORDER_FULFILLED, FULFILLMENT_TRACKING_NUMBER_UPDATED, FULFILLMENT_APPROVED], syncEvents: [])"""Cancels existing fulfillment and optionally restocks items.@@ -15983,6 +15987,9 @@ type Mutation {Updates a fulfillment for an order.Requires one of the following permissions: MANAGE_ORDERS.++ Triggers the following webhook events:+ - FULFILLMENT_TRACKING_NUMBER_UPDATED (async): Fulfillment tracking number is updated."""orderFulfillmentUpdateTracking("""ID of a fulfillment to update."""@@ -15990,7 +15997,7 @@ type Mutation {"""Fields required to update a fulfillment."""input: FulfillmentUpdateTrackingInput!- ): FulfillmentUpdateTracking @doc(category: "Orders")+ ): FulfillmentUpdateTracking @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_TRACKING_NUMBER_UPDATED], syncEvents: [])"""Refund products.@@ -23808,9 +23815,10 @@ Requires one of the following permissions: MANAGE_ORDERS.Triggers the following webhook events:- FULFILLMENT_CREATED (async): A new fulfillment is created.- ORDER_FULFILLED (async): Order is fulfilled.+- FULFILLMENT_TRACKING_NUMBER_UPDATED (async): Sent when fulfillment tracking number is updated.- FULFILLMENT_APPROVED (async): A fulfillment is approved."""-type OrderFulfill @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_CREATED, ORDER_FULFILLED, FULFILLMENT_APPROVED], syncEvents: []) {+type OrderFulfill @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_CREATED, ORDER_FULFILLED, FULFILLMENT_TRACKING_NUMBER_UPDATED, FULFILLMENT_APPROVED], syncEvents: []) {"""List of created fulfillments."""fulfillments: [Fulfillment!]@@ -23900,8 +23908,11 @@ type FulfillmentApprove @doc(category: "Orders") @webhookEventsInfo(asyncEvents:Updates a fulfillment for an order.Requires one of the following permissions: MANAGE_ORDERS.++Triggers the following webhook events:+- FULFILLMENT_TRACKING_NUMBER_UPDATED (async): Fulfillment tracking number is updated."""-type FulfillmentUpdateTracking @doc(category: "Orders") {+type FulfillmentUpdateTracking @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_TRACKING_NUMBER_UPDATED], syncEvents: []) {"""A fulfillment with updated tracking."""fulfillment: Fulfillment@@ -31530,6 +31541,31 @@ type FulfillmentCreated implements Event @doc(category: "Orders") {notifyCustomer: Boolean!}+"""+Event sent when the tracking number is updated.++Added in Saleor 3.16.+"""+type FulfillmentTrackingNumberUpdated implements Event @doc(category: "Orders") {+ """Time of the event."""+ issuedAt: DateTime++ """Saleor version that triggered the event."""+ version: String++ """The user or application that triggered the event."""+ issuingPrincipal: IssuingPrincipal++ """The application receiving the webhook."""+ recipient: App++ """The fulfillment the event relates to."""+ fulfillment: Fulfillment++ """The order the fulfillment belongs to."""+ order: Order+}+"""Event sent when fulfillment is canceled.diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.pyindex ea58550e2f..8fc7c14a4b 100644--- a/saleor/graphql/webhook/subscription_types.py+++ b/saleor/graphql/webhook/subscription_types.py@@ -1181,6 +1181,15 @@ class FulfillmentBase(AbstractType):return fulfillment.order+class FulfillmentTrackingNumberUpdated(SubscriptionObjectType, FulfillmentBase):+ class Meta:+ doc_category = DOC_CATEGORY_ORDERS+ root_type = "Fulfillment"+ enable_dry_run = True+ interfaces = (Event,)+ description = "Event sent when the tracking number is updated." + ADDED_IN_316++class FulfillmentCreated(SubscriptionObjectType, FulfillmentBase):notify_customer = graphene.Boolean(description=(@@ -2429,6 +2438,7 @@ WEBHOOK_TYPES_MAP = {WebhookEventAsyncType.INVOICE_DELETED: InvoiceDeleted,WebhookEventAsyncType.INVOICE_SENT: InvoiceSent,WebhookEventAsyncType.FULFILLMENT_CREATED: FulfillmentCreated,+ WebhookEventAsyncType.FULFILLMENT_TRACKING_NUMBER_UPDATED: FulfillmentTrackingNumberUpdated, # noqa: E501WebhookEventAsyncType.FULFILLMENT_CANCELED: FulfillmentCanceled,WebhookEventAsyncType.FULFILLMENT_APPROVED: FulfillmentApproved,WebhookEventAsyncType.FULFILLMENT_METADATA_UPDATED: FulfillmentMetadataUpdated,diff --git a/saleor/graphql/webhook/tests/mutations/test_webhook_dry_run.py b/saleor/graphql/webhook/tests/mutations/test_webhook_dry_run.pyindex 4f522c2e98..651ad07f08 100644--- a/saleor/graphql/webhook/tests/mutations/test_webhook_dry_run.py+++ b/saleor/graphql/webhook/tests/mutations/test_webhook_dry_run.py@@ -275,6 +275,7 @@ def async_subscription_webhooks_with_root_objects(subscription_invoice_sent_webhook,subscription_fulfillment_canceled_webhook,subscription_fulfillment_metadata_updated_webhook,+ subscription_fulfillment_tracking_number_updated,subscription_customer_created_webhook,subscription_customer_updated_webhook,subscription_customer_deleted_webhook,@@ -480,6 +481,10 @@ def async_subscription_webhooks_with_root_objects(subscription_fulfillment_metadata_updated_webhook,fulfillment,],+ events.FULFILLMENT_TRACKING_NUMBER_UPDATED: [+ subscription_fulfillment_tracking_number_updated,+ fulfillment,+ ],events.CUSTOMER_CREATED: [subscription_customer_created_webhook, customer_user],events.CUSTOMER_UPDATED: [subscription_customer_updated_webhook, customer_user],events.CUSTOMER_METADATA_UPDATED: [diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.pyindex c7a72464f0..8db0f7599e 100644--- a/saleor/plugins/webhook/plugin.py+++ b/saleor/plugins/webhook/plugin.py@@ -1029,6 +1029,16 @@ class WebhookPlugin(BasePlugin):WebhookEventAsyncType.FULFILLMENT_METADATA_UPDATED, fulfillment)+ def tracking_number_updated(self, fulfillment: "Fulfillment", previous_value):+ if not self.active:+ return previous_value+ event_type = WebhookEventAsyncType.FULFILLMENT_TRACKING_NUMBER_UPDATED+ if webhooks := get_webhooks_for_event(event_type):+ fulfillment_data = generate_fulfillment_payload(fulfillment, self.requestor)+ trigger_webhooks_async(+ fulfillment_data, event_type, webhooks, fulfillment, self.requestor+ )+def customer_created(self, customer: "User", previous_value: Any) -> Any:if not self.active:return previous_valuediff --git a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.pyindex b2da3eef1f..7be3b338ba 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py@@ -657,6 +657,14 @@ def subscription_fulfillment_metadata_updated_webhook(subscription_webhook):)+def subscription_fulfillment_tracking_number_updated(subscription_webhook):+ return subscription_webhook(+ queries.FULFILLMENT_TRACKING_NUMBER_UPDATED,+ WebhookEventAsyncType.FULFILLMENT_TRACKING_NUMBER_UPDATED,+ )++@pytest.fixturedef subscription_customer_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.pyindex ec4e7025d7..42a461185f 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py@@ -1426,6 +1426,24 @@ FULFILLMENT_METADATA_UPDATED = (""")+FULFILLMENT_TRACKING_NUMBER_UPDATED = (+ fragments.FULFILLMENT_DETAILS+ + """+ subscription{+ event{+ ...on FulfillmentTrackingNumberUpdated{+ fulfillment{+ ...FulfillmentDetails+ }+ order{+ id+ }+ }+ }+ }+"""+)+CUSTOMER_CREATED = (fragments.CUSTOMER_DETAILSdiff --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.pyindex 89c73df368..6d5078f00e 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@@ -1756,6 +1756,23 @@ def test_fulfillment_metadata_updated(assert deliveries[0].webhook == webhooks[0]+def test_fulfillment_tracking_number_updated(+ fulfillment, subscription_fulfillment_tracking_number_updated+):+ # given+ webhooks = [subscription_fulfillment_tracking_number_updated]+ event_type = WebhookEventAsyncType.FULFILLMENT_TRACKING_NUMBER_UPDATED+ expected_payload = generate_fulfillment_payload(fulfillment)++ # when+ deliveries = create_deliveries_for_subscriptions(event_type, fulfillment, webhooks)++ # then+ assert deliveries[0].payload.payload == json.dumps(expected_payload)+ assert len(deliveries) == len(webhooks)+ assert deliveries[0].webhook == webhooks[0]++def test_customer_created(customer_user, subscription_customer_created_webhook):# givenwebhooks = [subscription_customer_created_webhook]diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.pyindex 2c82a03235..e87f7b7113 100644--- a/saleor/webhook/event_types.py+++ b/saleor/webhook/event_types.py@@ -89,6 +89,7 @@ class WebhookEventAsyncType:FULFILLMENT_CANCELED = "fulfillment_canceled"FULFILLMENT_APPROVED = "fulfillment_approved"FULFILLMENT_METADATA_UPDATED = "fulfillment_metadata_updated"+ FULFILLMENT_TRACKING_NUMBER_UPDATED = "fulfillment_tracking_number_updated"DRAFT_ORDER_CREATED = "draft_order_created"DRAFT_ORDER_UPDATED = "draft_order_updated"@@ -414,6 +415,10 @@ class WebhookEventAsyncType:"name": "Fulfillment metadata updated","permission": OrderPermissions.MANAGE_ORDERS,},+ FULFILLMENT_TRACKING_NUMBER_UPDATED: {+ "name": "Fulfillment tracking number updated.",+ "permission": OrderPermissions.MANAGE_ORDERS,+ },DRAFT_ORDER_CREATED: {"name": "Draft order created","permission": OrderPermissions.MANAGE_ORDERS,