Skip to content

Commit

Permalink
Merge pull request #1541 from maykinmedia/tasks/2952-async-notifications
Browse files Browse the repository at this point in the history
[#2952] Wrap the zaak notification handler in a Celery task
  • Loading branch information
alextreme authored Jan 8, 2025
2 parents fecca33 + c47d97a commit 27bc304
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/open_inwoner/openzaak/api/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dataclasses
import logging

from rest_framework import status
Expand All @@ -10,7 +11,7 @@
from open_inwoner.openzaak.api_models import Notification
from open_inwoner.openzaak.auth import get_valid_subscription_from_request
from open_inwoner.openzaak.exceptions import InvalidAuth
from open_inwoner.openzaak.notifications import handle_zaken_notification
from open_inwoner.openzaak.tasks import process_zaken_notification
from open_inwoner.utils.logentry import system_action as log_system_action

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -95,4 +96,6 @@ def handle_notification(self, notification: Notification):
config = SiteConfiguration.get_solo()
if not config.notifications_cases_enabled:
return
handle_zaken_notification(notification)

notification_data = dataclasses.asdict(notification)
process_zaken_notification.delay(notification_data)
1 change: 0 additions & 1 deletion src/open_inwoner/openzaak/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ def form_service(self, service):
),
default=False,
)

order_statuses_by_date_set = models.BooleanField(
verbose_name=_(
"On the detail page of the case, order the statuses based on the date they have been set"
Expand Down
11 changes: 11 additions & 0 deletions src/open_inwoner/openzaak/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

from django.core.management import call_command

from zgw_consumers.api_models.base import factory

from open_inwoner.celery import app
from open_inwoner.openzaak.api_models import Notification
from open_inwoner.openzaak.notifications import handle_zaken_notification

logger = logging.getLogger(__name__)

Expand All @@ -19,3 +23,10 @@ def import_zgw_data():
logger.info("finished import_zgw_data() task")

return out.getvalue()


@app.task
def process_zaken_notification(notification_data: dict):
logger.info("Started process_zaken_notification() task")
notification = factory(Notification, notification_data)
handle_zaken_notification(notification)
6 changes: 4 additions & 2 deletions src/open_inwoner/openzaak/tests/test_notification_webhook.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from unittest.mock import patch

from django.test import TestCase
from django.test import TestCase, override_settings
from django.urls import reverse_lazy

from rest_framework import status
Expand Down Expand Up @@ -38,6 +38,7 @@ def generate_auth_header_value(client_id, secret):
return auth_value


@override_settings(CELERY_TASK_ALWAYS_EAGER=True, CELERY_TASK_EAGER_PROPAGATES=True)
class NotificationSubscriptionAuthTest(TestCase):
def test_valid_auth_retrieves_subscription(self):
subscription = SubscriptionFactory(client_id="foo", secret="password")
Expand Down Expand Up @@ -76,7 +77,8 @@ def test_invalid_auth_header_raises_exception(self):
get_valid_subscriptions_from_bearer(auth_value)


@patch("open_inwoner.openzaak.api.views.handle_zaken_notification", autospec=True)
@override_settings(CELERY_TASK_ALWAYS_EAGER=True, CELERY_TASK_EAGER_PROPAGATES=True)
@patch("open_inwoner.openzaak.tasks.handle_zaken_notification", autospec=True)
class NotificationWebhookAPITestCase(AssertTimelineLogMixin, APITestCase):
"""
NOTE these tests run against the mounted zaken webhook (eg: ZakenNotificationsWebhookView),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .test_notification_data import MockAPIData, MockAPIDataAlt


@override_settings(CELERY_TASK_ALWAYS_EAGER=True, CELERY_TASK_EAGER_PROPAGATES=True)
@requests_mock.Mocker()
@patch("open_inwoner.openzaak.notifications._handle_status_update", autospec=True)
class StatusNotificationHandlerTestCase(
Expand Down

0 comments on commit 27bc304

Please sign in to comment.