Skip to content

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

Add `notify_customer` flag to `FulfillmentApproved` type (#13637)

Artur386475847032
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d2e73638e1..15f5f4aa72 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,8 @@ in 3.17. Use `PaymentSettings.defaultTransactionFlowStrategy` instead.
- Add `STORED_PAYMENT_METHOD_DELETE_REQUESTED` webhook event - #13660 by @korycins
- Add `NOTIFY_CUSTOMER` flag to `FulfillmentCreated` type - #13620, by @Air-t
- Inform apps if customer should be notified when fulfillment is created.
+- Add `NOTIFY_CUSTOMER` flag to `FulfillmentApproved` type - #13637, by @Air-t
+ - Inform apps if customer should be notified when fulfillment is approved.
### Other changes
- Fix error in variant available stock calculation - 13593 by @awaisdar001
diff --git a/saleor/graphql/order/mutations/fulfillment_approve.py b/saleor/graphql/order/mutations/fulfillment_approve.py
index 417205ee43..728b1637c8 100644
--- a/saleor/graphql/order/mutations/fulfillment_approve.py
+++ b/saleor/graphql/order/mutations/fulfillment_approve.py
@@ -9,12 +9,14 @@ from ....order import FulfillmentStatus
from ....order.actions import approve_fulfillment
from ....order.error_codes import OrderErrorCode
from ....permission.enums import OrderPermissions
+from ....webhook.event_types import WebhookEventAsyncType
from ...app.dataloaders import get_app_promise
from ...core import ResolveInfo
from ...core.descriptions import ADDED_IN_31
from ...core.doc_category import DOC_CATEGORY_ORDERS
from ...core.mutations import BaseMutation
from ...core.types import OrderError
+from ...core.utils import WebhookEventInfo
from ...plugins.dataloaders import get_plugin_manager_promise
from ...site.dataloaders import get_site_promise
from ..types import Fulfillment, Order
@@ -41,6 +43,12 @@ class FulfillmentApprove(BaseMutation):
permissions = (OrderPermissions.MANAGE_ORDERS,)
error_type_class = OrderError
error_type_field = "order_errors"
+ webhook_events_info = [
+ WebhookEventInfo(
+ type=WebhookEventAsyncType.FULFILLMENT_APPROVED,
+ description="Fulfillment is approved.",
+ ),
+ ]
@classmethod
def clean_input(cls, info: ResolveInfo, fulfillment):
diff --git a/saleor/graphql/order/mutations/order_fulfill.py b/saleor/graphql/order/mutations/order_fulfill.py
index fce0e7939f..a5e16f9eb5 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_APPROVED,
+ description="A fulfillment is approved.",
+ ),
]
@classmethod
diff --git a/saleor/graphql/order/tests/mutations/test_fulfillment_approve.py b/saleor/graphql/order/tests/mutations/test_fulfillment_approve.py
index 2fedf9a4de..90fca363a6 100644
--- a/saleor/graphql/order/tests/mutations/test_fulfillment_approve.py
+++ b/saleor/graphql/order/tests/mutations/test_fulfillment_approve.py
@@ -10,6 +10,7 @@ from .....order.fetch import OrderLineInfo
from .....order.models import OrderLine
from .....plugins.manager import get_plugins_manager
from .....product.models import Product
+from .....webhook.event_types import WebhookEventAsyncType
from ....tests.utils import assert_no_permission, get_graphql_content
APPROVE_FULFILLMENT_MUTATION = """
@@ -67,7 +68,7 @@ def test_fulfillment_approve(
event = events[0]
assert event.type == OrderEvents.FULFILLMENT_FULFILLED_ITEMS
assert event.user == staff_api_client.user
- mock_fulfillment_approved.assert_called_once_with(fulfillment)
+ mock_fulfillment_approved.assert_called_once_with(fulfillment, True)
def test_fulfillment_approve_by_user_no_channel_access(
@@ -133,7 +134,7 @@ def test_fulfillment_approve_by_app(
assert event.type == OrderEvents.FULFILLMENT_FULFILLED_ITEMS
assert event.app == app_api_client.app
assert event.user is None
- mock_fulfillment_approved.assert_called_once_with(fulfillment)
+ mock_fulfillment_approved.assert_called_once_with(fulfillment, True)
@patch("saleor.order.actions.send_fulfillment_confirmation_to_customer", autospec=True)
@@ -433,7 +434,9 @@ def test_fulfillment_approve_partial_order_fulfill(
assert fulfillment_awaiting_approval.status == FulfillmentStatus.FULFILLED
assert mock_email_fulfillment.call_count == 0
- mock_fulfillment_approved.assert_called_once_with(fulfillment_awaiting_approval)
+ mock_fulfillment_approved.assert_called_once_with(
+ fulfillment_awaiting_approval, False
+ )
def test_fulfillment_approve_invalid_status(
@@ -498,3 +501,35 @@ def test_fulfillment_approve_preorder(
error = data["errors"][0]
assert error["field"] == "orderLineId"
assert error["code"] == OrderErrorCode.FULFILL_ORDER_LINE.name
+
+
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_fulfillment_approve_trigger_webhook_event(
+ mocked_trigger_async,
+ staff_api_client,
+ fulfillment,
+ permission_group_manage_orders,
+ settings,
+ subscription_fulfillment_approved_webhook,
+):
+ # given
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ permission_group_manage_orders.user_set.add(staff_api_client.user)
+ fulfillment.status = FulfillmentStatus.WAITING_FOR_APPROVAL
+ fulfillment.save(update_fields=["status"])
+ query = APPROVE_FULFILLMENT_MUTATION
+ fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.id)
+ variables = {"id": fulfillment_id, "notifyCustomer": True}
+
+ # when
+ staff_api_client.post_graphql(query, variables)
+
+ # then
+ mocked_trigger_async.assert_called_once()
+ assert mocked_trigger_async.call_args[0][1] == (
+ WebhookEventAsyncType.FULFILLMENT_APPROVED
+ )
+ assert mocked_trigger_async.call_args[0][3] == {
+ "fulfillment": fulfillment,
+ "notify_customer": True,
+ }
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 142c461b75..8ca724c393 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -15913,6 +15913,7 @@ type Mutation {
Triggers the following webhook events:
- FULFILLMENT_CREATED (async): A new fulfillment is created.
- ORDER_FULFILLED (async): Order is fulfilled.
+ - FULFILLMENT_APPROVED (async): A fulfillment is approved.
"""
orderFulfill(
"""Fields required to create a fulfillment."""
@@ -15920,7 +15921,7 @@ type Mutation {
"""ID of the order to be fulfilled."""
order: ID
- ): OrderFulfill @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_CREATED, ORDER_FULFILLED], syncEvents: [])
+ ): OrderFulfill @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_CREATED, ORDER_FULFILLED, FULFILLMENT_APPROVED], syncEvents: [])
"""
Cancels existing fulfillment and optionally restocks items.
@@ -15941,6 +15942,9 @@ type Mutation {
Added in Saleor 3.1.
Requires one of the following permissions: MANAGE_ORDERS.
+
+ Triggers the following webhook events:
+ - FULFILLMENT_APPROVED (async): Fulfillment is approved.
"""
orderFulfillmentApprove(
"""True if stock could be exceeded."""
@@ -15951,7 +15955,7 @@ type Mutation {
"""True if confirmation email should be send."""
notifyCustomer: Boolean!
- ): FulfillmentApprove @doc(category: "Orders")
+ ): FulfillmentApprove @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_APPROVED], syncEvents: [])
"""
Updates a fulfillment for an order.
@@ -23781,8 +23785,9 @@ 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_APPROVED (async): A fulfillment is approved.
"""
-type OrderFulfill @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_CREATED, ORDER_FULFILLED], syncEvents: []) {
+type OrderFulfill @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_CREATED, ORDER_FULFILLED, FULFILLMENT_APPROVED], syncEvents: []) {
"""List of created fulfillments."""
fulfillments: [Fulfillment!]
@@ -23854,8 +23859,11 @@ Approve existing fulfillment.
Added in Saleor 3.1.
Requires one of the following permissions: MANAGE_ORDERS.
+
+Triggers the following webhook events:
+- FULFILLMENT_APPROVED (async): Fulfillment is approved.
"""
-type FulfillmentApprove @doc(category: "Orders") {
+type FulfillmentApprove @doc(category: "Orders") @webhookEventsInfo(asyncEvents: [FULFILLMENT_APPROVED], syncEvents: []) {
"""An approved fulfillment."""
fulfillment: Fulfillment
@@ -31469,7 +31477,7 @@ type FulfillmentCreated implements Event @doc(category: "Orders") {
order: Order
"""
- If true, send an email notification to the customer.
+ If true, the app should send a notification to the customer.
Added in Saleor 3.16.
"""
@@ -31524,6 +31532,13 @@ type FulfillmentApproved implements Event @doc(category: "Orders") {
"""The order the fulfillment belongs to."""
order: Order
+
+ """
+ If true, send a notification to the customer.
+
+ Added in Saleor 3.16.
+ """
+ notifyCustomer: Boolean!
}
"""
diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.py
index e1412c9fe6..d822363de9 100644
--- a/saleor/graphql/webhook/subscription_types.py
+++ b/saleor/graphql/webhook/subscription_types.py
@@ -1165,7 +1165,8 @@ class FulfillmentBase(AbstractType):
class FulfillmentCreated(SubscriptionObjectType, FulfillmentBase):
notify_customer = graphene.Boolean(
description=(
- "If true, send an email notification to the customer." + ADDED_IN_316
+ "If true, the app should send a notification to the customer."
+ + ADDED_IN_316
),
required=True,
)
@@ -1202,12 +1203,33 @@ class FulfillmentCanceled(SubscriptionObjectType, FulfillmentBase):
class FulfillmentApproved(SubscriptionObjectType, FulfillmentBase):
+ notify_customer = graphene.Boolean(
+ description="If true, send a notification to the customer." + ADDED_IN_316,
+ required=True,
+ )
+
class Meta:
+ doc_category = DOC_CATEGORY_ORDERS
root_type = "Fulfillment"
- enable_dry_run = True
+ enable_dry_run = False
interfaces = (Event,)
description = "Event sent when fulfillment is approved." + ADDED_IN_37
+ @staticmethod
+ def resolve_fulfillment(root, info: ResolveInfo):
+ _, data = root
+ return data["fulfillment"]
+
+ @staticmethod
+ def resolve_order(root, info: ResolveInfo):
+ _, data = root
+ return data["fulfillment"].order
+
+ @staticmethod
+ def resolve_notify_customer(root, _info: ResolveInfo):
+ _, data = root
+ return data["notify_customer"]
+
class FulfillmentMetadataUpdated(SubscriptionObjectType, FulfillmentBase):
class Meta:
diff --git a/saleor/graphql/webhook/tests/mutations/test_webhook_dry_run.py b/saleor/graphql/webhook/tests/mutations/test_webhook_dry_run.py
index 70611dcda9..dc09a2169a 100644
--- a/saleor/graphql/webhook/tests/mutations/test_webhook_dry_run.py
+++ b/saleor/graphql/webhook/tests/mutations/test_webhook_dry_run.py
@@ -273,7 +273,6 @@ def async_subscription_webhooks_with_root_objects(
subscription_invoice_deleted_webhook,
subscription_invoice_sent_webhook,
subscription_fulfillment_canceled_webhook,
- subscription_fulfillment_approved_webhook,
subscription_fulfillment_metadata_updated_webhook,
subscription_customer_created_webhook,
subscription_customer_updated_webhook,
@@ -471,10 +470,6 @@ def async_subscription_webhooks_with_root_objects(
subscription_fulfillment_canceled_webhook,
fulfillment,
],
- events.FULFILLMENT_APPROVED: [
- subscription_fulfillment_approved_webhook,
- fulfillment,
- ],
events.FULFILLMENT_METADATA_UPDATED: [
subscription_fulfillment_metadata_updated_webhook,
fulfillment,
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index fff9a2f46a..9a87c3f261 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -563,7 +563,7 @@ def approve_fulfillment(
update_order_status(order)
call_event(manager.order_updated, order)
- call_event(manager.fulfillment_approved, fulfillment)
+ call_event(manager.fulfillment_approved, fulfillment, notify_customer)
if order.status == OrderStatus.FULFILLED:
call_event(manager.order_fulfilled, order)
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 8494f6c780..f329562456 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -875,13 +875,16 @@ class PluginsManager(PaymentInterface):
channel_slug=fulfillment.order.channel.slug,
)
- def fulfillment_approved(self, fulfillment: "Fulfillment"):
+ def fulfillment_approved(
+ self, fulfillment: "Fulfillment", notify_customer: Optional[bool] = True
+ ):
default_value = None
return self.__run_method_on_plugins(
"fulfillment_approved",
default_value,
fulfillment,
channel_slug=fulfillment.order.channel.slug,
+ notify_customer=notify_customer,
)
def fulfillment_metadata_updated(self, fulfillment: "Fulfillment"):
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index a6e3c02e01..2c7d084701 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -969,14 +969,26 @@ class WebhookPlugin(BasePlugin):
fulfillment_data, event_type, webhooks, fulfillment, self.requestor
)
- def fulfillment_approved(self, fulfillment: "Fulfillment", previous_value):
+ def fulfillment_approved(
+ self,
+ fulfillment: "Fulfillment",
+ notify_customer: Optional[bool] = True,
+ previous_value: Optional[Any] = None,
+ ):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.FULFILLMENT_APPROVED
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
+ fulfillment_data,
+ event_type,
+ webhooks,
+ {
+ "fulfillment": fulfillment,
+ "notify_customer": notify_customer,
+ },
+ self.requestor,
)
def fulfillment_metadata_updated(self, fulfillment: "Fulfillment", previous_value):
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
index d839735fde..a0697cad26 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -1372,12 +1372,14 @@ FULFILLMENT_CANCELED = (
"""
)
+
FULFILLMENT_APPROVED = (
fragments.FULFILLMENT_DETAILS
+ """
subscription{
event{
...on FulfillmentApproved{
+ notifyCustomer
fulfillment{
...FulfillmentDetails
}
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 bcf1922e72..e8b8f0832f 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
@@ -1691,13 +1691,22 @@ def test_fulfillment_approved(fulfillment, subscription_fulfillment_approved_web
# given
webhooks = [subscription_fulfillment_approved_webhook]
event_type = WebhookEventAsyncType.FULFILLMENT_APPROVED
- expected_payload = generate_fulfillment_payload(fulfillment)
+ expected_payload = generate_fulfillment_payload(
+ fulfillment, add_notify_customer_field=True
+ )
# when
- deliveries = create_deliveries_for_subscriptions(event_type, fulfillment, webhooks)
+ deliveries = create_deliveries_for_subscriptions(
+ event_type,
+ {
+ "fulfillment": fulfillment,
+ "notify_customer": True,
+ },
+ webhooks,
+ )
# then
- assert deliveries[0].payload.payload == json.dumps(expected_payload)
+ assert json.loads(deliveries[0].payload.payload) == expected_payload
assert len(deliveries) == len(webhooks)
assert deliveries[0].webhook == webhooks[0]