Skip to content

Commit

Permalink
✨ [#4606] Implement dynamically resolving the document type
Browse files Browse the repository at this point in the history
The specified document type is now dynamically resolved within the
case type of the created case, using the submissions completion
time to determine the correct version to use.

The legacy configuration options now emit deprecation warnings, but will
still be supported for the foreseeable future.
  • Loading branch information
sergei-maertens committed Nov 14, 2024
1 parent bf5b791 commit b78d73c
Show file tree
Hide file tree
Showing 4 changed files with 921 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/openforms/contrib/zgw/clients/catalogi.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def find_informatieobjecttypen(
catalogus: str,
description: str,
valid_on: date | None = None,
within_casetype: str = "", # URL reference of a case type
) -> list[InformatieObjectType] | None:
"""
Look up an informatieobjecttype within the specified catalogue.
Expand Down Expand Up @@ -299,6 +300,19 @@ def find_informatieobjecttypen(
f"'{description}'. Version (date) ranges may not overlap."
)

if within_casetype:
# Filter down the options to those present in the requested case type
case_type_response = self.get(within_casetype)
case_type_response.raise_for_status()
case_type: CaseType = case_type_response.json()
case_type_document_types = case_type.get("informatieobjecttypen", [])

all_versions = [
version
for version in all_versions
if version["url"] in case_type_document_types
]

return all_versions

def list_statustypen(self, zaaktype: str) -> list[dict]:
Expand Down
62 changes: 59 additions & 3 deletions src/openforms/registrations/contrib/zgw_apis/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import warnings
from datetime import datetime
from functools import partial, wraps
from typing import Any, TypedDict
Expand Down Expand Up @@ -113,7 +114,7 @@ def _resolve_case_type(
catalogue: CatalogueOption,
identification: str,
submission_completed: datetime,
):
) -> str:
version_valid_on = datetime_in_amsterdam(submission_completed).date()

catalogus = catalogi_client.find_catalogus(**catalogue)
Expand All @@ -136,6 +137,37 @@ def _resolve_case_type(
return version["url"]


def _resolve_document_type(
catalogi_client: CatalogiClient,
catalogue: CatalogueOption,
zaaktype_url: str,
description: str,
submission_completed: datetime,
):
version_valid_on = datetime_in_amsterdam(submission_completed).date()

catalogus = catalogi_client.find_catalogus(**catalogue)
if catalogus is None:
raise RuntimeError(f"Could not resolve catalogue {catalogue}")
versions = catalogi_client.find_informatieobjecttypen(
catalogus=catalogus["url"],
description=description,
valid_on=version_valid_on,
within_casetype=zaaktype_url,
)
if versions is None:
raise RuntimeError(
"Could not find a document type with description "
f"'{description}' in the case type that is valid on "
f"{version_valid_on.isoformat()}."
)

# the client enforces there's a single version returned when a valid_on date is
# passed.
version = versions[0]
return version["url"]


@register("zgw-create-zaak")
class ZGWRegistration(BasePlugin[RegistrationOptions]):
verbose_name = _("ZGW API's")
Expand Down Expand Up @@ -219,6 +251,11 @@ def pre_register_submission(
submission.completed_on,
)
else:
warnings.warn(
"Using the zaaktype URL option is deprecated and will be removed in "
"Open Forms 4.0.",
DeprecationWarning,
)
zaaktype_url = options["zaaktype"]

with get_zaken_client(zgw) as zaken_client:
Expand Down Expand Up @@ -295,9 +332,28 @@ def register_submission(
get_zaken_client(zgw) as zaken_client,
get_catalogi_client(zgw) as catalogi_client,
):
# resolve (default) document type to use
if document_type_description := options["document_type_description"]:
catalogue = options.get("catalogue")
assert catalogue is not None # enforced by validation
informatieobjecttype_url = _resolve_document_type(
catalogi_client,
catalogue=catalogue,
zaaktype_url=zaak["zaaktype"],
description=document_type_description,
submission_completed=submission.completed_on,
)
else:
warnings.warn(
"Using the informatieobjecttype URL option is deprecated and will "
"be removed in Open Forms 4.0.",
DeprecationWarning,
)
informatieobjecttype_url = options["informatieobjecttype"]

# Upload the summary PDF
pdf_options: DocumentOptions = {
"informatieobjecttype": options["informatieobjecttype"],
"informatieobjecttype": informatieobjecttype_url,
"organisatie_rsin": options["organisatie_rsin"],
"auteur": options["auteur"],
"doc_vertrouwelijkheidaanduiding": options[
Expand Down Expand Up @@ -387,7 +443,7 @@ def register_submission(
for attachment in submission.attachments:
# collect attributes of the attachment and add them to the configuration
# attribute names conform to the Documenten API specification
iot = attachment.informatieobjecttype or options["informatieobjecttype"]
iot = attachment.informatieobjecttype or informatieobjecttype_url
bronorganisatie = (
attachment.bronorganisatie or options["organisatie_rsin"]
)
Expand Down
Loading

0 comments on commit b78d73c

Please sign in to comment.