From c4e65bbe4e9eee6bee400a2c90552f0aef4502a0 Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Wed, 9 Jul 2025 09:51:34 +0530 Subject: [PATCH] CHANGE_RESTAUTHHELPER : RV --- app/Helpers/RestAuthHelper.php | 236 +++++++++++++++++++++++++++++---- 1 file changed, 210 insertions(+), 26 deletions(-) diff --git a/app/Helpers/RestAuthHelper.php b/app/Helpers/RestAuthHelper.php index 1e3f35d..d432d8d 100644 --- a/app/Helpers/RestAuthHelper.php +++ b/app/Helpers/RestAuthHelper.php @@ -10,16 +10,176 @@ use App\Models\EmployeePolicyModel; class RestAuthHelper { + // public static function getPreAndPostDataByEmailOrMobile($params) + // { + // // print_r($params); die; + // $mobile_number = $params['mobile_number'] ?? null; + // $email_id = $params['email_id'] ?? null; + // $otp = $params['otp'] ?? null; + // $old_mpin = $params['$old_mpin'] ?? null; + + // // Return null if both inputs are empty + // if (empty($mobile_number) && empty($email_id)) { + // return null; + // } + + // $params = [ + // 'mobile_number' => $mobile_number, + // 'email_id' => $email_id, + // 'otp' => $otp, + // '$old_mpin' => $old_mpin, + // ]; + + // $pre_data = self::getPreEmployeeData($params); + // $post_data = self::getPostEmployeeData($params); + + // // Return empty array if both are missing + // if (empty($pre_data) && empty($post_data)) { + // return []; + // } + + // // Determine the latest record + // $latest_key = null; + // if (!empty($pre_data) && !empty($post_data)) { + // $latest_key = strtotime($pre_data['created_at']) > strtotime($post_data['created_at']) ? 'pre' : 'post'; + // } elseif (!empty($pre_data)) { + // $latest_key = 'pre'; + // } elseif (!empty($post_data)) { + // $latest_key = 'post'; + // } + + // // Compare client_short_name only if both records exist + // if (!empty($pre_data) && !empty($post_data)) { + // $latest_data = $latest_key === 'pre' ? $pre_data : $post_data; + // $other_data = $latest_key === 'pre' ? $post_data : $pre_data; + + // if ($latest_data['client_short_name'] === $other_data['client_short_name']) { + // return [ + // 'pre' => $pre_data, + // 'post' => $post_data, + // ]; + // } + // } + + // // If one of the data is missing or client names mismatch + // return [ + // 'pre' => $latest_key === 'pre' ? $pre_data : [], + // 'post' => $latest_key === 'post' ? $post_data : [], + // ]; + // } + + // public static function getPreEmployeeData(array $params) + // { + // $employeeModel = new EmployeeModel(); + + // $mobile_number = $params['mobile_number'] ?? null; + // $email_id = $params['email_id'] ?? null; + // $otp = $params['otp'] ?? null; + // $old_mpin = $params['old_mpin'] ?? null; + + // if (!empty($mobile_number)) { + // $builder = $employeeModel + // ->select('employees.id as employee_id, employees.*') + // ->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner') + // ->where('employees.is_active', 1) + // ->where("TRIM(employees.relationship) = 'self'", null, false) + // ->whereIn('employees.emp_status', ['draft', 'enrolled']) + // ->where('employees.mobile', $mobile_number) + // ->where('EP.is_active', 1) + // ->whereIn('EP.status', ['draft', 'enrolled']); + + // if (!empty($old_mpin)) { + // $builder->where('employees.mpin', $old_mpin); + // } + + // $employeeData = $builder->orderBy('employees.id', 'desc')->first(); + + // } elseif (!empty($email_id)) { + // $builder = $employeeModel + // ->select('employees.id as employee_id, employees.*') + // ->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner') + // ->where('employees.is_active', 1) + // ->where("TRIM(employees.relationship) = 'self'", null, false) + // ->whereIn('employees.emp_status', ['draft', 'enrolled']) + // ->where('employees.email_corporate', $email_id) + // ->where('EP.is_active', 1) + // ->whereIn('EP.status', ['draft', 'enrolled']); + + // if (!empty($otp)) { + // $builder->where('employees.otp', $otp); + // } + + // if (!empty($old_mpin)) { + // $builder->where('employees.mpin', $old_mpin); + // } + + // $employeeData = $builder->orderBy('employees.id', 'desc')->first(); + + // } else { + // return null; + // } + + // if($employeeData){ + // $clientModel = new ClientModel; + // $client_data = $clientModel->where('is_active', 1)->where('id', $employeeData['client_id'])->first(); + // $employeeData['client_short_name'] = $client_data['short_name']; + // } + + // return $employeeData; + // } + + // public static function getPostEmployeeData(array $params) + // { + // // print_r($params); die; + // $mobile_number = $params['mobile_number'] ?? null; + // $email_id = $params['email_id'] ?? null; + // $otp = $params['otp'] ?? null; + // $old_mpin = $params['old_mpin'] ?? null; + + + // if (!empty($mobile_number) || !empty($email_id)) { + + // $client = \Config\Services::curlrequest(); + // $endPoint = 'getPostEmployeeDataForAuth'; + // $url = env('POST_ENROLLMENT_BASEURL') . $endPoint; + + // $postData = []; + + // if (!empty($mobile_number)) { + // $postData['mobile_number'] = $mobile_number; + // } + + // if (!empty($email_id)) { + // $postData['email_id'] = $email_id; + // } + + // if (!empty($otp)) { + // $postData['otp'] = $otp; + // } + + // if (!empty($old_mpin)) { + // $postData['old_mpin'] = $old_mpin; + // } + + // $response = $client->post( $url, ['json' => $postData, 'http_errors' => false]); + // $post_json = $response->getBody(); + // $post_data = json_decode($post_json, true); + + // return $post_data['data']; + // } + // } + public static function getPreAndPostDataByEmailOrMobile($params) - { - // print_r($params); die; + { + log_message('error', 'Function getPreAndPostDataByEmailOrMobile called with: ' . json_encode($params)); + $mobile_number = $params['mobile_number'] ?? null; $email_id = $params['email_id'] ?? null; $otp = $params['otp'] ?? null; - $old_mpin = $params['$old_mpin'] ?? null; + $old_mpin = $params['old_mpin'] ?? null; - // Return null if both inputs are empty if (empty($mobile_number) && empty($email_id)) { + log_message('error', 'Both mobile_number and email_id are empty. Returning null.'); return null; } @@ -27,18 +187,20 @@ class RestAuthHelper 'mobile_number' => $mobile_number, 'email_id' => $email_id, 'otp' => $otp, - '$old_mpin' => $old_mpin, + 'old_mpin' => $old_mpin, ]; $pre_data = self::getPreEmployeeData($params); $post_data = self::getPostEmployeeData($params); - // Return empty array if both are missing + log_message('error', 'Pre Data: ' . json_encode($pre_data)); + log_message('error', 'Post Data: ' . json_encode($post_data)); + if (empty($pre_data) && empty($post_data)) { + log_message('error', 'Both pre_data and post_data are empty. Returning empty array.'); return []; } - // Determine the latest record $latest_key = null; if (!empty($pre_data) && !empty($post_data)) { $latest_key = strtotime($pre_data['created_at']) > strtotime($post_data['created_at']) ? 'pre' : 'post'; @@ -48,36 +210,43 @@ class RestAuthHelper $latest_key = 'post'; } - // Compare client_short_name only if both records exist + log_message('error', 'Latest key determined as: ' . $latest_key); + if (!empty($pre_data) && !empty($post_data)) { $latest_data = $latest_key === 'pre' ? $pre_data : $post_data; $other_data = $latest_key === 'pre' ? $post_data : $pre_data; if ($latest_data['client_short_name'] === $other_data['client_short_name']) { - return [ - 'pre' => $pre_data, - 'post' => $post_data, - ]; + log_message('error', 'client_short_name match. Returning both records.'); + return ['pre' => $pre_data, 'post' => $post_data]; } } - // If one of the data is missing or client names mismatch - return [ + $result = [ 'pre' => $latest_key === 'pre' ? $pre_data : [], 'post' => $latest_key === 'post' ? $post_data : [], ]; + + log_message('error', 'Returning data: ' . json_encode($result)); + return $result; } public static function getPreEmployeeData(array $params) { + log_message('error', 'Function getPreEmployeeData called with: ' . json_encode($params)); + $employeeModel = new EmployeeModel(); $mobile_number = $params['mobile_number'] ?? null; $email_id = $params['email_id'] ?? null; - $otp = $params['otp'] ?? null; + $otp = $params['otp'] ?? null; $old_mpin = $params['old_mpin'] ?? null; + $employeeData = null; + if (!empty($mobile_number)) { + log_message('error', 'Searching employee by mobile_number: ' . $mobile_number); + $builder = $employeeModel ->select('employees.id as employee_id, employees.*') ->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner') @@ -93,8 +262,9 @@ class RestAuthHelper } $employeeData = $builder->orderBy('employees.id', 'desc')->first(); - } elseif (!empty($email_id)) { + log_message('error', 'Searching employee by email_id: ' . $email_id); + $builder = $employeeModel ->select('employees.id as employee_id, employees.*') ->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner') @@ -114,31 +284,33 @@ class RestAuthHelper } $employeeData = $builder->orderBy('employees.id', 'desc')->first(); - } else { + log_message('warning', 'No mobile or email found. Returning null.'); return null; } - if($employeeData){ + log_message('error', 'Pre employee data fetched: ' . json_encode($employeeData)); + + if ($employeeData) { $clientModel = new ClientModel; $client_data = $clientModel->where('is_active', 1)->where('id', $employeeData['client_id'])->first(); - $employeeData['client_short_name'] = $client_data['short_name']; + $employeeData['client_short_name'] = $client_data['short_name'] ?? ''; + log_message('error', 'Client short name attached: ' . $employeeData['client_short_name']); } return $employeeData; } public static function getPostEmployeeData(array $params) - { - // print_r($params); die; + { + log_message('error', 'Function getPostEmployeeData called with: ' . json_encode($params)); + $mobile_number = $params['mobile_number'] ?? null; $email_id = $params['email_id'] ?? null; - $otp = $params['otp'] ?? null; + $otp = $params['otp'] ?? null; $old_mpin = $params['old_mpin'] ?? null; - if (!empty($mobile_number) || !empty($email_id)) { - $client = \Config\Services::curlrequest(); $endPoint = 'getPostEmployeeDataForAuth'; $url = env('POST_ENROLLMENT_BASEURL') . $endPoint; @@ -161,11 +333,23 @@ class RestAuthHelper $postData['old_mpin'] = $old_mpin; } - $response = $client->post( $url, ['json' => $postData, 'http_errors' => false]); + log_message('error', 'Sending POST to external API: ' . $url); + log_message('error', 'POST payload: ' . json_encode($postData)); + + $response = $client->post($url, ['json' => $postData, 'http_errors' => false]); + $post_json = $response->getBody(); + log_message('error', 'Response from API: ' . $post_json); + $post_data = json_decode($post_json, true); - return $post_data['data']; + $data = $post_data['data'] ?? []; + log_message('error', 'Parsed post_data: ' . json_encode($data)); + return $data; } + + log_message('warning', 'No mobile or email present in params for post fetch.'); } + + }