Skip to content

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

Improve draft orders webhooks in 3.1 (#8200)

Jakubd9c21de1c0ed
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8ea538ec06..eceb1449af 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,10 +12,12 @@ All notable, unreleased changes to this project will be documented in this file.
- Deprecate `setup_future_usage` from `checkoutComplete.paymentData` input - will be removed in Saleor 4.0 - #7994 by @mateuszgrzyb
- Possibility to pass metadata in input of `checkoutPaymentCreate` - #8076 by @mateuszgrzyb
- Fix shipping address issue in `availableCollectionPoints` resolver for checkout - #8143 by @kuchichan
+- Improve draft orders and orders webhooks by @jakubkuc
# 3.0.0 [Unreleased]
+- Improve draft orders and orders webhooks - #SALEOR-4008 by @jakubkuc
- Extend editorjs validator to accept blocks different than text - #SALEOR-3354 by @mociepka
- Add query contains only schema validation - #6827 by @fowczarek
- Add introspection caching - #6871 by @fowczarek
diff --git a/saleor/graphql/invoice/tests/test_invoice_delete.py b/saleor/graphql/invoice/tests/test_invoice_delete.py
index ae4dec2ecc..c67abc2c4e 100644
--- a/saleor/graphql/invoice/tests/test_invoice_delete.py
+++ b/saleor/graphql/invoice/tests/test_invoice_delete.py
@@ -36,7 +36,7 @@ def test_invoice_delete(staff_api_client, permission_manage_orders, order):
).exists()
-@patch("saleor.plugins.base_plugin.BasePlugin.invoice_delete")
+@patch("saleor.plugins.manager.PluginsManager.invoice_delete")
def test_invoice_delete_invalid_id(
plugin_mock, staff_api_client, permission_manage_orders
):
diff --git a/saleor/graphql/invoice/tests/test_invoice_request.py b/saleor/graphql/invoice/tests/test_invoice_request.py
index 152ab5b199..9e5533496f 100644
--- a/saleor/graphql/invoice/tests/test_invoice_request.py
+++ b/saleor/graphql/invoice/tests/test_invoice_request.py
@@ -41,7 +41,7 @@ def setup_dummy_gateways(settings):
return settings
-@patch("saleor.plugins.base_plugin.BasePlugin.invoice_request")
+@patch("saleor.plugins.manager.PluginsManager.invoice_request")
def test_invoice_request(
plugin_mock, staff_api_client, permission_manage_orders, order
):
@@ -61,7 +61,7 @@ def test_invoice_request(
number=number, order=order.pk, status=JobStatus.PENDING
).first()
assert invoice
- plugin_mock.assert_called_once_with(order, invoice, number, previous_value=None)
+ plugin_mock.assert_called_once_with(order=order, invoice=invoice, number=number)
assert InvoiceEvent.objects.filter(
type=InvoiceEvents.REQUESTED,
user=staff_api_client.user,
diff --git a/saleor/graphql/invoice/tests/test_invoice_request_delete.py b/saleor/graphql/invoice/tests/test_invoice_request_delete.py
index 2c916cfbe6..f8122507ff 100644
--- a/saleor/graphql/invoice/tests/test_invoice_request_delete.py
+++ b/saleor/graphql/invoice/tests/test_invoice_request_delete.py
@@ -29,7 +29,7 @@ def setup_dummy_gateways(settings):
return settings
-@patch("saleor.plugins.base_plugin.BasePlugin.invoice_delete")
+@patch("saleor.plugins.manager.PluginsManager.invoice_delete")
def test_invoice_request_delete(
plugin_mock, staff_api_client, permission_manage_orders, order
):
@@ -38,7 +38,7 @@ def test_invoice_request_delete(
staff_api_client.user.user_permissions.add(permission_manage_orders)
staff_api_client.post_graphql(INVOICE_REQUEST_DELETE_MUTATION, variables)
invoice.refresh_from_db()
- plugin_mock.assert_called_once_with(invoice, previous_value=None)
+ plugin_mock.assert_called_once_with(invoice)
assert InvoiceEvent.objects.filter(
type=InvoiceEvents.REQUESTED_DELETION,
user=staff_api_client.user,
@@ -47,7 +47,7 @@ def test_invoice_request_delete(
).exists()
-@patch("saleor.plugins.base_plugin.BasePlugin.invoice_delete")
+@patch("saleor.plugins.manager.PluginsManager.invoice_delete")
def test_invoice_request_delete_invalid_id(
plugin_mock, staff_api_client, permission_manage_orders
):
@@ -61,7 +61,7 @@ def test_invoice_request_delete_invalid_id(
plugin_mock.assert_not_called()
-@patch("saleor.plugins.base_plugin.BasePlugin.invoice_delete")
+@patch("saleor.plugins.manager.PluginsManager.invoice_delete")
def test_invoice_request_delete_no_permission(
plugin_mock, staff_api_client, permission_manage_orders, order
):
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.py
index 5fbf93a72c..bfe9f2c16c 100644
--- a/saleor/graphql/order/mutations/draft_orders.py
+++ b/saleor/graphql/order/mutations/draft_orders.py
@@ -1,5 +1,6 @@
import graphene
from django.core.exceptions import ValidationError
+from django.db import transaction
from graphene.types import InputObjectType
from ....account.models import User
@@ -313,6 +314,16 @@ class DraftOrderCreate(ModelMutation, I18nMixin):
code=OrderErrorCode.TAX_ERROR.value,
)
+ if new_instance:
+ transaction.on_commit(
+ lambda: info.context.plugins.draft_order_created(instance)
+ )
+
+ else:
+ transaction.on_commit(
+ lambda: info.context.plugins.draft_order_updated(instance)
+ )
+
# Post-process the results
recalculate_order(instance)
@@ -372,6 +383,14 @@ class DraftOrderDelete(ModelDeleteMutation):
}
)
+ @classmethod
+ @traced_atomic_transaction()
+ def perform_mutation(cls, _root, info, **data):
+ order = cls.get_instance(info, **data)
+ response = super().perform_mutation(_root, info, **data)
+ transaction.on_commit(lambda: info.context.plugins.draft_order_deleted(order))
+ return response
+
class DraftOrderComplete(BaseMutation):
order = graphene.Field(Order, description="Completed order.")
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index c2eeddea6c..4c3eb30dfc 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -1,5 +1,6 @@
import graphene
from django.core.exceptions import ValidationError
+from django.db import transaction
from ....account.models import User
from ....core.exceptions import InsufficientStock
@@ -168,6 +169,13 @@ def clean_order_refund(order):
)
+def get_webhook_handler_by_order_status(status, info):
+ if status == OrderStatus.DRAFT:
+ return info.context.plugins.draft_order_updated
+ else:
+ return info.context.plugins.order_updated
+
+
class OrderUpdateInput(graphene.InputObjectType):
billing_address = AddressInput(description="Billing address of the customer.")
user_email = graphene.String(description="Email address of the customer.")
@@ -228,7 +236,7 @@ class OrderUpdate(DraftOrderCreate):
info.context.plugins,
info.context.site.settings.include_taxes_in_prices,
)
- info.context.plugins.order_updated(instance)
+ transaction.on_commit(lambda: info.context.plugins.order_updated(instance))
class OrderUpdateShippingInput(graphene.InputObjectType):
@@ -420,6 +428,7 @@ class OrderAddNote(BaseMutation):
return cleaned_input
@classmethod
+ @traced_atomic_transaction()
def perform_mutation(cls, _root, info, **data):
order = cls.get_node_or_error(info, data.get("id"), only_type=Order)
cleaned_input = cls.clean_input(info, order, data)
@@ -429,6 +438,8 @@ class OrderAddNote(BaseMutation):
app=info.context.app,
message=cleaned_input["message"],
)
+ func = get_webhook_handler_by_order_status(order.status, info)
+ transaction.on_commit(lambda: func(order))
return OrderAddNote(order=order, event=event)
@@ -806,6 +817,7 @@ class OrderLinesCreate(EditableOrderValidationMixin, BaseMutation):
)
@classmethod
+ @traced_atomic_transaction()
def perform_mutation(cls, _root, info, **data):
order = cls.get_node_or_error(info, data.get("id"), only_type=Order)
cls.validate_order(order)
@@ -830,6 +842,10 @@ class OrderLinesCreate(EditableOrderValidationMixin, BaseMutation):
)
recalculate_order(order)
+
+ func = get_webhook_handler_by_order_status(order.status, info)
+ transaction.on_commit(lambda: func(order))
+
return OrderLinesCreate(order=order, order_lines=lines)
@@ -849,6 +865,7 @@ class OrderLineDelete(EditableOrderValidationMixin, BaseMutation):
error_type_field = "order_errors"
@classmethod
+ @traced_atomic_transaction()
def perform_mutation(cls, _root, info, id):
manager = info.context.plugins
line = cls.get_node_or_error(
@@ -896,6 +913,8 @@ class OrderLineDelete(EditableOrderValidationMixin, BaseMutation):
)
recalculate_order(order)
+ func = get_webhook_handler_by_order_status(order.status, info)
+ transaction.on_commit(lambda: func(order))
return OrderLineDelete(order=order, order_line=line)
@@ -934,6 +953,7 @@ class OrderLineUpdate(EditableOrderValidationMixin, ModelMutation):
return cleaned_input
@classmethod
+ @traced_atomic_transaction()
def save(cls, info, instance, cleaned_input):
manager = info.context.plugins
warehouse_pk = (
@@ -964,6 +984,9 @@ class OrderLineUpdate(EditableOrderValidationMixin, ModelMutation):
)
recalculate_order(instance.order)
+ func = get_webhook_handler_by_order_status(instance.order.status, info)
+ transaction.on_commit(lambda: func(instance.order))
+
@classmethod
def success_response(cls, instance):
response = super().success_response(instance)
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 8604775d96..32d40ca1a8 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -129,6 +129,15 @@ def orders(customer_user, channel_USD, channel_PLN):
)
+def assert_proper_webhook_called_once(order, status, draft_mock, order_mock):
+ if status == OrderStatus.DRAFT:
+ draft_mock.assert_called_once_with(order)
+ order_mock.assert_not_called()
+ else:
+ draft_mock.assert_not_called()
+ order_mock.assert_called_once_with(order)
+
+
def test_orderline_query(staff_api_client, permission_manage_orders, fulfilled_order):
order = fulfilled_order
query = """
@@ -3488,10 +3497,14 @@ def test_order_lines_create_for_variant_with_many_stocks_with_out_of_stock_webho
)
-@patch("saleor.plugins.manager.PluginsManager.product_variant_out_of_stock")
@pytest.mark.parametrize("status", (OrderStatus.DRAFT, OrderStatus.UNCONFIRMED))
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
+@patch("saleor.plugins.manager.PluginsManager.product_variant_out_of_stock")
def test_order_lines_create(
product_variant_out_of_stock_webhook_mock,
+ order_updated_webhook_mock,
+ draft_order_updated_webhook_mock,
status,
order_with_lines,
permission_manage_orders,
@@ -3511,11 +3524,16 @@ def test_order_lines_create(
# mutation should fail without proper permissions
response = staff_api_client.post_graphql(query, variables)
assert_no_permission(response)
+ order_updated_webhook_mock.assert_not_called()
+ draft_order_updated_webhook_mock.assert_not_called()
assert not OrderEvent.objects.exists()
# assign permissions
staff_api_client.user.user_permissions.add(permission_manage_orders)
response = staff_api_client.post_graphql(query, variables)
+ assert_proper_webhook_called_once(
+ order, status, draft_order_updated_webhook_mock, order_updated_webhook_mock
+ )
assert OrderEvent.objects.count() == 1
assert OrderEvent.objects.last().type == order_events.OrderEvents.ADDED_PRODUCTS
content = get_graphql_content(response)
@@ -3534,8 +3552,14 @@ def test_order_lines_create(
product_variant_out_of_stock_webhook_mock.assert_not_called()
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_order_lines_create_with_unavailable_variant(
- draft_order, permission_manage_orders, staff_api_client
+ order_updated_webhook_mock,
+ draft_order_updated_webhoook_mock,
+ draft_order,
+ permission_manage_orders,
+ staff_api_client,
):
query = ORDER_LINES_CREATE_MUTATION
order = draft_order
@@ -3556,10 +3580,16 @@ def test_order_lines_create_with_unavailable_variant(
assert error["code"] == OrderErrorCode.NOT_AVAILABLE_IN_CHANNEL.name
assert error["field"] == "input"
assert error["variants"] == [variant_id]
+ order_updated_webhook_mock.assert_not_called()
+ draft_order_updated_webhoook_mock.assert_not_called()
@pytest.mark.parametrize("status", (OrderStatus.DRAFT, OrderStatus.UNCONFIRMED))
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_order_lines_create_with_existing_variant(
+ order_updated_webhook_mock,
+ draft_order_updated_webhook_mock,
status,
order_with_lines,
permission_manage_orders,
@@ -3581,6 +3611,8 @@ def test_order_lines_create_with_existing_variant(
response = staff_api_client.post_graphql(query, variables)
assert_no_permission(response)
assert not OrderEvent.objects.exists()
+ order_updated_webhook_mock.assert_not_called()
+ draft_order_updated_webhook_mock.assert_not_called()
# assign permissions
staff_api_client.user.user_permissions.add(permission_manage_orders)
@@ -3591,11 +3623,22 @@ def test_order_lines_create_with_existing_variant(
data = content["data"]["orderLinesCreate"]
assert data["orderLines"][0]["productSku"] == variant.sku
assert data["orderLines"][0]["quantity"] == old_quantity + quantity
+ assert_proper_webhook_called_once(
+ order, status, draft_order_updated_webhook_mock, order_updated_webhook_mock
+ )
@pytest.mark.parametrize("status", (OrderStatus.DRAFT, OrderStatus.UNCONFIRMED))
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_order_lines_create_with_product_and_variant_not_assigned_to_channel(
- status, order_with_lines, permission_manage_orders, staff_api_client, variant
+ order_updated_webhook_mock,
+ draft_order_updated_webhook_mock,
+ status,
+ order_with_lines,
+ permission_manage_orders,
+ staff_api_client,
+ variant,
):
query = ORDER_LINES_CREATE_MUTATION
order = order_with_lines
@@ -3617,10 +3660,16 @@ def test_order_lines_create_with_product_and_variant_not_assigned_to_channel(
assert error["code"] == OrderErrorCode.PRODUCT_NOT_PUBLISHED.name
assert error["field"] == "input"
assert error["variants"] == [variant_id]
+ order_updated_webhook_mock.assert_not_called()
+ draft_order_updated_webhook_mock.assert_not_called()
@pytest.mark.parametrize("status", (OrderStatus.DRAFT, OrderStatus.UNCONFIRMED))
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_order_lines_create_with_variant_not_assigned_to_channel(
+ order_update_webhook_mock,
+ draft_order_update_webhook_mock,
status,
order_with_lines,
staff_api_client,
@@ -3650,10 +3699,18 @@ def test_order_lines_create_with_variant_not_assigned_to_channel(
assert error["code"] == OrderErrorCode.NOT_AVAILABLE_IN_CHANNEL.name
assert error["field"] == "input"
assert error["variants"] == [variant_id]
+ order_update_webhook_mock.assert_not_called()
+ draft_order_update_webhook_mock.assert_not_called()
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_invalid_order_when_creating_lines(
- order_with_lines, staff_api_client, permission_manage_orders
+ order_updated_webhook_mock,
+ draft_order_updated_webhook_mock,
+ order_with_lines,
+ staff_api_client,
+ permission_manage_orders,
):
query = ORDER_LINES_CREATE_MUTATION
order = order_with_lines
@@ -3668,6 +3725,8 @@ def test_invalid_order_when_creating_lines(
content = get_graphql_content(response)
data = content["data"]["orderLinesCreate"]
assert data["errors"]
+ order_updated_webhook_mock.assert_not_called()
+ draft_order_updated_webhook_mock.assert_not_called()
ORDER_LINE_UPDATE_MUTATION = """
@@ -3828,7 +3887,11 @@ def test_order_line_update_with_back_in_stock_webhook_called_twice_success_scena
@pytest.mark.parametrize("status", (OrderStatus.DRAFT, OrderStatus.UNCONFIRMED))
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_order_line_update(
+ order_updated_webhook_mock,
+ draft_order_updated_webhook_mock,
status,
order_with_lines,
permission_manage_orders,
@@ -3854,6 +3917,8 @@ def test_order_line_update(
# mutation should fail without proper permissions
response = staff_api_client.post_graphql(query, variables)
assert_no_permission(response)
+ order_updated_webhook_mock.assert_not_called()
+ draft_order_updated_webhook_mock.assert_not_called()
# assign permissions
staff_api_client.user.user_permissions.add(permission_manage_orders)
@@ -3861,7 +3926,9 @@ def test_order_line_update(
content = get_graphql_content(response)
data = content["data"]["orderLineUpdate"]
assert data["orderLine"]["quantity"] == new_quantity
-
+ assert_proper_webhook_called_once(
+ order, status, draft_order_updated_webhook_mock, order_updated_webhook_mock
+ )
removed_items_event = OrderEvent.objects.last() # type: OrderEvent
assert removed_items_event.type == order_events.OrderEvents.REMOVED_PRODUCTS
assert removed_items_event.user == staff_user
@@ -3878,8 +3945,14 @@ def test_order_line_update(
assert data["errors"][0]["field"] == "quantity"
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_invalid_order_when_updating_lines(
- order_with_lines, staff_api_client, permission_manage_orders
+ order_update_webhook_mock,
+ draft_order_update_webhook_mock,
+ order_with_lines,
+ staff_api_client,
+ permission_manage_orders,
):
query = ORDER_LINE_UPDATE_MUTATION
order = order_with_lines
@@ -3892,6 +3965,8 @@ def test_invalid_order_when_updating_lines(
content = get_graphql_content(response)
data = content["data"]["orderLineUpdate"]
assert data["errors"]
+ order_update_webhook_mock.assert_not_called()
+ draft_order_update_webhook_mock.assert_not_called()
QUERY_GET_FIRST_EVENT = """
@@ -4060,8 +4135,15 @@ def test_order_line_remove_with_back_in_stock_webhook(
@pytest.mark.parametrize("status", (OrderStatus.DRAFT, OrderStatus.UNCONFIRMED))
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_order_line_remove(
- status, order_with_lines, permission_manage_orders, staff_api_client
+ order_updated_webhook_mock,
+ draft_order_updated_webhook_mock,
+ status,
+ order_with_lines,
+ permission_manage_orders,
+ staff_api_client,
):
query = ORDER_LINE_DELETE_MUTATION
order = order_with_lines
@@ -4080,10 +4162,19 @@ def test_order_line_remove(
assert OrderEvent.objects.last().type == order_events.OrderEvents.REMOVED_PRODUCTS
assert data["orderLine"]["id"] == line_id
assert line not in order.lines.all()
+ assert_proper_webhook_called_once(
+ order, status, draft_order_updated_webhook_mock, order_updated_webhook_mock
+ )
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_invalid_order_when_removing_lines(
- staff_api_client, order_with_lines, permission_manage_orders
+ order_update_webhook_mock,
+ draft_order_update_webhook_mock,
+ staff_api_client,
+ order_with_lines,
+ permission_manage_orders,
):
query = ORDER_LINE_DELETE_MUTATION
order = order_with_lines
@@ -4096,6 +4187,8 @@ def test_invalid_order_when_removing_lines(
content = get_graphql_content(response)
data = content["data"]["orderLineDelete"]
assert data["errors"]
+ order_update_webhook_mock.assert_not_called()
+ draft_order_update_webhook_mock.assert_not_called()
ORDER_UPDATE_MUTATION = """
@@ -4117,9 +4210,9 @@ ORDER_UPDATE_MUTATION = """
"""
-@patch("saleor.plugins.base_plugin.BasePlugin.order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_order_update(
- plugin_mock,
+ order_updated_webhook_mock,
staff_api_client,
permission_manage_orders,
order_with_lines,
@@ -4150,12 +4243,12 @@ def test_order_update(
assert order.user_email == email
assert order.user is None
assert order.status == OrderStatus.UNFULFILLED
- assert plugin_mock.called is True
+ order_updated_webhook_mock.assert_called_once_with(order)
-@patch("saleor.plugins.base_plugin.BasePlugin.order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_order_update_with_draft_order(
- plugin_mock,
+ order_updated_webhook_mock,
staff_api_client,
permission_manage_orders,
draft_order,
@@ -4174,7 +4267,7 @@ def test_order_update_with_draft_order(
error = content["data"]["orderUpdate"]["errors"][0]
assert error["field"] == "id"
assert error["code"] == OrderErrorCode.INVALID.name
- assert plugin_mock.called is False
+ order_updated_webhook_mock.assert_not_called()
def test_order_update_anonymous_user_no_user_email(
@@ -4285,8 +4378,13 @@ ORDER_ADD_NOTE_MUTATION = """
"""
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_order_add_note_as_staff_user(
- staff_api_client, permission_manage_orders, order_with_lines, staff_user
+ order_updated_webhook_mock,
+ staff_api_client,
+ permission_manage_orders,
+ order_with_lines,
+ staff_user,
):
"""We are testing that adding a note to an order as a staff user is doing the
expected behaviors."""
@@ -4304,6 +4402,7 @@ def test_order_add_note_as_staff_user(
assert data["order"]["id"] == order_id
assert data["event"]["user"]["email"] == staff_user.email
assert data["event"]["message"] == message
+ order_updated_webhook_mock.assert_called_once_with(order)
order.refresh_from_db()
assert order.status == OrderStatus.UNFULFILLED
@@ -4325,8 +4424,13 @@ def test_order_add_note_as_staff_user(
" ",
),
)
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_order_add_note_fail_on_empty_message(
- staff_api_client, permission_manage_orders, order_with_lines, message
+ order_updated_webhook_mock,
+ staff_api_client,
+ permission_manage_orders,
+ order_with_lines,
+ message,
):
order_id = graphene.Node.to_global_id("Order", order_with_lines.id)
variables = {"id": order_id, "message": message}
@@ -4337,6 +4441,7 @@ def test_order_add_note_fail_on_empty_message(
data = content["data"]["orderAddNote"]
assert data["errors"][0]["field"] == "message"
assert data["errors"][0]["code"] == OrderErrorCode.REQUIRED.name
+ order_updated_webhook_mock.assert_not_called()
MUTATION_ORDER_CANCEL = """
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 87158e02ea..159f75e0b9 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -7182,6 +7182,9 @@ enum WebhookEventTypeEnum {
ORDER_UPDATED
ORDER_CANCELLED
ORDER_FULFILLED
+ DRAFT_ORDER_CREATED
+ DRAFT_ORDER_UPDATED
+ DRAFT_ORDER_DELETED
INVOICE_REQUESTED
INVOICE_DELETED
INVOICE_SENT
@@ -7221,6 +7224,9 @@ enum WebhookSampleEventTypeEnum {
ORDER_UPDATED
ORDER_CANCELLED
ORDER_FULFILLED
+ DRAFT_ORDER_CREATED
+ DRAFT_ORDER_UPDATED
+ DRAFT_ORDER_DELETED
INVOICE_REQUESTED
INVOICE_DELETED
INVOICE_SENT
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 038e8728ba..31f52c5559 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -544,6 +544,24 @@ class PluginsManager(PaymentInterface):
"order_confirmed", default_value, order, channel_slug=order.channel.slug
)
+ def draft_order_created(self, order: "Order"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "draft_order_created", default_value, order, channel_slug=order.channel.slug
+ )
+
+ def draft_order_updated(self, order: "Order"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "draft_order_updated", default_value, order, channel_slug=order.channel.slug
+ )
+
+ def draft_order_deleted(self, order: "Order"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "draft_order_deleted", default_value, order, channel_slug=order.channel.slug
+ )
+
def invoice_request(
self, order: "Order", invoice: "Invoice", number: Optional[str]
):
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index d718616142..ad3ddb15cf 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -125,6 +125,30 @@ class WebhookPlugin(BasePlugin):
order_data = generate_order_payload(order)
trigger_webhooks_for_event.delay(WebhookEventType.ORDER_FULFILLED, order_data)
+ def draft_order_created(self, order: "Order", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ order_data = generate_order_payload(order)
+ trigger_webhooks_for_event.delay(
+ WebhookEventType.DRAFT_ORDER_CREATED, order_data
+ )
+
+ def draft_order_updated(self, order: "Order", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ order_data = generate_order_payload(order)
+ trigger_webhooks_for_event.delay(
+ WebhookEventType.DRAFT_ORDER_UPDATED, order_data
+ )
+
+ def draft_order_deleted(self, order: "Order", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ order_data = generate_order_payload(order)
+ trigger_webhooks_for_event.delay(
+ WebhookEventType.DRAFT_ORDER_DELETED, order_data
+ )
+
def fulfillment_created(self, fulfillment: "Fulfillment", previous_value):
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 3a4e050d1a..18c551e674 100644
--- a/saleor/plugins/webhook/tests/test_webhook.py
+++ b/saleor/plugins/webhook/tests/test_webhook.py
@@ -116,6 +116,42 @@ def test_order_confirmed(mocked_webhook_trigger, settings, order_with_lines):
)
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")
+def test_draft_order_created(mocked_webhook_trigger, settings, order_with_lines):
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ manager = get_plugins_manager()
+ manager.draft_order_created(order_with_lines)
+
+ expected_data = generate_order_payload(order_with_lines)
+ mocked_webhook_trigger.assert_called_once_with(
+ WebhookEventType.DRAFT_ORDER_CREATED, expected_data
+ )
+
+
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")
+def test_draft_order_deleted(mocked_webhook_trigger, settings, order_with_lines):
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ manager = get_plugins_manager()
+ manager.draft_order_deleted(order_with_lines)
+
+ expected_data = generate_order_payload(order_with_lines)
+ mocked_webhook_trigger.assert_called_once_with(
+ WebhookEventType.DRAFT_ORDER_DELETED, expected_data
+ )
+
+
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")
+def test_draft_order_updated(mocked_webhook_trigger, settings, order_with_lines):
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ manager = get_plugins_manager()
+ manager.draft_order_updated(order_with_lines)
+
+ expected_data = generate_order_payload(order_with_lines)
+ mocked_webhook_trigger.assert_called_once_with(
+ WebhookEventType.DRAFT_ORDER_UPDATED, expected_data
+ )
+
+
@mock.patch("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")
def test_customer_created(mocked_webhook_trigger, settings, customer_user):
settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.py
index 8a0a2e8f90..229ebb9d56 100644
--- a/saleor/webhook/event_types.py
+++ b/saleor/webhook/event_types.py
@@ -19,6 +19,10 @@ class WebhookEventType:
FULFILLMENT_CANCELED = "fulfillment_canceled"
ORDER_FULFILLED = "order_fulfilled"
+ DRAFT_ORDER_CREATED = "draft_order_created"
+ DRAFT_ORDER_UPDATED = "draft_order_updated"
+ DRAFT_ORDER_DELETED = "draft_order_deleted"
+
INVOICE_REQUESTED = "invoice_requested"
INVOICE_DELETED = "invoice_deleted"
INVOICE_SENT = "invoice_sent"
@@ -67,6 +71,9 @@ class WebhookEventType:
ORDER_UPDATED: "Order updated",
ORDER_CANCELLED: "Order cancelled",
ORDER_FULFILLED: "Order fulfilled",
+ DRAFT_ORDER_CREATED: "Draft order created",
+ DRAFT_ORDER_UPDATED: "Draft order updated",
+ DRAFT_ORDER_DELETED: "Draft order deleted",
INVOICE_REQUESTED: "Invoice requested",
INVOICE_DELETED: "Invoice deleted",
INVOICE_SENT: "Invoice sent",
@@ -107,6 +114,9 @@ class WebhookEventType:
(ORDER_UPDATED, DISPLAY_LABELS[ORDER_UPDATED]),
(ORDER_CANCELLED, DISPLAY_LABELS[ORDER_CANCELLED]),
(ORDER_FULFILLED, DISPLAY_LABELS[ORDER_FULFILLED]),
+ (DRAFT_ORDER_CREATED, DISPLAY_LABELS[DRAFT_ORDER_CREATED]),
+ (DRAFT_ORDER_UPDATED, DISPLAY_LABELS[DRAFT_ORDER_UPDATED]),
+ (DRAFT_ORDER_DELETED, DISPLAY_LABELS[DRAFT_ORDER_DELETED]),
(INVOICE_REQUESTED, DISPLAY_LABELS[INVOICE_REQUESTED]),
(INVOICE_DELETED, DISPLAY_LABELS[INVOICE_DELETED]),
(INVOICE_SENT, DISPLAY_LABELS[INVOICE_SENT]),
@@ -156,6 +166,9 @@ class WebhookEventType:
ORDER_UPDATED: OrderPermissions.MANAGE_ORDERS,
ORDER_CANCELLED: OrderPermissions.MANAGE_ORDERS,
ORDER_FULFILLED: OrderPermissions.MANAGE_ORDERS,
+ DRAFT_ORDER_CREATED: OrderPermissions.MANAGE_ORDERS,
+ DRAFT_ORDER_DELETED: OrderPermissions.MANAGE_ORDERS,
+ DRAFT_ORDER_UPDATED: OrderPermissions.MANAGE_ORDERS,
INVOICE_REQUESTED: OrderPermissions.MANAGE_ORDERS,
INVOICE_DELETED: OrderPermissions.MANAGE_ORDERS,
INVOICE_SENT: OrderPermissions.MANAGE_ORDERS,
diff --git a/saleor/webhook/tests/test_webhook_sample_payloads.py b/saleor/webhook/tests/test_webhook_sample_payloads.py
index 27de223e5d..e8464bb7c0 100644
--- a/saleor/webhook/tests/test_webhook_sample_payloads.py
+++ b/saleor/webhook/tests/test_webhook_sample_payloads.py
@@ -110,6 +110,9 @@ def test_generate_sample_payload_fulfillment_created(fulfillment):
WebhookEventType.ORDER_CANCELLED,
WebhookEventType.ORDER_FULFILLED,
WebhookEventType.ORDER_FULLY_PAID,
+ WebhookEventType.DRAFT_ORDER_CREATED,
+ WebhookEventType.DRAFT_ORDER_UPDATED,
+ WebhookEventType.DRAFT_ORDER_DELETED,
WebhookEventType.PRODUCT_CREATED,
WebhookEventType.PRODUCT_UPDATED,
"Non_existing_event",