Skip to content

Commit

Permalink
Merge pull request #1370 from maykinmedia/task/2700-zaak-notification…
Browse files Browse the repository at this point in the history
…s-backends

[#2700] Support multiple ZGW backends for zaak notifications
  • Loading branch information
alextreme authored Aug 30, 2024
2 parents da110e7 + a2d28f5 commit 6dbabce
Show file tree
Hide file tree
Showing 7 changed files with 483 additions and 133 deletions.
6 changes: 4 additions & 2 deletions src/open_inwoner/cms/cases/views/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from open_inwoner.openzaak.api_models import Status, StatusType, Zaak
from open_inwoner.openzaak.clients import CatalogiClient, ZakenClient
from open_inwoner.openzaak.documents import (
fetch_single_information_object_url,
fetch_single_information_object_from_url,
fetch_single_information_object_uuid,
)
from open_inwoner.openzaak.models import (
Expand Down Expand Up @@ -630,7 +630,9 @@ def get_case_document_files(
# [case_info.informatieobject for case_info in case_info_objects],
# )
info_objects = [
fetch_single_information_object_url(case_info.informatieobject)
fetch_single_information_object_from_url(
case_info.informatieobject, api_group=api_group
)
for case_info in case_info_objects
]

Expand Down
9 changes: 5 additions & 4 deletions src/open_inwoner/openzaak/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
from django.conf import settings

from open_inwoner.openzaak.api_models import InformatieObject
from open_inwoner.openzaak.clients import DocumentenClient, build_documenten_client
from open_inwoner.openzaak.clients import DocumentenClient

from ..utils.decorators import cache as cache_result

logger = logging.getLogger(__name__)


@cache_result("information_object_url:{url}", timeout=settings.CACHE_ZGW_ZAKEN_TIMEOUT)
def fetch_single_information_object_url(url: str) -> InformatieObject | None:
if client := build_documenten_client():
return client._fetch_single_information_object(url=url)
def fetch_single_information_object_from_url(
url: str, api_group
) -> InformatieObject | None:
return api_group.documenten_client._fetch_single_information_object(url=url)


# not cached because currently only used in info-object download view
Expand Down
103 changes: 43 additions & 60 deletions src/open_inwoner/openzaak/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
ZaakType,
)
from open_inwoner.openzaak.cases import resolve_status
from open_inwoner.openzaak.clients import (
CatalogiClient,
ZakenClient,
build_catalogi_client,
build_zaken_client,
)
from open_inwoner.openzaak.documents import fetch_single_information_object_url
from open_inwoner.openzaak.clients import CatalogiClient, ZakenClient
from open_inwoner.openzaak.documents import fetch_single_information_object_from_url
from open_inwoner.openzaak.models import (
OpenZaakConfig,
UserCaseInfoObjectNotification,
Expand Down Expand Up @@ -65,14 +60,6 @@ def handle_zaken_notification(notification: Notification):
resources = ("status", "zaakinformatieobject")
r = notification.resource # short alias for logging

client = build_zaken_client()
if not client:
log_system_action(
f"ignored {r} notification: cannot build Zaken API client for case {case_url}",
log_level=logging.ERROR,
)
return

if notification.resource not in resources:
log_system_action(
f"ignored {r} notification: resource is not "
Expand All @@ -81,9 +68,16 @@ def handle_zaken_notification(notification: Notification):
)
return

try:
api_group = ZGWApiGroupConfig.objects.resolve_group_from_hints(url=case_url)
except ZGWApiGroupConfig.DoesNotExist:
logger.error("No API group defined for case %s", case_url)
return

zaken_client = api_group.zaken_client

# check if we have users that need to be informed about this case
roles = client.fetch_case_roles(case_url)
if not roles:
if not (roles := zaken_client.fetch_case_roles(case_url)):
log_system_action(
f"ignored {r} notification: cannot retrieve rollen for case {case_url}",
# NOTE this used to be logging.ERROR, but as this is also our first call
Expand All @@ -101,17 +95,14 @@ def handle_zaken_notification(notification: Notification):
return

# check if this case is visible
case = client.fetch_case_by_url_no_cache(case_url)
if not case:
if not (case := zaken_client.fetch_case_by_url_no_cache(case_url)):
log_system_action(
f"ignored {r} notification: cannot retrieve case {case_url}",
log_level=logging.ERROR,
)
return

case_type = None
if catalogi_client := build_catalogi_client():
case_type = catalogi_client.fetch_single_case_type(case.zaaktype)
case_type = api_group.catalogi_client.fetch_single_case_type(case.zaaktype)

if not case_type:
log_system_action(
Expand All @@ -131,9 +122,11 @@ def handle_zaken_notification(notification: Notification):
return

if notification.resource == "status":
_handle_status_notification(notification, case, inform_users)
_handle_status_notification(notification, case, inform_users, api_group)
elif notification.resource == "zaakinformatieobject":
_handle_zaakinformatieobject_notification(notification, case, inform_users)
_handle_zaakinformatieobject_notification(
notification, case, inform_users, api_group
)
else:
raise NotImplementedError("programmer error in earlier resource filter")

Expand All @@ -142,18 +135,18 @@ def send_case_update_email(
user: User,
case: Zaak,
template_name: str,
api_group: ZGWApiGroupConfig,
status: Status | None = None,
extra_context: dict = None,
):
group = ZGWApiGroupConfig.objects.resolve_group_from_hints(url=case.url)

"""
send the actual mail
"""
case_detail_url = build_absolute_url(
reverse(
"cases:case_detail",
kwargs={"object_id": str(case.uuid), "api_group_id": group.id},
kwargs={"object_id": str(case.uuid), "api_group_id": api_group.id},
)
)

Expand Down Expand Up @@ -197,23 +190,18 @@ def _wrap_join(iter, glue="") -> str:
# Helper functions for ZaakInformatieObject notifications
#
def _handle_zaakinformatieobject_notification(
notification: Notification, case: Zaak, inform_users
notification: Notification,
case: Zaak,
inform_users: list[User],
api_group: ZGWApiGroupConfig,
):
oz_config = OpenZaakConfig.get_solo()
oz_config = api_group.open_zaak_config
r = notification.resource # short alias for logging

client = build_zaken_client()
if not client:
log_system_action(
f"ignored {r} notification: cannot build Zaken API client for case {case.url}",
log_level=logging.ERROR,
)
return

# check if this is a zaakinformatieobject we want to inform on
ziobj_url = notification.resource_url

ziobj = client.fetch_single_case_information_object(ziobj_url)
ziobj = api_group.zaken_client.fetch_single_case_information_object(ziobj_url)

if not ziobj:
log_system_action(
Expand All @@ -222,7 +210,9 @@ def _handle_zaakinformatieobject_notification(
)
return

info_object = fetch_single_information_object_url(ziobj.informatieobject)
info_object = fetch_single_information_object_from_url(
ziobj.informatieobject, api_group=api_group
)
if not info_object:
log_system_action(
f"ignored {r} notification: cannot retrieve informatieobject "
Expand Down Expand Up @@ -267,11 +257,14 @@ def _handle_zaakinformatieobject_notification(
log_level=logging.INFO,
)
for user in inform_users:
_handle_zaakinformatieobject_update(user, case, ziobj)
_handle_zaakinformatieobject_update(user, case, ziobj, api_group)


def _handle_zaakinformatieobject_update(
user: User, case: Zaak, zaak_info_object: ZaakInformatieObject
user: User,
case: Zaak,
zaak_info_object: ZaakInformatieObject,
api_group: ZGWApiGroupConfig,
):
template_name = "case_document_notification"

Expand Down Expand Up @@ -310,7 +303,7 @@ def _handle_zaakinformatieobject_update(
)
return

send_case_update_email(user, case, template_name)
send_case_update_email(user, case, template_name, api_group=api_group)
note.mark_sent()

log_system_action(
Expand Down Expand Up @@ -511,27 +504,14 @@ def _handle_status_notification(
notification: Notification,
case: Zaak,
inform_users: list[User],
api_group: ZGWApiGroupConfig,
):
"""
Check status notification settings of user and case-related objects/configs
"""
oz_config = OpenZaakConfig.get_solo()

catalogi_client = build_catalogi_client()
if not catalogi_client:
log_system_action(
f"ignored {notification.resource} notification for {case.url}: cannot create Catalogi API client",
log_level=logging.ERROR,
)
return None

zaken_client = build_zaken_client()
if not zaken_client:
log_system_action(
f"ignored {notification.resource} notification for {case.url}: cannot create Zaken API client",
log_level=logging.ERROR,
)
return
oz_config = api_group.open_zaak_config
catalogi_client = api_group.catalogi_client
zaken_client = api_group.zaken_client

if not (status_history := _check_status_history(notification, case, zaken_client)):
return
Expand Down Expand Up @@ -568,14 +548,15 @@ def _handle_status_notification(
log_level=logging.INFO,
)
# TODO: replace with notify_about_status_update(...args, method: Callable)
_handle_status_update(user, case, status, status_type_config)
_handle_status_update(user, case, status, status_type_config, api_group)


def _handle_status_update(
user: User,
case: Zaak,
status: Status,
status_type_config: ZaakTypeStatusTypeConfig,
api_group: ZGWApiGroupConfig,
):
# choose template
if status_type_config.action_required:
Expand Down Expand Up @@ -611,7 +592,9 @@ def _handle_status_update(
)
return

send_case_update_email(user, case, template_name, status=status)
send_case_update_email(
user, case, template_name, api_group=api_group, status=status
)
note.mark_sent()

log_system_action(
Expand Down
Loading

0 comments on commit 6dbabce

Please sign in to comment.