Skip to content

Commit

Permalink
Merge pull request #247 from GemeenteUtrecht/feature/upsert-zaakeigen…
Browse files Browse the repository at this point in the history
…schap

✨ Upset zaakeigenschapwaarde instead of always create
  • Loading branch information
damm89 authored Mar 11, 2024
2 parents d08fd86 + 78e2882 commit 7374403
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 11 deletions.
38 changes: 28 additions & 10 deletions src/bptl/work_units/zgw/tasks/zaak_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
84 changes: 83 additions & 1 deletion src/bptl/work_units/zgw/tests/test_eigenschappen.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from copy import deepcopy

from django.test import TestCase

import requests_mock
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand Down

0 comments on commit 7374403

Please sign in to comment.