Skip to content

Commit 3c0d245

Browse files
[#239] Updated Serializer
1 parent fe1f646 commit 3c0d245

File tree

2 files changed

+235
-3
lines changed

2 files changed

+235
-3
lines changed

src/openklant/components/klantinteracties/api/serializers/partijen.py

+35-3
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ class PartijIdentificatorSerializer(
380380
NestedGegevensGroepMixin, serializers.HyperlinkedModelSerializer
381381
):
382382
identificeerde_partij = PartijForeignKeySerializer(
383-
required=True,
383+
required=False,
384384
allow_null=True,
385385
help_text=_("Partij-identificator die hoorde bij een partij."),
386386
source="partij",
@@ -443,14 +443,23 @@ def validate(self, attrs):
443443

444444
return super().validate(attrs)
445445

446+
def check_partij(self, partij):
447+
if not partij:
448+
raise serializers.ValidationError(
449+
{"identificeerdePartij": _("Dit veld is vereist.")},
450+
code="required",
451+
)
452+
446453
@handle_db_exceptions
447454
@transaction.atomic
448455
def update(self, instance, validated_data):
456+
self.check_partij(validated_data["partij"])
449457
return super().update(instance, validated_data)
450458

451459
@handle_db_exceptions
452460
@transaction.atomic
453461
def create(self, validated_data):
462+
self.check_partij(validated_data["partij"])
454463
return super().create(validated_data)
455464

456465

@@ -515,8 +524,9 @@ class PartijSerializer(NestedGegevensGroepMixin, PolymorphicSerializer):
515524
vertegenwoordigden = serializers.SerializerMethodField(
516525
help_text=_("Partijen die een andere partijen vertegenwoordigden."),
517526
)
518-
partij_identificatoren = PartijIdentificatorForeignkeySerializer(
519-
read_only=True,
527+
partij_identificatoren = PartijIdentificatorSerializer(
528+
required=False,
529+
allow_null=True,
520530
many=True,
521531
source="partijidentificator_set",
522532
help_text=_("Partij-identificatoren die hoorde bij deze partij."),
@@ -760,6 +770,7 @@ def create(self, validated_data):
760770
partij_identificatie = validated_data.pop("partij_identificatie", None)
761771
digitale_adressen = validated_data.pop("digitaaladres_set")
762772
rekeningnummers = validated_data.pop("rekeningnummer_set")
773+
partij_identificatoren = validated_data.pop("partijidentificator_set", None)
763774

764775
if voorkeurs_digitaal_adres := validated_data.pop(
765776
"voorkeurs_digitaal_adres", None
@@ -837,6 +848,27 @@ def create(self, validated_data):
837848
rekeningnummer.partij = partij
838849
rekeningnummer.save()
839850

851+
if partij_identificatoren:
852+
for partij_identificator in partij_identificatoren:
853+
if partij_identificator.get("partij", ""):
854+
raise serializers.ValidationError(
855+
{
856+
"partijIdentificatoren.identificeerdePartij": _(
857+
"Het veld `identificeerde_partij` wordt automatisch ingesteld en hoeft niet te worden opgegeven."
858+
)
859+
},
860+
code="invalid",
861+
)
862+
partij_identificator["identificeerde_partij"] = {
863+
"uuid": str(partij.uuid)
864+
}
865+
partij_identificator_serializer = PartijIdentificatorSerializer(
866+
data=partij_identificator
867+
)
868+
partij_identificator_serializer.is_valid(raise_exception=True)
869+
partij_identificator_serializer.create(
870+
partij_identificator_serializer.validated_data
871+
)
840872
return partij
841873

842874

src/openklant/components/klantinteracties/api/tests/test_partijen.py

+200
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,206 @@ def _get_detail_url(partij: Partij) -> str:
21462146
received_adressen[0]["url"], f"http://testserver{expected_url}"
21472147
)
21482148

2149+
def test_get_partij_and_partij_identificatoren(self):
2150+
partij = PartijFactory.create()
2151+
partij_identificator = PartijIdentificatorFactory.create(
2152+
partij=partij,
2153+
partij_identificator_code_objecttype="natuurlijk_persoon",
2154+
partij_identificator_code_soort_object_id="bsn",
2155+
partij_identificator_object_id="296648875",
2156+
partij_identificator_code_register="brp",
2157+
)
2158+
detail_url = reverse(
2159+
"klantinteracties:partij-detail", kwargs={"uuid": str(partij.uuid)}
2160+
)
2161+
2162+
response = self.client.get(detail_url)
2163+
2164+
self.assertEqual(response.status_code, status.HTTP_200_OK)
2165+
data = response.json()
2166+
url = "http://testserver/klantinteracties/api/v1"
2167+
self.assertEqual(
2168+
data["partijIdentificatoren"],
2169+
[
2170+
{
2171+
"uuid": str(partij_identificator.uuid),
2172+
"url": f"{url}/partij-identificatoren/{str(partij_identificator.uuid)}",
2173+
"identificeerdePartij": {
2174+
"uuid": str(partij.uuid),
2175+
"url": f"{url}/partijen/{str(partij.uuid)}",
2176+
},
2177+
"anderePartijIdentificator": partij_identificator.andere_partij_identificator,
2178+
"partijIdentificator": {
2179+
"codeObjecttype": partij_identificator.partij_identificator_code_objecttype,
2180+
"codeSoortObjectId": partij_identificator.partij_identificator_code_soort_object_id,
2181+
"objectId": partij_identificator.partij_identificator_object_id,
2182+
"codeRegister": partij_identificator.partij_identificator_code_register,
2183+
},
2184+
"subIdentificatorVan": partij_identificator.sub_identificator_van,
2185+
}
2186+
],
2187+
)
2188+
2189+
def test_create_partij_with_null_partij_identificator(self):
2190+
digitaal_adres = DigitaalAdresFactory.create()
2191+
rekeningnummer = RekeningnummerFactory.create()
2192+
list_url = reverse("klantinteracties:partij-list")
2193+
data = {
2194+
"digitaleAdressen": [{"uuid": str(digitaal_adres.uuid)}],
2195+
"voorkeursDigitaalAdres": {"uuid": str(digitaal_adres.uuid)},
2196+
"rekeningnummers": [{"uuid": str(rekeningnummer.uuid)}],
2197+
"voorkeursRekeningnummer": {"uuid": str(rekeningnummer.uuid)},
2198+
"soortPartij": "persoon",
2199+
"indicatieActief": True,
2200+
}
2201+
self.assertEqual(Partij.objects.all().count(), 0)
2202+
2203+
with self.subTest("key_not_specified"):
2204+
self.assertFalse("partijIdentificatoren" in data)
2205+
response = self.client.post(list_url, data)
2206+
response_data = response.json()
2207+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2208+
self.assertEqual(response_data["partijIdentificatoren"], [])
2209+
partij = Partij.objects.get(uuid=str(response_data["uuid"]))
2210+
self.assertEqual(partij.partijidentificator_set.count(), 0)
2211+
self.assertEqual(Partij.objects.all().count(), 1)
2212+
2213+
with self.subTest("null_value"):
2214+
data["partijIdentificatoren"] = None
2215+
response = self.client.post(list_url, data)
2216+
response_data = response.json()
2217+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2218+
self.assertEqual(response_data["partijIdentificatoren"], [])
2219+
partij = Partij.objects.get(uuid=str(response_data["uuid"]))
2220+
self.assertEqual(partij.partijidentificator_set.count(), 0)
2221+
self.assertEqual(Partij.objects.all().count(), 2)
2222+
2223+
with self.subTest("empty_list_value"):
2224+
data["partijIdentificatoren"] = []
2225+
response = self.client.post(list_url, data)
2226+
response_data = response.json()
2227+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2228+
self.assertEqual(response_data["partijIdentificatoren"], [])
2229+
partij = Partij.objects.get(uuid=str(response_data["uuid"]))
2230+
self.assertEqual(partij.partijidentificator_set.count(), 0)
2231+
self.assertEqual(Partij.objects.all().count(), 3)
2232+
2233+
def test_create_partij_with_new_partij_identificator(self):
2234+
digitaal_adres = DigitaalAdresFactory.create()
2235+
rekeningnummer = RekeningnummerFactory.create()
2236+
list_url = reverse("klantinteracties:partij-list")
2237+
data = {
2238+
"digitaleAdressen": [{"uuid": str(digitaal_adres.uuid)}],
2239+
"voorkeursDigitaalAdres": {"uuid": str(digitaal_adres.uuid)},
2240+
"rekeningnummers": [{"uuid": str(rekeningnummer.uuid)}],
2241+
"partijIdentificatoren": [
2242+
{
2243+
"anderePartijIdentificator": "anderePartijIdentificator",
2244+
"partijIdentificator": {
2245+
"codeObjecttype": "natuurlijk_persoon",
2246+
"codeSoortObjectId": "bsn",
2247+
"objectId": "296648875",
2248+
"codeRegister": "brp",
2249+
},
2250+
}
2251+
],
2252+
"voorkeursRekeningnummer": {"uuid": str(rekeningnummer.uuid)},
2253+
"soortPartij": "persoon",
2254+
"indicatieActief": True,
2255+
"partijIdentificatie": {
2256+
"contactnaam": {
2257+
"voorletters": "P",
2258+
"voornaam": "Phil",
2259+
"voorvoegselAchternaam": "",
2260+
"achternaam": "Bozeman",
2261+
}
2262+
},
2263+
}
2264+
2265+
response = self.client.post(list_url, data)
2266+
response_data = response.json()
2267+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2268+
self.assertEqual(Partij.objects.all().count(), 1)
2269+
self.assertEqual(PartijIdentificator.objects.all().count(), 1)
2270+
2271+
partij = Partij.objects.get(uuid=response_data["uuid"])
2272+
self.assertEqual(partij.partijidentificator_set.count(), 1)
2273+
self.assertEqual(len(response_data["partijIdentificatoren"]), 1)
2274+
2275+
partij_identificator = partij.partijidentificator_set.get()
2276+
partij_identificator_dict = response_data["partijIdentificatoren"][0]
2277+
self.assertEqual(
2278+
partij_identificator_dict["uuid"],
2279+
str(partij_identificator.uuid),
2280+
)
2281+
self.assertEqual(
2282+
partij_identificator_dict["identificeerdePartij"]["uuid"],
2283+
str(partij_identificator.partij.uuid),
2284+
)
2285+
self.assertEqual(
2286+
partij_identificator_dict["partijIdentificator"],
2287+
{
2288+
"codeObjecttype": "natuurlijk_persoon",
2289+
"codeSoortObjectId": "bsn",
2290+
"objectId": "296648875",
2291+
"codeRegister": "brp",
2292+
},
2293+
)
2294+
self.assertEqual(
2295+
partij_identificator_dict["subIdentificatorVan"],
2296+
partij_identificator.sub_identificator_van,
2297+
)
2298+
2299+
def test_invalid_create_partij_with_partij_identificator(self):
2300+
digitaal_adres = DigitaalAdresFactory.create()
2301+
rekeningnummer = RekeningnummerFactory.create()
2302+
list_url = reverse("klantinteracties:partij-list")
2303+
partij = PartijFactory.create()
2304+
self.assertEqual(Partij.objects.all().count(), 1)
2305+
2306+
# identificeerdePartij' must not be selected.
2307+
data = {
2308+
"digitaleAdressen": [{"uuid": str(digitaal_adres.uuid)}],
2309+
"voorkeursDigitaalAdres": {"uuid": str(digitaal_adres.uuid)},
2310+
"rekeningnummers": [{"uuid": str(rekeningnummer.uuid)}],
2311+
"partijIdentificatoren": [
2312+
{
2313+
"identificeerdePartij": {"uuid": str(partij.uuid)},
2314+
"anderePartijIdentificator": "anderePartijIdentificator",
2315+
"partijIdentificator": {
2316+
"codeObjecttype": "natuurlijk_persoon",
2317+
"codeSoortObjectId": "bsn",
2318+
"objectId": "296648875",
2319+
"codeRegister": "brp",
2320+
},
2321+
}
2322+
],
2323+
"voorkeursRekeningnummer": {"uuid": str(rekeningnummer.uuid)},
2324+
"soortPartij": "persoon",
2325+
"indicatieActief": True,
2326+
"partijIdentificatie": {
2327+
"contactnaam": {
2328+
"voorletters": "P",
2329+
"voornaam": "Phil",
2330+
"voorvoegselAchternaam": "",
2331+
"achternaam": "Bozeman",
2332+
}
2333+
},
2334+
}
2335+
2336+
response = self.client.post(list_url, data)
2337+
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
2338+
error = get_validation_errors(
2339+
response, "partijIdentificatoren.identificeerdePartij"
2340+
)
2341+
self.assertEqual(error["code"], "invalid")
2342+
self.assertEqual(
2343+
error["reason"],
2344+
"Het veld `identificeerde_partij` wordt automatisch ingesteld en hoeft niet te worden opgegeven.",
2345+
)
2346+
2347+
self.assertEqual(Partij.objects.all().count(), 1)
2348+
21492349

21502350
class PartijIdentificatorTests(APITestCase):
21512351
def test_list_partij_identificator(self):

0 commit comments

Comments
 (0)