From 15909fc7a0b99d91eb1ef7c4e4452631a437bb56 Mon Sep 17 00:00:00 2001 From: Remy van der Wereld Date: Mon, 30 Dec 2024 12:34:53 +0100 Subject: [PATCH] WON-41 Added vakantieverhuur registration endpoint by bagId --- .env | 8 +-- .gitignore | 1 + app/apps/addresses/serializers.py | 14 ++++ app/apps/addresses/views.py | 64 ++++++++++++++++++- app/apps/health/health_checks.py | 22 ++----- app/config/settings.py | 19 ++---- app/utils/api_queries_toeristische_verhuur.py | 42 ++++++------ 7 files changed, 114 insertions(+), 56 deletions(-) diff --git a/.env b/.env index 3a453ab8f..5d6cbf605 100644 --- a/.env +++ b/.env @@ -26,12 +26,8 @@ DEFAULT_THEME=Vakantieverhuur DEFAULT_REASON=SIG melding VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_URL=https://api.acceptatie.toeristischeverhuur.nl/api/ VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN=SECRET_ACCESS_TOKEN -VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_HEALTH_CHECK_BAG_ID=0503100000000209 -VAKANTIEVERHUUR_REGISTRATIE_API_URL=https://acceptatie.toeristischeverhuur.nl/ext/api/Registrations/ -VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN=SECRET_ACCESS_TOKEN -VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BSN=999999990 -VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BAG_ID=0503100000000209 -VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_REGISTRATION_NUMBER=05034542BC484F3891BB +VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BAG_ID=0503100000000209 +VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BSN=999999990 ZAKEN_CONTAINER_HOST=http://zaak-gateway:8000 DEFAULT_SCHEDULE_ACTIONS=Huisbezoek,Hercontrole diff --git a/.gitignore b/.gitignore index a6d52bd8c..1e6e26ae0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ __pycache__/ app/.env app/.coverage private_media/ +.local.env diff --git a/app/apps/addresses/serializers.py b/app/apps/addresses/serializers.py index 36f81e02e..7bd84af6a 100644 --- a/app/apps/addresses/serializers.py +++ b/app/apps/addresses/serializers.py @@ -116,3 +116,17 @@ class MeldingenSerializer(serializers.Serializer): totalPages = serializers.IntegerField() totalRecords = serializers.IntegerField() data = serializers.ListField(child=serializers.DictField()) + + +class RegistrationNumberSerializer(serializers.Serializer): + registrationNumber = serializers.CharField(required=True) + + +class RegistrationDetailsSerializer(serializers.Serializer): + registrationNumber = serializers.CharField(required=True) + requester = serializers.DictField() + rentalHouse = serializers.DictField() + requestForOther = serializers.BooleanField() + requestForBedAndBreakfast = serializers.BooleanField() + createdAt = serializers.DateTimeField() + agreementDate = serializers.DateTimeField() diff --git a/app/apps/addresses/views.py b/app/apps/addresses/views.py index 072fb287b..233ca955a 100644 --- a/app/apps/addresses/views.py +++ b/app/apps/addresses/views.py @@ -6,6 +6,8 @@ DistrictSerializer, HousingCorporationSerializer, MeldingenSerializer, + RegistrationDetailsSerializer, + RegistrationNumberSerializer, ResidentsSerializer, ) from apps.cases.models import Advertisement @@ -21,7 +23,11 @@ from rest_framework.response import Response from rest_framework.viewsets import ViewSet from utils.api_queries_brp import get_brp_by_nummeraanduiding_id -from utils.api_queries_toeristische_verhuur import get_vakantieverhuur_meldingen +from utils.api_queries_toeristische_verhuur import ( + get_vakantieverhuur_meldingen, + get_vakantieverhuur_registration, + get_vakantieverhuur_registrations_by_bag_id, +) logger = logging.getLogger(__name__) @@ -240,3 +246,59 @@ def meldingen(self, request, bag_id): {"error": "Toeristische verhuur meldingen could not be obtained"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR, ) + + @extend_schema( + description="Gets all registrations for holiday rental by bag_id", + responses={status.HTTP_200_OK: RegistrationDetailsSerializer(many=True)}, + ) + @action( + detail=True, + url_path="registrations", + methods=["get"], + pagination_class=None, + ) + def registrations(self, request, bag_id): + try: + ( + registrations_data, + status_code, + ) = get_vakantieverhuur_registrations_by_bag_id( + bag_id, + ) + serialized_registrations = RegistrationNumberSerializer( + data=registrations_data, many=True + ) + serialized_registrations.is_valid(raise_exception=True) + + # Fetch details for each registration number + detailed_registrations = [] + for registration in serialized_registrations.data: + # Remove spaces from registration number + registration_number = registration["registrationNumber"].replace( + " ", "" + ) + try: + # Fetch detailed data for the current registrationNumber + ( + registration_details, + detail_status_code, + ) = get_vakantieverhuur_registration(registration_number) + if detail_status_code == 200: # Only append if successful + detailed_registrations.append(registration_details) + else: + print( + f"Failed to fetch details for {registration_number}. Status: {detail_status_code}" + ) + except Exception as e: + print(f"Error fetching details for {registration_number}: {e}") + + serializer = RegistrationDetailsSerializer( + detailed_registrations, many=True + ) + + return Response(serializer.data, status=status_code) + except Exception: + return Response( + {"error": "Toeristische verhuur registrations could not be obtained"}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR, + ) diff --git a/app/apps/health/health_checks.py b/app/apps/health/health_checks.py index 8e5282db6..e71a26a7d 100644 --- a/app/apps/health/health_checks.py +++ b/app/apps/health/health_checks.py @@ -13,10 +13,9 @@ do_bag_search_pdok_by_bag_id, ) from utils.api_queries_toeristische_verhuur import ( - get_bag_vakantieverhuur_registrations, - get_bsn_vakantieverhuur_registrations, get_vakantieverhuur_meldingen, - get_vakantieverhuur_registration, + get_vakantieverhuur_registrations_by_bag_id, + get_vakantieverhuur_registrations_by_bsn_number, ) logger = logging.getLogger(__name__) @@ -265,22 +264,15 @@ class VakantieVerhuurRegistratieCheck(BaseHealthCheckBackend): def check_status(self): try: - registration = get_vakantieverhuur_registration( - settings.VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_REGISTRATION_NUMBER - ) - assert bool( - registration - ), "The registration data is empty and could not be retrieved" - - bsn_registrations = get_bsn_vakantieverhuur_registrations( - settings.VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BSN + bsn_registrations = get_vakantieverhuur_registrations_by_bsn_number( + settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BSN ) assert ( len(bsn_registrations) > 0 ), "The registration data is empty and could not be retrieved using the BSN number" - bag_registrations = get_bag_vakantieverhuur_registrations( - settings.VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BAG_ID + bag_registrations = get_vakantieverhuur_registrations_by_bag_id( + settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BAG_ID ) assert ( len(bag_registrations) > 0 @@ -311,7 +303,7 @@ def check_status(self): try: get_vakantieverhuur_meldingen( - settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_HEALTH_CHECK_BAG_ID, + settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BAG_ID, query_params=params, use_retry=False, ) diff --git a/app/config/settings.py b/app/config/settings.py index 354965562..fb3a3e46b 100644 --- a/app/config/settings.py +++ b/app/config/settings.py @@ -560,22 +560,11 @@ def get_redis_url(): VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN = os.getenv( "VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN" ) -VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_HEALTH_CHECK_BAG_ID = os.getenv( - "VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_HEALTH_CHECK_BAG_ID", "0503100000000209" +VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BAG_ID = os.getenv( + "VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BAG_ID", "0503100000000209" ) - -VAKANTIEVERHUUR_REGISTRATIE_API_URL = os.getenv("VAKANTIEVERHUUR_REGISTRATIE_API_URL") -VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN = os.getenv( - "VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN" -) -VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BSN = os.getenv( - "VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BSN" -) -VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BAG_ID = os.getenv( - "VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BAG_ID" -) -VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_REGISTRATION_NUMBER = os.getenv( - "VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_REGISTRATION_NUMBER" +VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BSN = os.getenv( + "VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BSN" ) DEFAULT_AUTO_FIELD = "django.db.models.AutoField" diff --git a/app/utils/api_queries_toeristische_verhuur.py b/app/utils/api_queries_toeristische_verhuur.py index 8e3e0f290..bf5fd3698 100644 --- a/app/utils/api_queries_toeristische_verhuur.py +++ b/app/utils/api_queries_toeristische_verhuur.py @@ -39,57 +39,61 @@ def _get_vakantieverhuur_meldingen_internal(): return _get_vakantieverhuur_meldingen_internal() -@retry(stop=stop_after_attempt(3), after=after_log(logger, logging.ERROR)) +@retry(stop=stop_after_attempt(1), after=after_log(logger, logging.ERROR)) def get_vakantieverhuur_registration(registration_number): """ Get the Vakantieverhuur registration """ - header = {"x-api-key": settings.VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN} - url = ( - f"{settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_URL}{registration_number}" - ) + header = { + "x-api-key": settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN + } + url = f"{settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_URL}registrations/{registration_number}" response = requests.get( url=url, headers=header, - verify="/usr/local/share/ca-certificates/adp_rootca.crt", + timeout=30, ) response.raise_for_status() - return response.json() + return response.json(), response.status_code @retry(stop=stop_after_attempt(3), after=after_log(logger, logging.ERROR)) -def get_bsn_vakantieverhuur_registrations(bsn_number): +def get_vakantieverhuur_registrations_by_bag_id(bag_id): """ - Get the Vakantieverhuur registrations using a BSN number + Get the Vakantieverhuur registration by bag_id """ - header = {"x-api-key": settings.VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN} - url = f"{settings.VAKANTIEVERHUUR_REGISTRATIE_API_URL}bsn/{bsn_number}" + header = { + "x-api-key": settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN + } + url = f"{settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_URL}registrations/adresseerbaarObjectIdentificatie/{bag_id}" response = requests.get( url=url, headers=header, - verify="/usr/local/share/ca-certificates/adp_rootca.crt", + timeout=30, ) response.raise_for_status() - return response.json() + return response.json(), response.status_code @retry(stop=stop_after_attempt(3), after=after_log(logger, logging.ERROR)) -def get_bag_vakantieverhuur_registrations(bag_id): +def get_vakantieverhuur_registrations_by_bsn_number(bsn_number): """ - Get the Vakantieverhuur registrations using a BSN number + Get the Vakantieverhuur registration by bsn_number """ - header = {"x-api-key": settings.VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN} - url = f"{settings.VAKANTIEVERHUUR_REGISTRATIE_API_URL}bagid/{bag_id}" + header = { + "x-api-key": settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN + } + url = f"{settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_URL}registrations/bsn/{bsn_number}" response = requests.get( url=url, headers=header, - verify="/usr/local/share/ca-certificates/adp_rootca.crt", + timeout=30, ) response.raise_for_status() - return response.json() + return response.json(), response.status_code