From 882b022b707d04d4c5a9641eb1c1fe7315a9b762 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Thu, 15 Feb 2024 14:15:21 +0100 Subject: [PATCH 1/3] :sparkles: [#2088] Implement Zaken search for eHerkenning users issue: https://taiga.maykinmedia.nl/project/open-inwoner/issue/2088 --- src/open_inwoner/cms/cases/views/cases.py | 2 +- src/open_inwoner/openzaak/clients.py | 23 ++++++++++++++++++- .../management/commands/zgw_dev_status.py | 2 +- src/open_inwoner/openzaak/utils.py | 23 +++++++++++++++++++ src/open_inwoner/pdc/managers.py | 2 +- src/open_inwoner/search/views.py | 7 +++--- 6 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/open_inwoner/cms/cases/views/cases.py b/src/open_inwoner/cms/cases/views/cases.py index c1e969149e..7414999fc2 100644 --- a/src/open_inwoner/cms/cases/views/cases.py +++ b/src/open_inwoner/cms/cases/views/cases.py @@ -74,7 +74,7 @@ def get_cases(self): else: raw_cases = client.fetch_cases_by_kvk_or_rsin(kvk_or_rsin=kvk_or_rsin) else: - raw_cases = client.fetch_cases(self.request.user.bsn) + raw_cases = client.fetch_cases_by_bsn(self.request.user.bsn) preprocessed_cases = preprocess_data(raw_cases) return preprocessed_cases diff --git a/src/open_inwoner/openzaak/clients.py b/src/open_inwoner/openzaak/clients.py index 7f17c6b173..5ebae816b4 100644 --- a/src/open_inwoner/openzaak/clients.py +++ b/src/open_inwoner/openzaak/clients.py @@ -39,11 +39,32 @@ class ZakenClient(APIClient): + def fetch_cases( + self, + user_bsn=None, + user_kvk_or_rsin=None, + max_requests=4, + identificatie=None, + vestigingsnummer=None, + ): + if user_bsn: + return self.fetch_cases_by_bsn( + user_bsn, max_requests=max_requests, identificatie=identificatie + ) + elif user_kvk_or_rsin: + return self.fetch_cases_by_kvk_or_rsin( + user_kvk_or_rsin, + max_requests=max_requests, + zaak_identificatie=identificatie, + vestigingsnummer=vestigingsnummer, + ) + return [] + @cache_result( "cases:{user_bsn}:{max_requests}:{identificatie}", timeout=settings.CACHE_ZGW_ZAKEN_TIMEOUT, ) - def fetch_cases( + def fetch_cases_by_bsn( self, user_bsn: str, max_requests: Optional[int] = 4, diff --git a/src/open_inwoner/openzaak/management/commands/zgw_dev_status.py b/src/open_inwoner/openzaak/management/commands/zgw_dev_status.py index ce2b2dd81b..25b81d6ff9 100644 --- a/src/open_inwoner/openzaak/management/commands/zgw_dev_status.py +++ b/src/open_inwoner/openzaak/management/commands/zgw_dev_status.py @@ -69,7 +69,7 @@ def handle(self, *args, **options): if not case_uuid: # if no case_ref is supplied display list of cases and some information about each - cases = zaken_client.fetch_cases(user.bsn) + cases = zaken_client.fetch_cases_by_bsn(user.bsn) for case in cases: case_type = catalogi_client.fetch_single_case_type(case.zaaktype) case.zaaktype = case_type diff --git a/src/open_inwoner/openzaak/utils.py b/src/open_inwoner/openzaak/utils.py index 97fc8a64ac..b11f6c1a8a 100644 --- a/src/open_inwoner/openzaak/utils.py +++ b/src/open_inwoner/openzaak/utils.py @@ -3,6 +3,7 @@ from zgw_consumers.api_models.constants import RolTypes, VertrouwelijkheidsAanduidingen +from open_inwoner.kvk.branches import get_kvk_branch_number from open_inwoner.openzaak.api_models import InformatieObject, Rol, Zaak, ZaakType from .models import ( @@ -142,3 +143,25 @@ def translate_single_status(status_text: str) -> str: ) except StatusTranslation.DoesNotExist: return "" + + +def get_fetch_parameters(request) -> dict: + """ + Determine the parameters used to perform ZGW resource fetches + """ + user = request.user + + if user.bsn: + return {"user_bsn": user.bsn} + elif user.kvk: + kvk_or_rsin = user.kvk + config = OpenZaakConfig.get_solo() + if config.fetch_eherkenning_zaken_with_rsin: + kvk_or_rsin = user.rsin + + parameters = {"user_kvk_or_rsin": kvk_or_rsin} + vestigingsnummer = get_kvk_branch_number(request.session) + if vestigingsnummer: + parameters.update({"vestigingsnummer": vestigingsnummer}) + return parameters + return {} diff --git a/src/open_inwoner/pdc/managers.py b/src/open_inwoner/pdc/managers.py index 918b438933..7bd4f2eb72 100644 --- a/src/open_inwoner/pdc/managers.py +++ b/src/open_inwoner/pdc/managers.py @@ -57,7 +57,7 @@ def filter_by_zaken_for_request(self, request): return self.none() if request.user.bsn: - cases = client.fetch_cases(request.user.bsn) + cases = client.fetch_cases_by_bsn(request.user.bsn) elif request.user.kvk: kvk_or_rsin = request.user.kvk config = OpenZaakConfig.get_solo() diff --git a/src/open_inwoner/search/views.py b/src/open_inwoner/search/views.py index 9412dd3d9b..955dc6dcba 100644 --- a/src/open_inwoner/search/views.py +++ b/src/open_inwoner/search/views.py @@ -10,6 +10,8 @@ from open_inwoner.configurations.models import SiteConfiguration from open_inwoner.openzaak.clients import build_client +from open_inwoner.openzaak.models import OpenZaakConfig +from open_inwoner.openzaak.utils import get_fetch_parameters from open_inwoner.utils.mixins import PaginationMixin from open_inwoner.utils.views import CommonPageMixin, LoginMaybeRequiredMixin, LogMixin @@ -62,10 +64,9 @@ def search(self, form): self.log_user_action(user, _("search query: {query}").format(query=query)) # Check if the query exactly matches with a case that belongs to the user - # TODO should be implemented for KVK as well - if hasattr(self.request.user, "bsn"): + if search_params := get_fetch_parameters(self.request): if client := build_client("zaak"): - cases = client.fetch_cases(self.request.user.bsn, identificatie=query) + cases = client.fetch_cases(**search_params, identificatie=query) if cases and len(cases) == 1: return HttpResponseRedirect( reverse( From 0c2649b442d96c08fdc5a77a64ca33fb1d46e01a Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Thu, 15 Feb 2024 14:18:12 +0100 Subject: [PATCH 2/3] :recycle: Use generic fetch_cases for kvk/bsn case fetching to avoid repeating the same logic to determine the fetch parameters --- src/open_inwoner/cms/cases/views/cases.py | 18 +++--------------- src/open_inwoner/pdc/managers.py | 19 +++---------------- 2 files changed, 6 insertions(+), 31 deletions(-) diff --git a/src/open_inwoner/cms/cases/views/cases.py b/src/open_inwoner/cms/cases/views/cases.py index 7414999fc2..ed4abb772b 100644 --- a/src/open_inwoner/cms/cases/views/cases.py +++ b/src/open_inwoner/cms/cases/views/cases.py @@ -6,12 +6,12 @@ from view_breadcrumbs import BaseBreadcrumbMixin from open_inwoner.htmx.mixins import RequiresHtmxMixin -from open_inwoner.kvk.branches import get_kvk_branch_number from open_inwoner.openzaak.cases import preprocess_data from open_inwoner.openzaak.clients import build_client from open_inwoner.openzaak.formapi import fetch_open_submissions from open_inwoner.openzaak.models import OpenZaakConfig from open_inwoner.openzaak.types import UniformCase +from open_inwoner.openzaak.utils import get_fetch_parameters from open_inwoner.utils.mixins import PaginationMixin from open_inwoner.utils.views import CommonPageMixin @@ -61,20 +61,8 @@ def get_cases(self): if client is None: return [] - if self.request.user.kvk: - kvk_or_rsin = self.request.user.kvk - config = OpenZaakConfig.get_solo() - if config.fetch_eherkenning_zaken_with_rsin: - kvk_or_rsin = self.request.user.rsin - vestigingsnummer = get_kvk_branch_number(self.request.session) - if vestigingsnummer: - raw_cases = client.fetch_cases_by_kvk_or_rsin( - kvk_or_rsin=kvk_or_rsin, vestigingsnummer=vestigingsnummer - ) - else: - raw_cases = client.fetch_cases_by_kvk_or_rsin(kvk_or_rsin=kvk_or_rsin) - else: - raw_cases = client.fetch_cases_by_bsn(self.request.user.bsn) + raw_cases = client.fetch_cases(**get_fetch_parameters(self.request)) + preprocessed_cases = preprocess_data(raw_cases) return preprocessed_cases diff --git a/src/open_inwoner/pdc/managers.py b/src/open_inwoner/pdc/managers.py index 7bd4f2eb72..16ae5d049d 100644 --- a/src/open_inwoner/pdc/managers.py +++ b/src/open_inwoner/pdc/managers.py @@ -9,10 +9,10 @@ from open_inwoner.accounts.models import User from open_inwoner.configurations.models import SiteConfiguration -from open_inwoner.kvk.branches import get_kvk_branch_number from open_inwoner.openzaak.api_models import Zaak from open_inwoner.openzaak.clients import build_client -from open_inwoner.openzaak.models import OpenZaakConfig, ZaakTypeConfig +from open_inwoner.openzaak.models import ZaakTypeConfig +from open_inwoner.openzaak.utils import get_fetch_parameters class ProductQueryset(models.QuerySet): @@ -56,20 +56,7 @@ def filter_by_zaken_for_request(self, request): if client is None: return self.none() - if request.user.bsn: - cases = client.fetch_cases_by_bsn(request.user.bsn) - elif request.user.kvk: - kvk_or_rsin = request.user.kvk - config = OpenZaakConfig.get_solo() - if config.fetch_eherkenning_zaken_with_rsin: - kvk_or_rsin = request.user.rsin - vestigingsnummer = get_kvk_branch_number(request.session) - if vestigingsnummer: - cases = client.fetch_cases_by_kvk_or_rsin( - kvk_or_rsin=kvk_or_rsin, vestigingsnummer=vestigingsnummer - ) - else: - cases = client.fetch_cases_by_kvk_or_rsin(kvk_or_rsin=kvk_or_rsin) + cases = client.fetch_cases(**get_fetch_parameters(request)) return self.filter_by_zaken(cases) From 47a73732c2625df7b4de838c92c2b66f58e19f54 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Thu, 15 Feb 2024 16:45:16 +0100 Subject: [PATCH 3/3] :ok_hand: [#2088] PR feedback --- src/open_inwoner/cms/cases/views/cases.py | 4 ++-- src/open_inwoner/openzaak/clients.py | 15 ++++++++++----- src/open_inwoner/openzaak/utils.py | 5 ++++- src/open_inwoner/pdc/managers.py | 4 ++-- src/open_inwoner/search/views.py | 5 ++--- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/open_inwoner/cms/cases/views/cases.py b/src/open_inwoner/cms/cases/views/cases.py index ed4abb772b..81fd4f08a9 100644 --- a/src/open_inwoner/cms/cases/views/cases.py +++ b/src/open_inwoner/cms/cases/views/cases.py @@ -11,7 +11,7 @@ from open_inwoner.openzaak.formapi import fetch_open_submissions from open_inwoner.openzaak.models import OpenZaakConfig from open_inwoner.openzaak.types import UniformCase -from open_inwoner.openzaak.utils import get_fetch_parameters +from open_inwoner.openzaak.utils import get_user_fetch_parameters from open_inwoner.utils.mixins import PaginationMixin from open_inwoner.utils.views import CommonPageMixin @@ -61,7 +61,7 @@ def get_cases(self): if client is None: return [] - raw_cases = client.fetch_cases(**get_fetch_parameters(self.request)) + raw_cases = client.fetch_cases(**get_user_fetch_parameters(self.request)) preprocessed_cases = preprocess_data(raw_cases) return preprocessed_cases diff --git a/src/open_inwoner/openzaak/clients.py b/src/open_inwoner/openzaak/clients.py index 5ebae816b4..1569d4dd54 100644 --- a/src/open_inwoner/openzaak/clients.py +++ b/src/open_inwoner/openzaak/clients.py @@ -41,12 +41,17 @@ class ZakenClient(APIClient): def fetch_cases( self, - user_bsn=None, - user_kvk_or_rsin=None, - max_requests=4, - identificatie=None, - vestigingsnummer=None, + user_bsn: Optional[str] = None, + user_kvk_or_rsin: Optional[str] = None, + max_requests: int = 4, + identificatie: Optional[str] = None, + vestigingsnummer: Optional[str] = None, ): + if user_bsn and (user_kvk_or_rsin or vestigingsnummer): + raise ValueError( + "either `user_bsn` or `user_kvk_or_rsin`/`vestigingsnummer` should be supplied, not both" + ) + if user_bsn: return self.fetch_cases_by_bsn( user_bsn, max_requests=max_requests, identificatie=identificatie diff --git a/src/open_inwoner/openzaak/utils.py b/src/open_inwoner/openzaak/utils.py index b11f6c1a8a..edc7f99ef8 100644 --- a/src/open_inwoner/openzaak/utils.py +++ b/src/open_inwoner/openzaak/utils.py @@ -145,12 +145,15 @@ def translate_single_status(status_text: str) -> str: return "" -def get_fetch_parameters(request) -> dict: +def get_user_fetch_parameters(request) -> dict: """ Determine the parameters used to perform ZGW resource fetches """ user = request.user + if not user.is_authenticated: + return {} + if user.bsn: return {"user_bsn": user.bsn} elif user.kvk: diff --git a/src/open_inwoner/pdc/managers.py b/src/open_inwoner/pdc/managers.py index 16ae5d049d..531e9dfbbc 100644 --- a/src/open_inwoner/pdc/managers.py +++ b/src/open_inwoner/pdc/managers.py @@ -12,7 +12,7 @@ from open_inwoner.openzaak.api_models import Zaak from open_inwoner.openzaak.clients import build_client from open_inwoner.openzaak.models import ZaakTypeConfig -from open_inwoner.openzaak.utils import get_fetch_parameters +from open_inwoner.openzaak.utils import get_user_fetch_parameters class ProductQueryset(models.QuerySet): @@ -56,7 +56,7 @@ def filter_by_zaken_for_request(self, request): if client is None: return self.none() - cases = client.fetch_cases(**get_fetch_parameters(request)) + cases = client.fetch_cases(**get_user_fetch_parameters(request)) return self.filter_by_zaken(cases) diff --git a/src/open_inwoner/search/views.py b/src/open_inwoner/search/views.py index 955dc6dcba..daf0254d49 100644 --- a/src/open_inwoner/search/views.py +++ b/src/open_inwoner/search/views.py @@ -10,8 +10,7 @@ from open_inwoner.configurations.models import SiteConfiguration from open_inwoner.openzaak.clients import build_client -from open_inwoner.openzaak.models import OpenZaakConfig -from open_inwoner.openzaak.utils import get_fetch_parameters +from open_inwoner.openzaak.utils import get_user_fetch_parameters from open_inwoner.utils.mixins import PaginationMixin from open_inwoner.utils.views import CommonPageMixin, LoginMaybeRequiredMixin, LogMixin @@ -64,7 +63,7 @@ def search(self, form): self.log_user_action(user, _("search query: {query}").format(query=query)) # Check if the query exactly matches with a case that belongs to the user - if search_params := get_fetch_parameters(self.request): + if search_params := get_user_fetch_parameters(self.request): if client := build_client("zaak"): cases = client.fetch_cases(**search_params, identificatie=query) if cases and len(cases) == 1: