diff --git a/api/tasks.py b/api/tasks.py index 0f2c6a1..a24085e 100644 --- a/api/tasks.py +++ b/api/tasks.py @@ -15,6 +15,7 @@ from apibase.env import env, BASE_DIR from procrastinate.contrib.django import app from procrastinate import builtin_tasks import time +import requests logger = logging.getLogger(__name__) @@ -143,6 +144,13 @@ def verify_user_with_person_api_task(user_id: int): Verify the user with the Person API. :param user_id: The ID of the user to verify. """ + PERSON_VERIFY_BASE_URL = env.str("PERSON_VERIFY_BASE_URL", default="") # type: ignore + + if not PERSON_VERIFY_BASE_URL: + raise ValueError( + "PERSON_VERIFY_BASE_URL is not set in the environment variables." + ) + print(f"Verifying user with ID: {user_id}") if not user_id: logger.error("User ID is not provided.") @@ -151,22 +159,16 @@ def verify_user_with_person_api_task(user_id: int): if not t_user: logger.error(f"User with ID {user_id} not found.") return None - print(t_user) - return + print("t_user:", t_user) + response = requests.get(f"{PERSON_VERIFY_BASE_URL}/api/person/{t_user.t_id_card}") + verification_failed_message = f"""*The following user verification failed*:\n\n*ID Card:* {t_user.t_id_card}\n*Name:* {t_user.t_first_name} {t_user.t_last_name}\n*House Name:* {t_user.t_address}\n*Date of Birth:* {t_user.t_dob}\n*Island:* {(t_user.t_atoll.name if t_user.t_atoll else "N/A")} {(t_user.t_island.name if t_user.t_island else "N/A")}\n*Mobile:* {t_user.t_mobile}\nVisit [SAR Link Portal](https://portal.sarlink.net/users/{user_id}/details) to manually verify this user. """ - # logger.info(verification_failed_message) - PERSON_VERIFY_BASE_URL = env.str("PERSON_VERIFY_BASE_URL", default="") # type: ignore + logger.info(verification_failed_message) - if not PERSON_VERIFY_BASE_URL: - raise ValueError( - "PERSON_VERIFY_BASE_URL is not set in the environment variables." - ) - import requests - response = requests.get(f"{PERSON_VERIFY_BASE_URL}/api/person/{t_user.t_id_card}") if response.status_code == 200: data = response.json() api_nic = data.get("nic") @@ -176,10 +178,10 @@ def verify_user_with_person_api_task(user_id: int): api_atoll = data.get("atoll_en") api_island_name = data.get("island_name_en") - if not t_user.mobile or t_user.dob is None: + if not t_user.t_mobile or t_user.t_dob is None: logger.error("User mobile or date of birth is not set.") return None - if not t_user.island or t_user.atoll is None: + if not t_user.t_island or t_user.t_atoll is None: logger.error("User island or atoll is not set.") return None @@ -215,6 +217,9 @@ def verify_user_with_person_api_task(user_id: int): logger.info(f"api island name type: {type(api_island_name)}") logger.info(f"user island name type: {type(t_user.t_island.name)}") + + print("CHECKING USER FIELDS AGAINST API DATA") + if ( data.get("nic") == t_user.t_id_card and data.get("name_en") == f"{t_user.t_first_name} {t_user.t_last_name}"