Skip to content

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

New events related to warehouse changes. (#9746)

SzymJebc0e0e66168
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 9246293a39..a1ccdd14c2 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1496,6 +1496,15 @@ enum WebhookEventTypeEnum {
TRANSLATION_CREATED
TRANSLATION_UPDATED
+ """A new warehouse created."""
+ WAREHOUSE_CREATED
+
+ """A warehouse is updated."""
+ WAREHOUSE_UPDATED
+
+ """A warehouse is deleted."""
+ WAREHOUSE_DELETED
+
"""A new voucher created."""
VOUCHER_CREATED
@@ -1738,6 +1747,15 @@ enum WebhookEventTypeAsyncEnum {
TRANSLATION_CREATED
TRANSLATION_UPDATED
+ """A new warehouse created."""
+ WAREHOUSE_CREATED
+
+ """A warehouse is updated."""
+ WAREHOUSE_UPDATED
+
+ """A warehouse is deleted."""
+ WAREHOUSE_DELETED
+
"""A new voucher created."""
VOUCHER_CREATED
@@ -2272,6 +2290,9 @@ enum WebhookSampleEventTypeEnum {
TRANSACTION_ACTION_REQUEST
TRANSLATION_CREATED
TRANSLATION_UPDATED
+ WAREHOUSE_CREATED
+ WAREHOUSE_UPDATED
+ WAREHOUSE_DELETED
VOUCHER_CREATED
VOUCHER_UPDATED
VOUCHER_DELETED
@@ -20294,7 +20315,7 @@ type Subscription {
event: Event
}
-union Event = AppInstalled | AppUpdated | AppDeleted | AppStatusChanged | CategoryCreated | CategoryUpdated | CategoryDeleted | ChannelCreated | ChannelUpdated | ChannelDeleted | ChannelStatusChanged | GiftCardCreated | GiftCardUpdated | GiftCardDeleted | GiftCardStatusChanged | MenuCreated | MenuUpdated | MenuDeleted | MenuItemCreated | MenuItemUpdated | MenuItemDeleted | OrderCreated | OrderUpdated | OrderConfirmed | OrderFullyPaid | OrderCancelled | OrderFulfilled | DraftOrderCreated | DraftOrderUpdated | DraftOrderDeleted | ProductCreated | ProductUpdated | ProductDeleted | ProductVariantCreated | ProductVariantUpdated | ProductVariantOutOfStock | ProductVariantBackInStock | ProductVariantDeleted | SaleCreated | SaleUpdated | SaleDeleted | InvoiceRequested | InvoiceDeleted | InvoiceSent | FulfillmentCreated | FulfillmentCanceled | CustomerCreated | CustomerUpdated | CollectionCreated | CollectionUpdated | CollectionDeleted | CheckoutCreated | CheckoutUpdated | PageCreated | PageUpdated | PageDeleted | ShippingPriceCreated | ShippingPriceUpdated | ShippingPriceDeleted | ShippingZoneCreated | ShippingZoneUpdated | ShippingZoneDeleted | TransactionActionRequest | TranslationCreated | TranslationUpdated | VoucherCreated | VoucherUpdated | VoucherDeleted
+union Event = AppInstalled | AppUpdated | AppDeleted | AppStatusChanged | CategoryCreated | CategoryUpdated | CategoryDeleted | ChannelCreated | ChannelUpdated | ChannelDeleted | ChannelStatusChanged | GiftCardCreated | GiftCardUpdated | GiftCardDeleted | GiftCardStatusChanged | MenuCreated | MenuUpdated | MenuDeleted | MenuItemCreated | MenuItemUpdated | MenuItemDeleted | OrderCreated | OrderUpdated | OrderConfirmed | OrderFullyPaid | OrderCancelled | OrderFulfilled | DraftOrderCreated | DraftOrderUpdated | DraftOrderDeleted | ProductCreated | ProductUpdated | ProductDeleted | ProductVariantCreated | ProductVariantUpdated | ProductVariantOutOfStock | ProductVariantBackInStock | ProductVariantDeleted | SaleCreated | SaleUpdated | SaleDeleted | InvoiceRequested | InvoiceDeleted | InvoiceSent | FulfillmentCreated | FulfillmentCanceled | CustomerCreated | CustomerUpdated | CollectionCreated | CollectionUpdated | CollectionDeleted | CheckoutCreated | CheckoutUpdated | PageCreated | PageUpdated | PageDeleted | ShippingPriceCreated | ShippingPriceUpdated | ShippingPriceDeleted | ShippingZoneCreated | ShippingZoneUpdated | ShippingZoneDeleted | TransactionActionRequest | TranslationCreated | TranslationUpdated | WarehouseCreated | WarehouseUpdated | WarehouseDeleted | VoucherCreated | VoucherUpdated | VoucherDeleted
type AppInstalled {
"""
@@ -21189,6 +21210,39 @@ type TranslationUpdated {
translation: TranslationTypes
}
+type WarehouseCreated {
+ """
+ Look up a warehouse.
+
+ Added in Saleor 3.4.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ warehouse: Warehouse
+}
+
+type WarehouseUpdated {
+ """
+ Look up a warehouse.
+
+ Added in Saleor 3.4.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ warehouse: Warehouse
+}
+
+type WarehouseDeleted {
+ """
+ Look up a warehouse.
+
+ Added in Saleor 3.4.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ warehouse: Warehouse
+}
+
type VoucherCreated {
"""
Look up a voucher.
diff --git a/saleor/graphql/tests/queries/fragments.py b/saleor/graphql/tests/queries/fragments.py
index 1a0b3786f0..e32c85fbef 100644
--- a/saleor/graphql/tests/queries/fragments.py
+++ b/saleor/graphql/tests/queries/fragments.py
@@ -142,23 +142,22 @@ fragment ShippingZoneDetails on ShippingZone {
"""
)
-WAREHOUSE_DETAILS = (
- SHIPPING_ZONE_DETAILS
- + """
+WAREHOUSE_DETAILS = """
fragment WarehouseDetails on Warehouse {
id
name
- companyName
- shippingZones {
+ shippingZones (first: 10) {
edges {
node {
- ...ShippingZoneDetails
+ id
}
}
}
+ address {
+ companyName
+ }
}
"""
-)
FULFILLMENT_DETAILS = (
diff --git a/saleor/graphql/warehouse/mutations.py b/saleor/graphql/warehouse/mutations.py
index 8ee730cfbe..15d4811937 100644
--- a/saleor/graphql/warehouse/mutations.py
+++ b/saleor/graphql/warehouse/mutations.py
@@ -98,6 +98,10 @@ class WarehouseCreate(WarehouseMixin, ModelMutation, I18nMixin):
address_form = cls.validate_address_form(cleaned_data["address"])
return address_form.save()
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ info.context.plugins.warehouse_created(instance)
+
class WarehouseShippingZoneAssign(WarehouseMixin, ModelMutation, I18nMixin):
class Meta:
@@ -177,6 +181,10 @@ class WarehouseUpdate(WarehouseMixin, ModelMutation, I18nMixin):
address_form = cls.validate_address_form(address_data, instance=address)
return address_form.save()
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ info.context.plugins.warehouse_updated(instance)
+
class WarehouseDelete(ModelDeleteMutation):
class Meta:
@@ -197,8 +205,35 @@ class WarehouseDelete(ModelDeleteMutation):
node_id = data.get("id")
model_type = cls.get_type_for_model()
instance = cls.get_node_or_error(info, node_id, only_type=model_type)
+
+ if instance:
+ cls.clean_instance(info, instance)
+
stocks = (stock for stock in instance.stock_set.only("product_variant"))
- result = super(WarehouseDelete, cls).perform_mutation(_root, info, **data)
+ address_id = instance.address_id
+ address = instance.address
+
+ db_id = instance.id
+ instance.delete()
+
+ # After the instance is deleted, set its ID to the original database's
+ # ID so that the success response contains ID of the deleted object.
+ # Additionally, assign copy of deleted Address object to allow fetching address
+ # data on success response or in subscription webhook query.
+ instance.id = db_id
+ address.id = address_id
+ instance.address = address
+
+ # Set `is_object_deleted` attribute to use it in Warehouse object type
+ # resolvers and for example decide if we should use Dataloader to resolve
+ # address or return object directly.
+ instance.is_object_deleted = True
+
+ cls.post_save_action(info, instance, None)
for stock in stocks:
transaction.on_commit(lambda: manager.product_variant_out_of_stock(stock))
- return result
+ return cls.success_response(instance)
+
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ transaction.on_commit(lambda: info.context.plugins.warehouse_deleted(instance))
diff --git a/saleor/graphql/warehouse/tests/test_warehouse.py b/saleor/graphql/warehouse/tests/test_warehouse.py
index 123a37b602..9f2cd3d63f 100644
--- a/saleor/graphql/warehouse/tests/test_warehouse.py
+++ b/saleor/graphql/warehouse/tests/test_warehouse.py
@@ -2,11 +2,13 @@ from unittest.mock import patch
import graphene
import pytest
+from django.utils.functional import SimpleLazyObject
from ....account.models import Address
from ....warehouse import WarehouseClickAndCollectOption
from ....warehouse.error_codes import WarehouseErrorCode
from ....warehouse.models import Stock, Warehouse
+from ....webhook.event_types import WebhookEventAsyncType
from ...tests.utils import (
assert_no_permission,
get_graphql_content,
@@ -594,6 +596,62 @@ def test_mutation_create_warehouse(
assert created_warehouse["companyName"] == warehouse.address.company_name
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_mutation_create_warehouse_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ permission_manage_products,
+ shipping_zone,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ variables = {
+ "input": {
+ "name": "Test warehouse",
+ "slug": "test-warhouse",
+ "email": "[email protected]",
+ "address": {
+ "streetAddress1": "Teczowa 8",
+ "city": "Wroclaw",
+ "country": "PL",
+ "postalCode": "53-601",
+ "companyName": "Amazing Company Inc",
+ },
+ "shippingZones": [
+ graphene.Node.to_global_id("ShippingZone", shipping_zone.id)
+ ],
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ MUTATION_CREATE_WAREHOUSE,
+ variables=variables,
+ permissions=[permission_manage_products],
+ )
+ content = get_graphql_content(response)
+ warehouse = Warehouse.objects.last()
+
+ # then
+ assert content["data"]["createWarehouse"]["warehouse"]
+ mocked_webhook_trigger.assert_called_once_with(
+ {
+ "id": graphene.Node.to_global_id("Warehouse", warehouse.id),
+ "name": warehouse.name,
+ },
+ WebhookEventAsyncType.WAREHOUSE_CREATED,
+ [any_webhook],
+ warehouse,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+
def test_mutation_create_warehouse_does_not_create_when_name_is_empty_string(
staff_api_client, permission_manage_products, shipping_zone
):
@@ -723,6 +781,50 @@ def test_mutation_update_warehouse(
assert warehouse.slug == warehouse_slug
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_mutation_update_warehouse_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ warehouse,
+ permission_manage_products,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.id)
+ variables = {
+ "id": warehouse_id,
+ "input": {"name": "New name"},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ MUTATION_UPDATE_WAREHOUSE,
+ variables=variables,
+ permissions=[permission_manage_products],
+ )
+ content = get_graphql_content(response)
+ warehouse.refresh_from_db()
+
+ # then
+ assert content["data"]["updateWarehouse"]["warehouse"]
+ mocked_webhook_trigger.assert_called_once_with(
+ {
+ "id": variables["id"],
+ "name": warehouse.name,
+ },
+ WebhookEventAsyncType.WAREHOUSE_UPDATED,
+ [any_webhook],
+ warehouse,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+
def test_mutation_update_warehouse_can_update_address(
staff_api_client, warehouse, permission_manage_products
):
@@ -981,6 +1083,43 @@ def test_delete_warehouse_mutation(
assert not Warehouse.objects.exists()
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_delete_warehouse_mutation_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ warehouse,
+ permission_manage_products,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
+ response = staff_api_client.post_graphql(
+ MUTATION_DELETE_WAREHOUSE,
+ variables={"id": warehouse_id},
+ permissions=[permission_manage_products],
+ )
+ content = get_graphql_content(response)
+
+ # then
+ assert len(content["data"]["deleteWarehouse"]["errors"]) == 0
+ mocked_webhook_trigger.assert_called_once_with(
+ {
+ "id": warehouse_id,
+ "name": warehouse.name,
+ },
+ WebhookEventAsyncType.WAREHOUSE_DELETED,
+ [any_webhook],
+ warehouse,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+
@patch("saleor.plugins.manager.PluginsManager.product_variant_out_of_stock")
def test_delete_warehouse_mutation_with_webhooks(
product_variant_out_of_stock_webhook,
diff --git a/saleor/graphql/warehouse/types.py b/saleor/graphql/warehouse/types.py
index f136ad2c6d..bb8a1e7e75 100644
--- a/saleor/graphql/warehouse/types.py
+++ b/saleor/graphql/warehouse/types.py
@@ -108,6 +108,9 @@ class Warehouse(ModelObjectType):
@staticmethod
def resolve_address(root, info):
+ if hasattr(root, "is_object_deleted") and root.is_object_deleted:
+ return root.address
+
return AddressByIdLoader(info.context).load(root.address_id)
@staticmethod
diff --git a/saleor/graphql/webhook/enums.py b/saleor/graphql/webhook/enums.py
index 617d95628b..58d9453c5b 100644
--- a/saleor/graphql/webhook/enums.py
+++ b/saleor/graphql/webhook/enums.py
@@ -78,6 +78,9 @@ WEBHOOK_EVENT_DESCRIPTION = {
WebhookEventAsyncType.SHIPPING_ZONE_CREATED: "A new shipping zone is created.",
WebhookEventAsyncType.SHIPPING_ZONE_UPDATED: "A shipping zone is updated.",
WebhookEventAsyncType.SHIPPING_ZONE_DELETED: "A shipping zone is deleted.",
+ WebhookEventAsyncType.WAREHOUSE_CREATED: "A new warehouse created.",
+ WebhookEventAsyncType.WAREHOUSE_UPDATED: "A warehouse is updated.",
+ WebhookEventAsyncType.WAREHOUSE_DELETED: "A warehouse is deleted.",
WebhookEventAsyncType.VOUCHER_CREATED: "A new voucher created.",
WebhookEventAsyncType.VOUCHER_UPDATED: "A voucher is updated.",
WebhookEventAsyncType.VOUCHER_DELETED: "A voucher is deleted.",
diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.py
index fc3c458b64..b3cb02a80c 100644
--- a/saleor/graphql/webhook/subscription_types.py
+++ b/saleor/graphql/webhook/subscription_types.py
@@ -649,6 +649,30 @@ class TranslationUpdated(ObjectType, TranslationBase):
...
+class WarehouseBase(AbstractType):
+ warehouse = graphene.Field(
+ "saleor.graphql.warehouse.types.Warehouse",
+ description="Look up a warehouse." + ADDED_IN_34 + PREVIEW_FEATURE,
+ )
+
+ @staticmethod
+ def resolve_warehouse(root, _info):
+ _, warehouse = root
+ return warehouse
+
+
+class WarehouseCreated(ObjectType, WarehouseBase):
+ ...
+
+
+class WarehouseUpdated(ObjectType, WarehouseBase):
+ ...
+
+
+class WarehouseDeleted(ObjectType, WarehouseBase):
+ ...
+
+
class VoucherBase(AbstractType):
voucher = graphene.Field(
"saleor.graphql.discount.types.Voucher",
@@ -744,6 +768,9 @@ class Event(Union):
TransactionActionRequest,
TranslationCreated,
TranslationUpdated,
+ WarehouseCreated,
+ WarehouseUpdated,
+ WarehouseDeleted,
VoucherCreated,
VoucherUpdated,
VoucherDeleted,
@@ -821,6 +848,9 @@ class Event(Union):
WebhookEventAsyncType.TRANSACTION_ACTION_REQUEST: TransactionActionRequest,
WebhookEventAsyncType.TRANSLATION_CREATED: TranslationCreated,
WebhookEventAsyncType.TRANSLATION_UPDATED: TranslationUpdated,
+ WebhookEventAsyncType.WAREHOUSE_CREATED: WarehouseCreated,
+ WebhookEventAsyncType.WAREHOUSE_UPDATED: WarehouseUpdated,
+ WebhookEventAsyncType.WAREHOUSE_DELETED: WarehouseDeleted,
WebhookEventAsyncType.VOUCHER_CREATED: VoucherCreated,
WebhookEventAsyncType.VOUCHER_UPDATED: VoucherUpdated,
WebhookEventAsyncType.VOUCHER_DELETED: VoucherDeleted,
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 76e67aa093..64e2a416f7 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -52,6 +52,7 @@ if TYPE_CHECKING:
)
from ..shipping.interface import ShippingMethodData
from ..shipping.models import ShippingMethod, ShippingZone
+ from ..warehouse.models import Warehouse
PluginConfigurationType = List[dict]
NoneType = type(None)
@@ -719,6 +720,24 @@ class BasePlugin:
void_payment: Callable[["PaymentData", Any], GatewayResponse]
+ # Trigger when warehouse is created.
+ #
+ # Overwrite this method if you need to trigger specific logic after a warehouse is
+ # created.
+ warehouse_created: Callable[["Warehouse", None], None]
+
+ # Trigger when warehouse is deleted.
+ #
+ # Overwrite this method if you need to trigger specific logic after a warehouse is
+ # deleted.
+ warehouse_deleted: Callable[["Warehouse", None], None]
+
+ # Trigger when warehouse is updated.
+ #
+ # Overwrite this method if you need to trigger specific logic after a warehouse is
+ # updated.
+ warehouse_updated: Callable[["Warehouse", None], None]
+
# Trigger when voucher is created.
#
# Overwrite this method if you need to trigger specific logic after a voucher is
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 33892f46b5..ae6f43acc8 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -68,7 +68,7 @@ if TYPE_CHECKING:
from ..shipping.interface import ShippingMethodData
from ..shipping.models import ShippingMethod, ShippingZone
from ..translation.models import Translation
- from ..warehouse.models import Stock
+ from ..warehouse.models import Stock, Warehouse
from .base_plugin import BasePlugin
NotifyEventTypeChoice = str
@@ -944,6 +944,24 @@ class PluginsManager(PaymentInterface):
"shipping_zone_deleted", default_value, shipping_zone
)
+ def warehouse_created(self, warehouse: "Warehouse"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "warehouse_created", default_value, warehouse
+ )
+
+ def warehouse_updated(self, warehouse: "Warehouse"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "warehouse_updated", default_value, warehouse
+ )
+
+ def warehouse_deleted(self, warehouse: "Warehouse"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "warehouse_deleted", default_value, warehouse
+ )
+
def voucher_created(self, voucher: "Voucher"):
default_value = None
return self.__run_method_on_plugins("voucher_created", default_value, voucher)
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index 5537d55ab8..32b33ab2eb 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -70,7 +70,7 @@ if TYPE_CHECKING:
from ...shipping.interface import ShippingMethodData
from ...shipping.models import ShippingMethod, ShippingZone
from ...translation.models import Translation
- from ...warehouse.models import Stock
+ from ...warehouse.models import Stock, Warehouse
logger = logging.getLogger(__name__)
@@ -835,6 +835,37 @@ class WebhookPlugin(BasePlugin):
translation_data, event_type, webhooks, translation, self.requestor
)
+ def _trigger_warehouse_event(self, event_type, warehouse):
+ if webhooks := get_webhooks_for_event(event_type):
+ payload = {
+ "id": graphene.Node.to_global_id("Warehouse", warehouse.id),
+ "name": warehouse.name,
+ }
+ trigger_webhooks_async(
+ payload, event_type, webhooks, warehouse, self.requestor
+ )
+
+ def warehouse_created(self, warehouse: "Warehouse", previous_value: None) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_warehouse_event(
+ WebhookEventAsyncType.WAREHOUSE_CREATED, warehouse
+ )
+
+ def warehouse_updated(self, warehouse: "Warehouse", previous_value: None) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_warehouse_event(
+ WebhookEventAsyncType.WAREHOUSE_UPDATED, warehouse
+ )
+
+ def warehouse_deleted(self, warehouse: "Warehouse", previous_value: None) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_warehouse_event(
+ WebhookEventAsyncType.WAREHOUSE_DELETED, warehouse
+ )
+
def _trigger_voucher_event(self, event_type, voucher):
if webhooks := get_webhooks_for_event(event_type):
payload = {
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
index 6e7ba90ae0..2db0d5b9cb 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
@@ -733,6 +733,27 @@ def subscription_translation_updated_webhook(subscription_webhook):
)
+def subscription_warehouse_created_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.WAREHOUSE_CREATED, WebhookEventAsyncType.WAREHOUSE_CREATED
+ )
+
+
+def subscription_warehouse_updated_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.WAREHOUSE_UPDATED, WebhookEventAsyncType.WAREHOUSE_UPDATED
+ )
+
+
+def subscription_warehouse_deleted_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.WAREHOUSE_DELETED, WebhookEventAsyncType.WAREHOUSE_DELETED
+ )
+
+
@pytest.fixture
def subscription_voucher_created_webhook(subscription_webhook):
return subscription_webhook(
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
index defc95a100..2b4a5c0f48 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
@@ -309,3 +309,28 @@ def generate_menu_item_payload(menu_item, menu_item_global_id):
},
"meta": None,
}
+
+
+def generate_warehouse_payload(warehouse, warehouse_global_id):
+ return json.dumps(
+ {
+ "warehouse": {
+ "id": warehouse_global_id,
+ "name": warehouse.name,
+ "shippingZones": {
+ "edges": [
+ {
+ "node": {
+ "id": graphene.Node.to_global_id(
+ "ShippingZone", zone.id
+ )
+ }
+ }
+ for zone in warehouse.shipping_zones.all()
+ ]
+ },
+ "address": {"companyName": warehouse.address.company_name},
+ },
+ "meta": None,
+ }
+ )
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
index a4a3fdb007..10bcdcccc9 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -988,3 +988,49 @@ TEST_VALID_SUBSCRIPTION_QUERY = """
}
}
"""
+
+
+WAREHOUSE_CREATED = (
+ fragments.WAREHOUSE_DETAILS
+ + """
+ subscription{
+ event{
+ ...on WarehouseCreated{
+ warehouse{
+ ...WarehouseDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+WAREHOUSE_UPDATED = (
+ fragments.WAREHOUSE_DETAILS
+ + """
+ subscription{
+ event{
+ ...on WarehouseUpdated{
+ warehouse{
+ ...WarehouseDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+WAREHOUSE_DELETED = (
+ fragments.WAREHOUSE_DETAILS
+ + """
+ subscription{
+ event{
+ ...on WarehouseDeleted{
+ warehouse{
+ ...WarehouseDetails
+ }
+ }
+ }
+ }
+"""
+)
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 7ee6589593..2d32c891d0 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
@@ -26,6 +26,7 @@ from .payloads import (
generate_sale_payload,
generate_shipping_method_payload,
generate_voucher_payload,
+ generate_warehouse_payload,
)
@@ -1122,6 +1123,62 @@ def test_product_created_multiple_events_in_subscription(
assert deliveries[0].webhook == webhooks[0]
+def test_warehouse_created(warehouse, subscription_warehouse_created_webhook):
+ # given
+ webhooks = [subscription_warehouse_created_webhook]
+ event_type = WebhookEventAsyncType.WAREHOUSE_CREATED
+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.id)
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, warehouse, webhooks)
+
+ # then
+ expected_payload = generate_warehouse_payload(warehouse, warehouse_id)
+
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_warehouse_updated(warehouse, subscription_warehouse_updated_webhook):
+ # given
+ webhooks = [subscription_warehouse_updated_webhook]
+ event_type = WebhookEventAsyncType.WAREHOUSE_UPDATED
+ voucher_id = graphene.Node.to_global_id("Warehouse", warehouse.id)
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, warehouse, webhooks)
+
+ # then
+ expected_payload = generate_warehouse_payload(warehouse, voucher_id)
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_warehouse_deleted(warehouse, subscription_warehouse_deleted_webhook):
+ # given
+ webhooks = [subscription_warehouse_deleted_webhook]
+
+ warehouse_id = warehouse.id
+ warehouse.delete()
+ warehouse.id = warehouse_id
+ warehouse.is_object_deleted = True
+
+ event_type = WebhookEventAsyncType.WAREHOUSE_DELETED
+ warehouse_global_id = graphene.Node.to_global_id("Warehouse", warehouse.id)
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, warehouse, webhooks)
+
+ # then
+ expected_payload = generate_warehouse_payload(warehouse, warehouse_global_id)
+
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
def test_voucher_created(voucher, subscription_voucher_created_webhook):
# given
webhooks = [subscription_voucher_created_webhook]
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 196b61e510..02792b93c9 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -5155,12 +5155,14 @@ def webhook_app(
permission_manage_gift_card,
permission_manage_discounts,
permission_manage_menus,
+ permission_manage_products,
):
app = App.objects.create(name="Sample app objects", is_active=True)
app.permissions.add(permission_manage_shipping)
app.permissions.add(permission_manage_gift_card)
app.permissions.add(permission_manage_discounts)
app.permissions.add(permission_manage_menus)
+ app.permissions.add(permission_manage_products)
return app
diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.py
index b4041e1ca8..d742094b58 100644
--- a/saleor/webhook/event_types.py
+++ b/saleor/webhook/event_types.py
@@ -106,6 +106,10 @@ class WebhookEventAsyncType:
TRANSLATION_CREATED = "translation_created"
TRANSLATION_UPDATED = "translation_updated"
+ WAREHOUSE_CREATED = "warehouse_created"
+ WAREHOUSE_UPDATED = "warehouse_updated"
+ WAREHOUSE_DELETED = "warehouse_deleted"
+
VOUCHER_CREATED = "voucher_created"
VOUCHER_UPDATED = "voucher_updated"
VOUCHER_DELETED = "voucher_deleted"
@@ -180,6 +184,9 @@ class WebhookEventAsyncType:
TRANSACTION_ACTION_REQUEST: "Payment action request",
TRANSLATION_CREATED: "Create translation",
TRANSLATION_UPDATED: "Update translation",
+ WAREHOUSE_CREATED: "Warehouse created",
+ WAREHOUSE_UPDATED: "Warehouse updated",
+ WAREHOUSE_DELETED: "Warehouse deleted",
VOUCHER_CREATED: "Voucher created",
VOUCHER_UPDATED: "Voucher updated",
VOUCHER_DELETED: "Voucher deleted",
@@ -254,6 +261,9 @@ class WebhookEventAsyncType:
(TRANSACTION_ACTION_REQUEST, DISPLAY_LABELS[TRANSACTION_ACTION_REQUEST]),
(TRANSLATION_CREATED, DISPLAY_LABELS[TRANSLATION_CREATED]),
(TRANSLATION_UPDATED, DISPLAY_LABELS[TRANSLATION_UPDATED]),
+ (WAREHOUSE_CREATED, DISPLAY_LABELS[WAREHOUSE_CREATED]),
+ (WAREHOUSE_UPDATED, DISPLAY_LABELS[WAREHOUSE_UPDATED]),
+ (WAREHOUSE_DELETED, DISPLAY_LABELS[WAREHOUSE_DELETED]),
(VOUCHER_CREATED, DISPLAY_LABELS[VOUCHER_CREATED]),
(VOUCHER_UPDATED, DISPLAY_LABELS[VOUCHER_UPDATED]),
(VOUCHER_DELETED, DISPLAY_LABELS[VOUCHER_DELETED]),
@@ -329,6 +339,9 @@ class WebhookEventAsyncType:
TRANSACTION_ACTION_REQUEST: PaymentPermissions.HANDLE_PAYMENTS,
TRANSLATION_CREATED: SitePermissions.MANAGE_TRANSLATIONS,
TRANSLATION_UPDATED: SitePermissions.MANAGE_TRANSLATIONS,
+ WAREHOUSE_CREATED: ProductPermissions.MANAGE_PRODUCTS,
+ WAREHOUSE_UPDATED: ProductPermissions.MANAGE_PRODUCTS,
+ WAREHOUSE_DELETED: ProductPermissions.MANAGE_PRODUCTS,
VOUCHER_CREATED: DiscountPermissions.MANAGE_DISCOUNTS,
VOUCHER_UPDATED: DiscountPermissions.MANAGE_DISCOUNTS,
VOUCHER_DELETED: DiscountPermissions.MANAGE_DISCOUNTS,
@@ -473,6 +486,9 @@ SUBSCRIBABLE_EVENTS = [
WebhookEventAsyncType.TRANSACTION_ACTION_REQUEST,
WebhookEventAsyncType.TRANSLATION_CREATED,
WebhookEventAsyncType.TRANSLATION_UPDATED,
+ WebhookEventAsyncType.WAREHOUSE_CREATED,
+ WebhookEventAsyncType.WAREHOUSE_UPDATED,
+ WebhookEventAsyncType.WAREHOUSE_DELETED,
WebhookEventAsyncType.VOUCHER_CREATED,
WebhookEventAsyncType.VOUCHER_UPDATED,
WebhookEventAsyncType.VOUCHER_DELETED,