From d64a2675e49af79b140dfc46d26d755751cba535 Mon Sep 17 00:00:00 2001 From: i701 Date: Tue, 15 Jul 2025 23:04:11 +0500 Subject: [PATCH] =?UTF-8?q?refactor(verification):=20enhance=20error=20han?= =?UTF-8?q?dling=20and=20response=20structure=20in=20user=20verification?= =?UTF-8?q?=20process=20=F0=9F=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/utils.py | 45 +++++++++++++++++++++++++++------------------ api/views.py | 17 ++++++++--------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/api/utils.py b/api/utils.py index 6e2e92e..7673665 100644 --- a/api/utils.py +++ b/api/utils.py @@ -1,5 +1,5 @@ import logging -from typing import List, TypedDict +from typing import List, Optional, TypedDict import requests from decouple import config from api.models import User @@ -40,7 +40,9 @@ def reverse_dhivehi_string(input_str): class MismatchResult(TypedDict): ok: bool - mismatch_fields: List[str] + mismatch_fields: Optional[List[str]] + error: Optional[str] + detail: Optional[str] def check_person_api_verification( @@ -63,20 +65,22 @@ def check_person_api_verification( raise ValueError( "PERSON_VERIFY_BASE_URL is not set in the environment variables." ) - print(id_card) response = requests.get(f"{PERSON_VERIFY_BASE_URL}/api/person/{id_card}") + api_reponse = response.json() + if response.status_code != 200: logger.error( f"Failed to fetch data from Person API for ID Card '{id_card}'. " f"Status Code: {response.status_code}, Response: {response.text}" ) - return {"ok": False, "mismatch_fields": ["api_error"]} - api_data = response.json() - if not api_data: - logger.error( - f"No data found in Person API for ID Card '{id_card}'. Response: {response.text}" - ) - return {"ok": False, "mismatch_fields": ["no_data"]} + return { + "ok": False, + "mismatch_fields": None, + "error": response.json()["error"] if "error" in response.json() else None, + "detail": response.json()["detail"] + if "detail" in response.json() + else None, + } # Initialize a list to hold fields that do not match mismatch_fields = [] @@ -86,12 +90,12 @@ def check_person_api_verification( user_dob_iso = user_data.dob.isoformat() if user_data.dob else None # Prepare API data for comparison - api_nic = api_data.get("nic") - api_name = api_data.get("name_en") - api_house_name = api_data.get("house_name_en") - api_dob = api_data.get("dob") - api_atoll = api_data.get("atoll_en") - api_island_name = api_data.get("island_name_en") + api_nic = api_reponse.get("nic") + api_name = api_reponse.get("name_en") + api_house_name = api_reponse.get("house_name_en") + api_dob = api_reponse.get("dob") + api_atoll = api_reponse.get("atoll_en") + api_island_name = api_reponse.get("island_name_en") # Perform comparisons and identify mismatches if user_data.id_card != api_nic: @@ -134,6 +138,11 @@ def check_person_api_verification( ) if mismatch_fields: - return {"ok": False, "mismatch_fields": mismatch_fields} + return { + "ok": False, + "mismatch_fields": mismatch_fields, + "error": None, + "detail": None, + } else: - return {"ok": True, "mismatch_fields": []} + return {"ok": True, "mismatch_fields": [], "error": None, "detail": None} diff --git a/api/views.py b/api/views.py index 0f8230b..93132d2 100644 --- a/api/views.py +++ b/api/views.py @@ -406,23 +406,22 @@ class UserVerifyAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView): {"message": "You are not authorized to update this user."}, status=status.HTTP_403_FORBIDDEN, ) + if user.verified: + return Response( + {"message": "User is already verified."}, + status=status.HTTP_400_BAD_REQUEST, + ) serializer = self.get_serializer(user, data=request.data, partial=True) serializer.is_valid(raise_exception=True) result = check_person_api_verification(user_data=user, id_card=user.id_card) if not result["ok"]: return Response( - { - "message": "User verification failed. Please check the api user details.", - "mismatch_fields": result["mismatch_fields"], - }, - status=status.HTTP_400_BAD_REQUEST, + result, + status=status.HTTP_404_NOT_FOUND, ) if result["mismatch_fields"]: return Response( - { - "message": "User verification failed due to mismatched fields.", - "mismatch_fields": result["mismatch_fields"], - }, + result, status=status.HTTP_400_BAD_REQUEST, ) user.verified = True