Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eSuite user syncing refactors #1546

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/open_inwoner/accounts/signals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.urls import reverse
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -57,7 +58,7 @@ def update_user_on_login(sender, user, request, *args, **kwargs):


def _update_user_from_openklant2(
user: User, service: OpenKlant2Service, request
user: User, service: OpenKlant2Service, request: None = None
) -> None:
if fetch_params := service.get_fetch_parameters(request=request):
partij, created = service.get_or_create_partij_for_user(
Expand All @@ -68,7 +69,7 @@ def _update_user_from_openklant2(


def _update_user_from_esuite(
user: User, service: eSuiteKlantenService, request
user: User, service: eSuiteKlantenService, request: None = None
) -> None:
if not (fetch_params := service.get_fetch_parameters(request=request)):
return
Expand All @@ -88,6 +89,38 @@ def _update_eherkenning_user_from_kvk_api(user: User):
user.save()


# TODO: Should we also try to fetch pre-existing klant for new user and update?
# The klant could have been created by a different service.
@receiver(post_save, sender=User)
def get_or_create_klant_for_new_user(
sender: type, instance: User, created: bool, **kwargs
) -> None:
if not created:
logger.info("No klanten sync performed because user has just been created")
return

user = instance

# OpenKlant2
try:
service = OpenKlant2Service()
except Exception:
logger.error("OpenKlant2 service failed to build")
else:
_update_user_from_openklant2(
user,
service,
)

# eSuite
try:
service = eSuiteKlantenService()
except Exception:
logger.error("eSuiteKlantenService failed to build")
else:
_update_user_from_esuite(user, service)


@receiver(user_logged_in)
def log_user_login(sender, user, request, *args, **kwargs):
current_path = request.path
Expand Down
74 changes: 0 additions & 74 deletions src/open_inwoner/accounts/views/signals.py

This file was deleted.

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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the name itself (not just the comment) should make clear that this is intended for eSuite: KlantWritePayloadESuite or the like?

"""The writable fields for en eSuite klant.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""The writable fields for en eSuite klant.
"""The writable fields for an 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
Loading
Loading