Skip to content

Commit

Permalink
Bring create eSuite klant logic in line with spec
Browse files Browse the repository at this point in the history
  • Loading branch information
swrichards committed Jan 9, 2025
1 parent a0173a3 commit ed85115
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 144 deletions.
53 changes: 22 additions & 31 deletions src/open_inwoner/cms/cases/views/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

from django.conf import settings
from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.core.exceptions import (
ImproperlyConfigured,
ObjectDoesNotExist,
PermissionDenied,
)
from django.http import (
Http404,
HttpRequest,
Expand Down Expand Up @@ -994,43 +998,30 @@ def register_by_api(self, form, config: OpenKlantConfig):
except ObjectDoesNotExist:
ztc = None

# TODO
openklant_config = OpenKlantConfig.get_solo()
service = eSuiteKlantenService(config=openklant_config)

if klanten_client := service.client:
klant = service.retrieve_klant(**get_fetch_parameters(self.request))

if klant:
klant = None
try:
service = eSuiteKlantenService(config=openklant_config)
except (ImproperlyConfigured, RuntimeError):
self.log_system_action("could not build client for klanten API")
else:
klant, created = service.get_or_create_klant(
fetch_params=get_fetch_parameters(self.request), user=self.request.user
)
if not klant:
self.log_system_action(
"retrieved klant for user", user=self.request.user
"could not create klant for user", user=self.request.user
)
else:
self.log_system_action(
"could not retrieve klant for user", user=self.request.user
)
data = {
"bronorganisatie": config.register_bronorganisatie_rsin,
"voornaam": self.request.user.first_name,
"voorvoegselAchternaam": self.request.user.infix,
"achternaam": self.request.user.last_name,
"emailadres": self.request.user.email,
"telefoonnummer": self.request.user.phonenumber,
}
# registering klanten won't work in e-Suite as it always pulls from BRP (but try anyway and fallback to appending details to tekst if fails)
klant = service.create_klant(data)

if klant:
if created:
self.log_system_action(
"created klant for basic authenticated user",
(
"created klant for basic authenticated user"
if created
else "retrieved klant for user"
),
user=self.request.user,
)
else:
self.log_system_action(
"could not create klant for user", user=self.request.user
)
else:
self.log_system_action("could not build client for klanten API")

# create contact moment
question = form.cleaned_data["question"]
Expand Down
17 changes: 12 additions & 5 deletions src/open_inwoner/openklant/api_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
from zgw_consumers.api_models.base import ZGWModel


class KlantCreateData(TypedDict):
bronorganisatie: str
voornaam: str
voorvoegselAchternaam: str
achternaam: str
class KlantWritePayload(TypedDict, total=False):
"""The writable fields for en eSuite klant.
Note this excludes the subjectIdentificatie field which eSuite uses to identify
the user and fetch the remaining personal details from the BRP (such as name,
address, and so forth). It is intended to specify what fields can be written for a
_known_ user.
"""

telefoonnummer: str
telefoonnummerAlternatief: str
emailadres: str
toestemmingZaakNotificatiesAlleenDigitaal: bool


@dataclass
Expand All @@ -32,6 +38,7 @@ class Klant(ZGWModel):
telefoonnummer: str = ""
emailadres: str = ""
toestemming_zaak_notificaties_alleen_digitaal: bool | None = None
bedrijfsnaam: str = ""

def get_name_display(self):
return " ".join(
Expand Down
63 changes: 0 additions & 63 deletions src/open_inwoner/openklant/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
Klant,
KlantContactMoment,
KlantContactRol,
KlantCreateData,
ObjectContactMoment,
)
from .models import OpenKlantConfig
Expand All @@ -24,68 +23,6 @@


class KlantenClient(APIClient):
def create_klant(
self,
user_bsn: str | None = None,
user_kvk_or_rsin: str | None = None,
vestigingsnummer: str | None = None,
data: KlantCreateData = None,
) -> Klant | None:
if user_bsn:
return self._create_klant_for_bsn(user_bsn)

if user_kvk_or_rsin:
return self._create_klant_for_kvk_or_rsin(
user_kvk_or_rsin, vestigingsnummer=vestigingsnummer
)

try:
response = self.post("klanten", json=data)
data = get_json_response(response)
except (RequestException, ClientError):
logger.exception("exception while making request")
return

klant = factory(Klant, data)

return klant

def _create_klant_for_bsn(self, user_bsn: str) -> Klant:
payload = {"subjectIdentificatie": {"inpBsn": user_bsn}}

try:
response = self.post("klanten", json=payload)
data = get_json_response(response)
except (RequestException, ClientError):
logger.exception("exception while making request")
return None

klant = factory(Klant, data)

return klant

def _create_klant_for_kvk_or_rsin(
self, user_kvk_or_rsin: str, *, vestigingsnummer=None
) -> list[Klant]:
payload = {"subjectIdentificatie": {"innNnpId": user_kvk_or_rsin}}

if vestigingsnummer:
payload = {"subjectIdentificatie": {"vestigingsNummer": vestigingsnummer}}

try:
response = self.post(
"klanten",
json=payload,
)
data = get_json_response(response)
except (RequestException, ClientError):
logger.exception("exception while making request")
return None

klant = factory(Klant, data)

return klant

def retrieve_klant(
self, user_bsn: str | None = None, user_kvk_or_rsin: str | None = None
) -> Klant | None:
Expand Down
68 changes: 23 additions & 45 deletions src/open_inwoner/openklant/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
Klant,
KlantContactMoment,
KlantContactRol,
KlantCreateData,
KlantWritePayload,
ObjectContactMoment,
)
from open_inwoner.openklant.constants import KlantenServiceType, Status
Expand Down Expand Up @@ -153,7 +153,9 @@ def __init__(self, config: OpenKlantConfig | None = None):
"eSuiteKlantenService instance needs a servivce configuration"
)

self.client = build_zgw_client(service=self.service_config)
self.client = build_zgw_client(
service=self.service_config, client_factory=APIClient
)
if not self.client:
raise RuntimeError("eSuiteKlantenService instance needs a client")

Expand Down Expand Up @@ -232,60 +234,36 @@ def create_klant(
user_bsn: str | None = None,
user_kvk_or_rsin: str | None = None,
vestigingsnummer: str | None = None,
data: KlantCreateData = None,
*,
data: KlantWritePayload | None = None,
) -> Klant | None:
if user_bsn:
return self._create_klant_for_bsn(user_bsn)
if sum(bool(arg) for arg in (user_bsn, user_kvk_or_rsin, vestigingsnummer)) > 1:
raise ValueError("Only one argument can be specified")

if user_kvk_or_rsin:
return self._create_klant_for_kvk_or_rsin(
user_kvk_or_rsin, vestigingsnummer=vestigingsnummer
)
payload = {}

try:
response = self.client.post("klanten", json=data)
data = get_json_response(response)
except (RequestException, ClientError):
logger.exception("exception while making request")
return
# Include writable attributes, if provided
if data:
payload.update(data)

klant = factory(Klant, data)

return klant

def _create_klant_for_bsn(self, user_bsn: str) -> Klant:
payload = {"subjectIdentificatie": {"inpBsn": user_bsn}}
if user_bsn:
payload = payload | {"subjectIdentificatie": {"inpBsn": user_bsn}}
elif user_kvk_or_rsin:
payload = payload | {"subjectIdentificatie": {"innNnpId": user_kvk_or_rsin}}
elif vestigingsnummer:
payload = payload | {
"subjectIdentificatie": {"vestigingsNummer": vestigingsnummer}
}

try:
response = self.client.post("klanten", json=payload)
data = get_json_response(response)
response_data = get_json_response(response)
except (RequestException, ClientError):
logger.exception("exception while making request")
return None

klant = factory(Klant, data)

return klant

def _create_klant_for_kvk_or_rsin(
self, user_kvk_or_rsin: str, *, vestigingsnummer=None
) -> list[Klant]:
payload = {"subjectIdentificatie": {"innNnpId": user_kvk_or_rsin}}
return

if vestigingsnummer:
payload = {"subjectIdentificatie": {"vestigingsNummer": vestigingsnummer}}

try:
response = self.client.post(
"klanten",
json=payload,
)
data = get_json_response(response)
except (RequestException, ClientError):
logger.exception("exception while making request")
return None

klant = factory(Klant, data)
klant = factory(Klant, response_data)

return klant

Expand Down
14 changes: 14 additions & 0 deletions src/open_inwoner/openklant/tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ def __init__(self):
emailadres="[email protected]",
telefoonnummer="0612345678",
toestemmingZaakNotificatiesAlleenDigitaal=False,
voornaam="John",
achternaam="Doe",
voorvoegselAchternaam="van der",
bedrijfsnaam="",
)
self.klant_kvk = generate_oas_component_cached(
"kc",
Expand All @@ -169,6 +173,12 @@ def __init__(self):
emailadres="[email protected]",
telefoonnummer="0687654321",
toestemmingZaakNotificatiesAlleenDigitaal=False,
# TODO: This should really include bedrijfsnaam, but that would require
# updating the schemas
voornaam="",
achternaam="",
voorvoegselAchternaam="",
bedrijfsnaam="AcmeCorp B.V.",
)
self.klant_vestiging = generate_oas_component_cached(
"kc",
Expand All @@ -182,6 +192,10 @@ def __init__(self):
emailadres="[email protected]",
telefoonnummer="0612345678",
toestemmingZaakNotificatiesAlleenDigitaal=False,
voornaam="",
achternaam="",
voorvoegselAchternaam="",
bedrijfsnaam="AcmeCorp B.V.",
)
self.contactmoment = generate_oas_component_cached(
"cmc",
Expand Down
Loading

0 comments on commit ed85115

Please sign in to comment.