From 13203d17b984688334bf5a2c96ccc494b58091b6 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Thu, 17 Oct 2024 15:05:39 +0200 Subject: [PATCH] :recycle: [#147] Make Betrokkene and OnderwerpObject optional in convenience endpoint because it should be possible to create e.g.: - a KlantContact with a Betrokkene but no OnderwerpObject - a KlantContact with an OnderwerpObject but no Betrokkene --- .../api/serializers/klantcontacten.py | 40 ++++++++------- .../api/tests/test_klantcontacten.py | 51 +++++++++++++++++++ .../components/klantinteracties/openapi.yaml | 2 - 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/src/openklant/components/klantinteracties/api/serializers/klantcontacten.py b/src/openklant/components/klantinteracties/api/serializers/klantcontacten.py index 88ae8a19..1228bb88 100644 --- a/src/openklant/components/klantinteracties/api/serializers/klantcontacten.py +++ b/src/openklant/components/klantinteracties/api/serializers/klantcontacten.py @@ -555,8 +555,8 @@ def __init__(self, *args, **kwargs): class KlantContactBetrokkeneOnderwerpObjectSerializer(serializers.Serializer): klantcontact = KlantcontactSerializer() - betrokkene = BetrokkeneConvenienceSerializer() - onderwerpobject = OnderwerpobjectConvenienceSerializer() + betrokkene = BetrokkeneConvenienceSerializer(required=False) + onderwerpobject = OnderwerpobjectConvenienceSerializer(required=False) @transaction.atomic def create(self, validated_data): @@ -567,22 +567,26 @@ def create(self, validated_data): klantcontact_data = validated_data["klantcontact"] klantcontact = Klantcontact.objects.create(**klantcontact_data) - betrokkene_data = validated_data["betrokkene"] - betrokkene_data["had_klantcontact"] = {"uuid": str(klantcontact.uuid)} - # TODO for some reason `was_partij` is converted to `partij` by the serializer - betrokkene_data.setdefault("was_partij", betrokkene_data.get("partij", None)) - betrokkene_serializer = BetrokkeneSerializer(data=betrokkene_data) - betrokkene_serializer.is_valid() - betrokkene = betrokkene_serializer.save() - - onderwerpobject_data = validated_data["onderwerpobject"] - onderwerpobject_data["klantcontact"] = {"uuid": str(klantcontact.uuid)} - onderwerpobject_data.setdefault("was_klantcontact", None) - onderwerpobject_serializer = OnderwerpobjectSerializer( - data=onderwerpobject_data - ) - onderwerpobject_serializer.is_valid() - onderwerpobject = onderwerpobject_serializer.save() + betrokkene = None + if betrokkene_data := validated_data.pop("betrokkene", None): + betrokkene_data["had_klantcontact"] = {"uuid": str(klantcontact.uuid)} + # TODO for some reason `was_partij` is converted to `partij` by the serializer + betrokkene_data.setdefault( + "was_partij", betrokkene_data.get("partij", None) + ) + betrokkene_serializer = BetrokkeneSerializer(data=betrokkene_data) + betrokkene_serializer.is_valid() + betrokkene = betrokkene_serializer.save() + + onderwerpobject = None + if onderwerpobject_data := validated_data.pop("onderwerpobject", None): + onderwerpobject_data["klantcontact"] = {"uuid": str(klantcontact.uuid)} + onderwerpobject_data.setdefault("was_klantcontact", None) + onderwerpobject_serializer = OnderwerpobjectSerializer( + data=onderwerpobject_data + ) + onderwerpobject_serializer.is_valid() + onderwerpobject = onderwerpobject_serializer.save() return { "klantcontact": klantcontact, diff --git a/src/openklant/components/klantinteracties/api/tests/test_klantcontacten.py b/src/openklant/components/klantinteracties/api/tests/test_klantcontacten.py index a45b7610..6e2f5a8a 100644 --- a/src/openklant/components/klantinteracties/api/tests/test_klantcontacten.py +++ b/src/openklant/components/klantinteracties/api/tests/test_klantcontacten.py @@ -1976,3 +1976,54 @@ def test_create_onderwerpobject_with_was_klantcontact(self): ) self.assertEqual(onderwerpobject.klantcontact, klantcontact) self.assertEqual(onderwerpobject.was_klantcontact, existing_klantcontact) + + def test_create_without_betrokkene_and_onderwerpobject(self): + del self.post_data["betrokkene"] + del self.post_data["onderwerpobject"] + response = self.client.post(self.url, self.post_data) + + self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) + + klantcontact = Klantcontact.objects.get() + + self.assertFalse(Betrokkene.objects.exists()) + self.assertFalse(Onderwerpobject.objects.exists()) + + klantcontact_url = reverse( + "klantinteracties:klantcontact-detail", + kwargs={"uuid": str(klantcontact.uuid)}, + ) + + klantcontact_url = f"http://testserver{klantcontact_url}" + + data = response.json() + + self.assertEqual( + list(data.keys()), ["klantcontact", "betrokkene", "onderwerpobject"] + ) + + with self.subTest("Klantcontact response data is correct"): + expected_klantcontact = { + "uuid": str(klantcontact.uuid), + "url": klantcontact_url, + "gingOverOnderwerpobjecten": [], + "hadBetrokkenActoren": [], + "hadBetrokkenen": [], + "indicatieContactGelukt": False, + "inhoud": "changed", + "kanaal": "changed", + "leiddeTotInterneTaken": [], + "nummer": "7948723947", + "omvatteBijlagen": [], + "onderwerp": "changed", + "plaatsgevondenOp": "2020-08-24T14:15:22Z", + "taal": "de", + "vertrouwelijk": False, + } + self.assertEqual(data["klantcontact"], expected_klantcontact) + + with self.subTest("Betrokkene is None in response"): + self.assertEqual(data["betrokkene"], None) + + with self.subTest("Onderwerpobject is None in response"): + self.assertEqual(data["onderwerpobject"], None) diff --git a/src/openklant/components/klantinteracties/openapi.yaml b/src/openklant/components/klantinteracties/openapi.yaml index e5cd8fc9..e0dc8bfa 100644 --- a/src/openklant/components/klantinteracties/openapi.yaml +++ b/src/openklant/components/klantinteracties/openapi.yaml @@ -3663,9 +3663,7 @@ components: onderwerpobject: $ref: '#/components/schemas/OnderwerpobjectConvenience' required: - - betrokkene - klantcontact - - onderwerpobject Klantcontact: type: object properties: