mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-07-17 02:25:50 +00:00
refactor(verification): enhance error handling and response structure in user verification process 🔨
This commit is contained in:
45
api/utils.py
45
api/utils.py
@ -1,5 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import List, TypedDict
|
from typing import List, Optional, TypedDict
|
||||||
import requests
|
import requests
|
||||||
from decouple import config
|
from decouple import config
|
||||||
from api.models import User
|
from api.models import User
|
||||||
@ -40,7 +40,9 @@ def reverse_dhivehi_string(input_str):
|
|||||||
|
|
||||||
class MismatchResult(TypedDict):
|
class MismatchResult(TypedDict):
|
||||||
ok: bool
|
ok: bool
|
||||||
mismatch_fields: List[str]
|
mismatch_fields: Optional[List[str]]
|
||||||
|
error: Optional[str]
|
||||||
|
detail: Optional[str]
|
||||||
|
|
||||||
|
|
||||||
def check_person_api_verification(
|
def check_person_api_verification(
|
||||||
@ -63,20 +65,22 @@ def check_person_api_verification(
|
|||||||
raise ValueError(
|
raise ValueError(
|
||||||
"PERSON_VERIFY_BASE_URL is not set in the environment variables."
|
"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}")
|
response = requests.get(f"{PERSON_VERIFY_BASE_URL}/api/person/{id_card}")
|
||||||
|
api_reponse = response.json()
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Failed to fetch data from Person API for ID Card '{id_card}'. "
|
f"Failed to fetch data from Person API for ID Card '{id_card}'. "
|
||||||
f"Status Code: {response.status_code}, Response: {response.text}"
|
f"Status Code: {response.status_code}, Response: {response.text}"
|
||||||
)
|
)
|
||||||
return {"ok": False, "mismatch_fields": ["api_error"]}
|
return {
|
||||||
api_data = response.json()
|
"ok": False,
|
||||||
if not api_data:
|
"mismatch_fields": None,
|
||||||
logger.error(
|
"error": response.json()["error"] if "error" in response.json() else None,
|
||||||
f"No data found in Person API for ID Card '{id_card}'. Response: {response.text}"
|
"detail": response.json()["detail"]
|
||||||
)
|
if "detail" in response.json()
|
||||||
return {"ok": False, "mismatch_fields": ["no_data"]}
|
else None,
|
||||||
|
}
|
||||||
|
|
||||||
# Initialize a list to hold fields that do not match
|
# Initialize a list to hold fields that do not match
|
||||||
mismatch_fields = []
|
mismatch_fields = []
|
||||||
@ -86,12 +90,12 @@ def check_person_api_verification(
|
|||||||
user_dob_iso = user_data.dob.isoformat() if user_data.dob else None
|
user_dob_iso = user_data.dob.isoformat() if user_data.dob else None
|
||||||
|
|
||||||
# Prepare API data for comparison
|
# Prepare API data for comparison
|
||||||
api_nic = api_data.get("nic")
|
api_nic = api_reponse.get("nic")
|
||||||
api_name = api_data.get("name_en")
|
api_name = api_reponse.get("name_en")
|
||||||
api_house_name = api_data.get("house_name_en")
|
api_house_name = api_reponse.get("house_name_en")
|
||||||
api_dob = api_data.get("dob")
|
api_dob = api_reponse.get("dob")
|
||||||
api_atoll = api_data.get("atoll_en")
|
api_atoll = api_reponse.get("atoll_en")
|
||||||
api_island_name = api_data.get("island_name_en")
|
api_island_name = api_reponse.get("island_name_en")
|
||||||
|
|
||||||
# Perform comparisons and identify mismatches
|
# Perform comparisons and identify mismatches
|
||||||
if user_data.id_card != api_nic:
|
if user_data.id_card != api_nic:
|
||||||
@ -134,6 +138,11 @@ def check_person_api_verification(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if mismatch_fields:
|
if mismatch_fields:
|
||||||
return {"ok": False, "mismatch_fields": mismatch_fields}
|
return {
|
||||||
|
"ok": False,
|
||||||
|
"mismatch_fields": mismatch_fields,
|
||||||
|
"error": None,
|
||||||
|
"detail": None,
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
return {"ok": True, "mismatch_fields": []}
|
return {"ok": True, "mismatch_fields": [], "error": None, "detail": None}
|
||||||
|
17
api/views.py
17
api/views.py
@ -406,23 +406,22 @@ class UserVerifyAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
|||||||
{"message": "You are not authorized to update this user."},
|
{"message": "You are not authorized to update this user."},
|
||||||
status=status.HTTP_403_FORBIDDEN,
|
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 = self.get_serializer(user, data=request.data, partial=True)
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
result = check_person_api_verification(user_data=user, id_card=user.id_card)
|
result = check_person_api_verification(user_data=user, id_card=user.id_card)
|
||||||
if not result["ok"]:
|
if not result["ok"]:
|
||||||
return Response(
|
return Response(
|
||||||
{
|
result,
|
||||||
"message": "User verification failed. Please check the api user details.",
|
status=status.HTTP_404_NOT_FOUND,
|
||||||
"mismatch_fields": result["mismatch_fields"],
|
|
||||||
},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST,
|
|
||||||
)
|
)
|
||||||
if result["mismatch_fields"]:
|
if result["mismatch_fields"]:
|
||||||
return Response(
|
return Response(
|
||||||
{
|
result,
|
||||||
"message": "User verification failed due to mismatched fields.",
|
|
||||||
"mismatch_fields": result["mismatch_fields"],
|
|
||||||
},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST,
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
)
|
)
|
||||||
user.verified = True
|
user.verified = True
|
||||||
|
Reference in New Issue
Block a user