Skip to content

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

Provide webhooks in handle_promotion_toggle manager calls (#15093)

Iga Karbowiak556a3529bd8c
diff --git a/saleor/discount/tasks.py b/saleor/discount/tasks.py
index 460fc5d918..84542395c8 100644
--- a/saleor/discount/tasks.py
+++ b/saleor/discount/tasks.py
@@ -21,6 +21,8 @@ from ..product.models import (
)
from ..product.tasks import update_products_discounted_prices_for_promotion_task
from ..product.utils.variant_prices import update_discounted_prices_for_promotion
+from ..webhook.event_types import WebhookEventAsyncType
+from ..webhook.utils import get_webhooks_for_event
from . import DiscountType
from .models import (
OrderDiscount,
@@ -66,12 +68,15 @@ def handle_promotion_toggle():
promotions
)
+ started_webhooks = get_webhooks_for_event(WebhookEventAsyncType.PROMOTION_STARTED)
for starting_promo in starting_promotions:
- manager.promotion_started(starting_promo)
+ manager.promotion_started(starting_promo, webhooks=started_webhooks)
+ ended_webhooks = get_webhooks_for_event(WebhookEventAsyncType.PROMOTION_ENDED)
for ending_promo in ending_promotions:
- manager.promotion_ended(ending_promo)
+ manager.promotion_ended(ending_promo, webhooks=ended_webhooks)
+ toggle_webhooks = get_webhooks_for_event(WebhookEventAsyncType.SALE_TOGGLE)
# DEPRECATED: will be removed in Saleor 4.0.
for promotion in promotions:
variants = promotion_id_to_variants.get(promotion.id)
@@ -85,7 +90,7 @@ def handle_promotion_toggle():
"categories": [],
"collections": [],
}
- manager.sale_toggle(promotion, catalogues)
+ manager.sale_toggle(promotion, catalogues, webhooks=toggle_webhooks)
if ending_promotions:
clear_promotion_rule_variants_task.delay()
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index c890204088..8943f9d934 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -777,10 +777,10 @@ class PluginsManager(PaymentInterface):
"sale_updated", default_value, sale, previous_catalogue, current_catalogue
)
- def sale_toggle(self, sale: "Promotion", catalogue):
+ def sale_toggle(self, sale: "Promotion", catalogue, webhooks=None):
default_value = None
return self.__run_method_on_plugins(
- "sale_toggle", default_value, sale, catalogue
+ "sale_toggle", default_value, sale, catalogue, webhooks=webhooks
)
def promotion_created(self, promotion: "Promotion"):
@@ -801,15 +801,17 @@ class PluginsManager(PaymentInterface):
"promotion_deleted", default_value, promotion, webhooks=webhooks
)
- def promotion_started(self, promotion: "Promotion"):
+ def promotion_started(self, promotion: "Promotion", webhooks=None):
default_value = None
return self.__run_method_on_plugins(
- "promotion_started", default_value, promotion
+ "promotion_started", default_value, promotion, webhooks=webhooks
)
- def promotion_ended(self, promotion: "Promotion"):
+ def promotion_ended(self, promotion: "Promotion", webhooks=None):
default_value = None
- return self.__run_method_on_plugins("promotion_ended", default_value, promotion)
+ return self.__run_method_on_plugins(
+ "promotion_ended", default_value, promotion, webhooks=webhooks
+ )
def promotion_rule_created(self, promotion_rule: "PromotionRule"):
default_value = None
diff --git a/saleor/plugins/tests/sample_plugins.py b/saleor/plugins/tests/sample_plugins.py
index f0ea461ec3..52cc8440c6 100644
--- a/saleor/plugins/tests/sample_plugins.py
+++ b/saleor/plugins/tests/sample_plugins.py
@@ -244,6 +244,7 @@ class PluginSample(BasePlugin):
sale: "Promotion",
catalogue: defaultdict[str, set[str]],
previous_value: Any,
+ webhooks,
):
return sale, catalogue
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index 6754663c63..09612d7e6a 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -1020,11 +1020,12 @@ class WebhookPlugin(BasePlugin):
sale: "Promotion",
catalogue: defaultdict[str, set[str]],
previous_value: Any,
+ webhooks=None,
):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.SALE_TOGGLE
- if webhooks := get_webhooks_for_event(event_type):
+ if webhooks := get_webhooks_for_event(event_type, webhooks):
sale_data_generator = partial(
generate_sale_toggle_payload,
sale,
@@ -1088,21 +1089,25 @@ class WebhookPlugin(BasePlugin):
self,
promotion: "Promotion",
previous_value: Any,
+ webhooks=None,
):
if not self.active:
return previous_value
self._trigger_promotion_event(
- WebhookEventAsyncType.PROMOTION_STARTED, promotion
+ WebhookEventAsyncType.PROMOTION_STARTED, promotion, webhooks=webhooks
)
def promotion_ended(
self,
promotion: "Promotion",
previous_value: Any,
+ webhooks=None,
):
if not self.active:
return previous_value
- self._trigger_promotion_event(WebhookEventAsyncType.PROMOTION_ENDED, promotion)
+ self._trigger_promotion_event(
+ WebhookEventAsyncType.PROMOTION_ENDED, promotion, webhooks=webhooks
+ )
def _trigger_promotion_rule_event(
self, event_type: str, promotion_rule: "PromotionRule"