Skip to content

Commit

Permalink
✨ [#1789] Generate ZaakTypeStatusTypeConfigs using ZGW import
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Oct 9, 2023
1 parent e1c8c8b commit 79cc92d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/open_inwoner/openzaak/management/commands/zgw_import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import_catalog_configs,
import_zaaktype_configs,
import_zaaktype_informatieobjecttype_configs,
import_zaaktype_statustype_configs,
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -42,3 +43,13 @@ def handle(self, *args, **options):
self.stdout.write(f" {c}")

self.stdout.write("")

imported = import_zaaktype_statustype_configs()

count = sum(len(t[1]) for t in imported)

self.stdout.write(f"imported {count} new zaaktype-statustype configs")
for ztc, status_types in sorted(imported, key=lambda t: str(t[0])):
self.stdout.write(str(ztc))
for c in sorted(map(str, status_types)):
self.stdout.write(f" {c}")
84 changes: 83 additions & 1 deletion src/open_inwoner/openzaak/zgw_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@

from django.db import transaction

from zgw_consumers.api_models.catalogi import InformatieObjectType
from zgw_consumers.api_models.catalogi import InformatieObjectType, StatusType

from open_inwoner.openzaak.api_models import ZaakType
from open_inwoner.openzaak.catalog import (
fetch_case_types_by_identification_no_cache,
fetch_catalogs_no_cache,
fetch_single_information_object_type,
fetch_single_status_type,
fetch_zaaktypes_no_cache,
)
from open_inwoner.openzaak.models import (
CatalogusConfig,
ZaakTypeConfig,
ZaakTypeInformatieObjectTypeConfig,
ZaakTypeStatusTypeConfig,
)


Expand Down Expand Up @@ -134,6 +136,18 @@ def import_zaaktype_informatieobjecttype_configs() -> List[
return created


def import_zaaktype_statustype_configs() -> List[Tuple[ZaakTypeConfig, StatusType]]:
"""
generate ZaakTypeInformatieObjectTypeConfigs for all ZaakTypeConfig
"""
created = []
for ztc in ZaakTypeConfig.objects.all():
imported = import_statustype_configs_for_type(ztc)
if imported:
created.append((ztc, imported))
return created


def import_zaaktype_informatieobjecttype_configs_for_type(
ztc: ZaakTypeConfig,
) -> List[ZaakTypeInformatieObjectTypeConfig]:
Expand Down Expand Up @@ -200,3 +214,71 @@ def import_zaaktype_informatieobjecttype_configs_for_type(
)

return create


def import_statustype_configs_for_type(
ztc: ZaakTypeConfig,
) -> List[ZaakTypeInformatieObjectTypeConfig]:
"""
generate ZaakTypeInformatieObjectTypeConfigs for all InformationObjectTypes used by each ZaakTypeConfigs source ZaakTypes
this is a bit complicated because one ZaakTypeConfig can represent multiple ZaakTypes
"""

# grab actual ZaakTypes for this identificatie
zaak_types: List[ZaakType] = get_configurable_zaaktypes_by_identification(
ztc.identificatie, ztc.catalogus_url
)
if not zaak_types:
return []

create = []
update = []

with transaction.atomic():
# map existing config records by url

info_map = {
zaaktype_statustype.statustype_url: zaaktype_statustype
for zaaktype_statustype in ztc.zaaktypestatustypeconfig_set.all()
}

# collect and implicitly de-duplicate informatieobjecttype url's and track which zaaktype used it
info_queue = defaultdict(list)
for zaak_type in zaak_types:
for url in zaak_type.statustypen:
info_queue[url].append(zaak_type)

if info_queue:
# load urls and update/create records
for statustype_url, using_zaak_types in info_queue.items():
status_type = fetch_single_status_type(statustype_url)

zaaktype_statustype = info_map.get(status_type.url)
if zaaktype_statustype:
# we got a record for this, see if we got data to update
for using in using_zaak_types:
# track which zaaktype UUID's are interested in this statustype
if using.uuid not in zaaktype_statustype.zaaktype_uuids:
zaaktype_statustype.zaaktype_uuids.append(using.uuid)
if zaaktype_statustype not in create:
update.append(zaaktype_statustype)
else:
# new record
zaaktype_statustype = ZaakTypeStatusTypeConfig(
zaaktype_config=ztc,
statustype_url=status_type.url,
omschrijving=status_type.omschrijving,
statustekst=status_type.statustekst,
zaaktype_uuids=[zt.uuid for zt in using_zaak_types],
)
create.append(zaaktype_statustype)
# not strictly necessary but let's be accurate
info_map[status_type.uuid] = zaaktype_statustype

if create:
ZaakTypeStatusTypeConfig.objects.bulk_create(create)
if update:
ZaakTypeStatusTypeConfig.objects.bulk_update(update, ["zaaktype_uuids"])

return create

0 comments on commit 79cc92d

Please sign in to comment.