Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Jan 9, 2024
1 parent 620804c commit 4e231f0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/open_inwoner/accounts/views/contactmoments.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
fetch_klantcontactmomenten,
get_fetch_parameters,
)
from open_inwoner.kvk.branches import get_kvk_branch_number
from open_inwoner.utils.views import CommonPageMixin


Expand Down Expand Up @@ -106,7 +107,8 @@ def get_anchors(self) -> list:

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
kcms = fetch_klantcontactmomenten(**get_fetch_parameters(self.request.user))
branch_number = get_kvk_branch_number(self.request.session)
kcms = fetch_klantcontactmomenten(**get_fetch_parameters(self.request.user, self.request.session, use_vestigingsnummer=True))
ctx["contactmomenten"] = [self.get_kcm_data(kcm) for kcm in kcms]
return ctx

Expand Down
26 changes: 19 additions & 7 deletions src/open_inwoner/openklant/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from zds_client import ClientError
from zgw_consumers.api_models.base import factory
from zgw_consumers.service import get_paginated_results

from open_inwoner.kvk.branches import get_kvk_branch_number
from open_inwoner.accounts.models import User
from open_inwoner.openklant.api_models import (
ContactMoment,
Expand All @@ -22,15 +22,15 @@


def fetch_klantcontactmomenten(
user_bsn: Optional[str] = None, user_kvk_or_rsin: Optional[str] = None
user_bsn: Optional[str] = None, user_kvk_or_rsin: Optional[str] = None, vestigingsnummer: Optional[str] = None,
) -> List[KlantContactMoment]:
if not user_bsn and not user_kvk_or_rsin:
return []

if user_bsn:
klanten = _fetch_klanten_for_bsn(user_bsn)
elif user_kvk_or_rsin:
klanten = _fetch_klanten_for_kvk_or_rsin(user_kvk_or_rsin)
klanten = _fetch_klanten_for_kvk_or_rsin(user_kvk_or_rsin, vestigingsnummer=vestigingsnummer)

if klanten is None:
return []
Expand Down Expand Up @@ -100,18 +100,25 @@ def _fetch_klanten_for_bsn(user_bsn: str, *, client=None) -> List[Klant]:


def _fetch_klanten_for_kvk_or_rsin(
user_kvk_or_rsin: str, *, client=None
user_kvk_or_rsin: str, *, vestigingsnummer=None, client=None
) -> List[Klant]:
client = client or build_client("klanten")
if client is None:
return []

params = {"subjectNietNatuurlijkPersoon__innNnpId": user_kvk_or_rsin}

if vestigingsnummer:
params = {
"subjectVestiging__vestigingsNummer": vestigingsnummer,
}

try:
response = get_paginated_results(
client,
"klant",
request_kwargs={
"params": {"subjectNietNatuurlijkPersoon__innNnpId": user_kvk_or_rsin}
"params": params
},
)
except (RequestException, ClientError) as e:
Expand Down Expand Up @@ -266,16 +273,21 @@ def create_contactmoment(
return contactmoment


def get_fetch_parameters(user: User) -> dict:
def get_fetch_parameters(user: User, session=None, use_vestigingsnummer: bool = False) -> dict:
"""
Determine the parameters used to perform Klanten/Contactmomenten fetches
"""
# TODO use request instead of user
if user.bsn:
return {"user_bsn": user.bsn}
elif user.kvk:
kvk_or_rsin = user.kvk
config = OpenKlantConfig.get_solo()
if config.use_rsin_for_innNnpId_query_parameter:
kvk_or_rsin = user.rsin
return {"user_kvk_or_rsin": kvk_or_rsin}

parameters = {"user_kvk_or_rsin": kvk_or_rsin}
if use_vestigingsnummer:
vestigingsnummer=get_kvk_branch_number(self.request.session)
return parameters
return {}

0 comments on commit 4e231f0

Please sign in to comment.