From 78e2882123621718a23c0fdf9dc1b4faab771017 Mon Sep 17 00:00:00 2001 From: damm89 Date: Mon, 11 Mar 2024 15:34:18 +0100 Subject: [PATCH] :sparkles: Upset zaakeigenschapwaarde instead of always create --- .../work_units/zgw/tasks/zaak_relations.py | 38 ++++++--- .../zgw/tests/test_eigenschappen.py | 84 ++++++++++++++++++- 2 files changed, 111 insertions(+), 11 deletions(-) diff --git a/src/bptl/work_units/zgw/tasks/zaak_relations.py b/src/bptl/work_units/zgw/tasks/zaak_relations.py index 8ee91cdc..8c7b9094 100644 --- a/src/bptl/work_units/zgw/tasks/zaak_relations.py +++ b/src/bptl/work_units/zgw/tasks/zaak_relations.py @@ -225,16 +225,34 @@ def perform(self) -> dict: logger.info("Eigenschap '%s' did not exist on the zaaktype, aborting.") return {} - zrc_client.create( - "zaakeigenschap", - { - "zaak": zaak_url, - "eigenschap": eigenschap_url, - "waarde": waarde, - }, - zaak_uuid=zaak_uuid, - request_kwargs={"headers": get_nlx_headers(variables)}, - ) + # Now make sure eigenschap doesnt already exist + zaak = zrc_client.retrieve("zaak", uuid=zaak_uuid) + zaakeigenschappen = { + zei["naam"]: zei + for zei in zrc_client.list("zaakeigenschap", zaak_uuid=zaak_uuid) + } + if zei := zaakeigenschappen.get(naam, None): + zrc_client.partial_update( + "zaakeigenschap", + { + "waarde": waarde, + }, + uuid=zei["uuid"], + zaak_uuid=zaak_uuid, + request_kwargs={"headers": get_nlx_headers(variables)}, + ) + + else: + zrc_client.create( + "zaakeigenschap", + { + "zaak": zaak_url, + "eigenschap": eigenschap_url, + "waarde": waarde, + }, + zaak_uuid=zaak_uuid, + request_kwargs={"headers": get_nlx_headers(variables)}, + ) return {} diff --git a/src/bptl/work_units/zgw/tests/test_eigenschappen.py b/src/bptl/work_units/zgw/tests/test_eigenschappen.py index e1bb310c..1cc137f8 100644 --- a/src/bptl/work_units/zgw/tests/test_eigenschappen.py +++ b/src/bptl/work_units/zgw/tests/test_eigenschappen.py @@ -1,3 +1,5 @@ +from copy import deepcopy + from django.test import TestCase import requests_mock @@ -34,7 +36,7 @@ def _get_datum_eigenschap(naam: str) -> dict: @requests_mock.Mocker(real_http=False) -class CreateDocumentRelationTaskTests(TestCase): +class CreateEigenschapRelationTaskTests(TestCase): @classmethod def setUpTestData(cls): super().setUpTestData() @@ -97,6 +99,11 @@ def test_relate_eigenschap(self, m): "results": [_get_datum_eigenschap("einde")], }, ) + m.get( + f"{ZAAK}/zaakeigenschappen", + status_code=200, + json=[], + ) m.post( f"{ZAAK}/zaakeigenschappen", status_code=201, @@ -121,6 +128,81 @@ def test_relate_eigenschap(self, m): "waarde": "2020-05-01", }, ) + self.assertEqual( + m.last_request.method, + "POST", + ) + + def test_relate_eigenschap_update_eigenschap(self, m): + mock_service_oas_get(m, ZRC_URL, "zrc") + mock_service_oas_get(m, ZTC_URL, "ztc") + + m.get(ZAAK, json={"zaaktype": ZAAKTYPE}) + # https://catalogi-api.vng.cloud/api/v1/schema/#operation/eigenschap_list + m.get( + f"{ZTC_URL}eigenschappen?zaaktype={ZAAKTYPE}", + json={ + "count": 2, + "next": f"{ZTC_URL}eigenschappen?zaaktype={ZAAKTYPE}&page=2", + "previous": None, + "results": [_get_datum_eigenschap("start")], + }, + ) + m.get( + f"{ZTC_URL}eigenschappen?zaaktype={ZAAKTYPE}&page=2", + json={ + "count": 2, + "next": None, + "previous": f"{ZTC_URL}eigenschappen?zaaktype={ZAAKTYPE}&page=1", + "results": [_get_datum_eigenschap("einde")], + }, + ) + m.get( + f"{ZAAK}/zaakeigenschappen", + status_code=200, + json=[ + { + "url": f"{ZAAK}/zaakeigenschappen/1234", + "uuid": "1234", + "zaak": ZAAK, + "eigenschap": f"{ZTC_URL}eigenschappen/start", + "naam": "start", + "waarde": "2020-04-01", + } + ], + ) + m.patch( + f"{ZAAK}/zaakeigenschappen/1234", + status_code=200, + json={ + "url": f"{ZAAK}/zaakeigenschappen/1234", + "uuid": "1234", + "zaak": ZAAK, + "eigenschap": f"{ZTC_URL}eigenschappen/start", + "naam": "start", + "waarde": "2020-05-01", + }, + ) + fetched_task = deepcopy(self.fetched_task) + fetched_task.variables["eigenschap"] = serialize_variable( + { + "naam": "start", + "waarde": "2020-04-01", + } + ) + task = CreateEigenschap(self.fetched_task) + task.perform() + + self.assertEqual( + m.last_request.json(), + { + "waarde": "2020-05-01", + }, + ) + self.assertEqual( + m.last_request.method, + "PATCH", + ) def test_eigenschap_does_not_exist(self, m): mock_service_oas_get(m, ZRC_URL, "zrc")