Skip to content

Commit

Permalink
[#53] Making klantnummer optional and auto-generating klantnummer if …
Browse files Browse the repository at this point in the history
…not provided
  • Loading branch information
alextreme committed Sep 4, 2023
1 parent d481f5f commit fc5996f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/openklant/components/klanten/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ class Meta:
"url": {"lookup_field": "uuid"},
"subject": {"required": False, "validators": [URLValidator()]},
"subject_type": {"validators": [IsImmutableValidator()]},
"klantnummer": {"required": False},
# Disabled for now, should return once logic is implemented
# "geverifieerd": {"validators": [IsImmutableValidator()]},
# Disabled for now, see https://github.com/maykinmedia/open-klant/pull/11#pullrequestreview-805051480
Expand All @@ -315,6 +316,11 @@ def __init__(self, *args, **kwargs):
self.fields[custom_field].help_text
)

def validate_klantnummer(self, value):
if not value:
value = str(Klant.objects.get_next_klantnummer())
return value

def validate(self, attrs):
validated_attrs = super().validate(attrs)
subject = validated_attrs.get("subject", None)
Expand All @@ -331,7 +337,6 @@ def validate(self, attrs):
_("subject or subjectIdentificatie must be provided"),
code="invalid-subject",
)

return validated_attrs

def to_internal_value(self, data):
Expand Down
78 changes: 78 additions & 0 deletions src/openklant/components/klanten/api/tests/test_klant.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,84 @@ def test_create_klant_website_url_optional(self):
self.assertEqual(klant.bronorganisatie, "950428139")
self.assertEqual(klant.website_url, "")

def test_create_klant_website_url_optional(self):
list_url = reverse(Klant)
data = {
"bronorganisatie": "950428139",
"voornaam": "Xavier",
"achternaam": "Jackson",
"emailadres": "test@gmail.com",
"adres": {
"straatnaam": "Keizersgracht",
"huisnummer": "117",
"huisletter": "A",
"postcode": "1015CJ",
"woonplaatsnaam": "test",
"landcode": "1234",
},
"subjectType": KlantType.natuurlijk_persoon,
"subject": SUBJECT,
}

with requests_mock.Mocker() as m:
m.get(SUBJECT, json={})
response = self.client.post(list_url, data)

self.assertEqual(response.status_code, status.HTTP_201_CREATED)

klant = Klant.objects.get()

self.assertEqual(klant.bronorganisatie, "950428139")
self.assertEqual(klant.website_url, "")
self.assertTrue(klant.klantnummer != 0)

def test_create_klant_website_url_duplicate_klantnummer(self):
list_url = reverse(Klant)
data = {
"bronorganisatie": "950428139",
"subjectType": KlantType.natuurlijk_persoon,
"klantnummer": "123",
"subject": SUBJECT,
}

with requests_mock.Mocker() as m:
m.get(SUBJECT, json={})
response = self.client.post(list_url, data)

self.assertEqual(response.status_code, status.HTTP_201_CREATED)

klant = Klant.objects.get()

self.assertEqual(klant.bronorganisatie, "950428139")
self.assertEqual(klant.website_url, "")
self.assertEqual(klant.klantnummer, "123")

with requests_mock.Mocker() as m:
m.get(SUBJECT, json={})
response = self.client.post(list_url, data)

self.assertEqual(response.status_code, 409)
klant = Klant.objects.get()

def test_create_klant_website_url_invalid_klantnummer(self):
list_url = reverse(Klant)
data = {
"bronorganisatie": "950428139",
"subjectType": KlantType.natuurlijk_persoon,
"klantnummer": "123456789",
"subject": SUBJECT,
}

with requests_mock.Mocker() as m:
m.get(SUBJECT, json={})
response = self.client.post(list_url, data)

self.assertEqual(response.status_code, 400)

klanten = Klant.objects.all()

self.assertFalse(klanten)

def test_create_klant_natuurlijkpersoon(self):
list_url = reverse(Klant)
data = {
Expand Down
16 changes: 15 additions & 1 deletion src/openklant/components/klanten/models/klanten.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from vng_api_common.exceptions import Conflict
from vng_api_common.fields import BSNField, RSINField
from vng_api_common.models import APIMixin

from .constants import GeslachtsAanduiding, KlantType, SoortRechtsvorm


class KlantManager(models.Manager):
def get_next_klantnummer(self):
id_max = Klant.objects.all().aggregate(Max("klantnummer"))["id__max"]
return id_max + 1 if id_max else 1


class Klant(APIMixin, models.Model):
uuid = models.UUIDField(
unique=True,
Expand Down Expand Up @@ -88,10 +95,17 @@ class Klant(APIMixin, models.Model):
default=False, help_text=_("Geeft aan of de KLANT wel of niet geverifieerd is.")
)

objects = KlantManager()

class Meta:
verbose_name = "klant"
verbose_name_plural = "klanten"
unique_together = ("bronorganisatie", "klantnummer")

def save(self, *args, **kwargs):
if not self.pk:
if Klant.objects.filter(klantnummer=self.klantnummer):
raise Conflict("Klantnummer bestaat al")
return super().save(*args, **kwargs)

@property
def subject_identificatie(self):
Expand Down

0 comments on commit fc5996f

Please sign in to comment.