Skip to content

Commit

Permalink
♻️ [#147] Make Betrokkene and OnderwerpObject optional in convenience…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
stevenbal committed Oct 18, 2024
1 parent 74b50b2 commit 13203d1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 0 additions & 2 deletions src/openklant/components/klantinteracties/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3663,9 +3663,7 @@ components:
onderwerpobject:
$ref: '#/components/schemas/OnderwerpobjectConvenience'
required:
- betrokkene
- klantcontact
- onderwerpobject
Klantcontact:
type: object
properties:
Expand Down

0 comments on commit 13203d1

Please sign in to comment.