CHANGE_RESTAUTHHELPER : RV
This commit is contained in:
parent
8784c20bf6
commit
c4e65bbe4e
@ -10,16 +10,176 @@ use App\Models\EmployeePolicyModel;
|
|||||||
|
|
||||||
class RestAuthHelper
|
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)
|
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;
|
$mobile_number = $params['mobile_number'] ?? null;
|
||||||
$email_id = $params['email_id'] ?? null;
|
$email_id = $params['email_id'] ?? null;
|
||||||
$otp = $params['otp'] ?? 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)) {
|
if (empty($mobile_number) && empty($email_id)) {
|
||||||
|
log_message('error', 'Both mobile_number and email_id are empty. Returning null.');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,18 +187,20 @@ class RestAuthHelper
|
|||||||
'mobile_number' => $mobile_number,
|
'mobile_number' => $mobile_number,
|
||||||
'email_id' => $email_id,
|
'email_id' => $email_id,
|
||||||
'otp' => $otp,
|
'otp' => $otp,
|
||||||
'$old_mpin' => $old_mpin,
|
'old_mpin' => $old_mpin,
|
||||||
];
|
];
|
||||||
|
|
||||||
$pre_data = self::getPreEmployeeData($params);
|
$pre_data = self::getPreEmployeeData($params);
|
||||||
$post_data = self::getPostEmployeeData($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)) {
|
if (empty($pre_data) && empty($post_data)) {
|
||||||
|
log_message('error', 'Both pre_data and post_data are empty. Returning empty array.');
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine the latest record
|
|
||||||
$latest_key = null;
|
$latest_key = null;
|
||||||
if (!empty($pre_data) && !empty($post_data)) {
|
if (!empty($pre_data) && !empty($post_data)) {
|
||||||
$latest_key = strtotime($pre_data['created_at']) > strtotime($post_data['created_at']) ? 'pre' : 'post';
|
$latest_key = strtotime($pre_data['created_at']) > strtotime($post_data['created_at']) ? 'pre' : 'post';
|
||||||
@ -48,36 +210,43 @@ class RestAuthHelper
|
|||||||
$latest_key = 'post';
|
$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)) {
|
if (!empty($pre_data) && !empty($post_data)) {
|
||||||
$latest_data = $latest_key === 'pre' ? $pre_data : $post_data;
|
$latest_data = $latest_key === 'pre' ? $pre_data : $post_data;
|
||||||
$other_data = $latest_key === 'pre' ? $post_data : $pre_data;
|
$other_data = $latest_key === 'pre' ? $post_data : $pre_data;
|
||||||
|
|
||||||
if ($latest_data['client_short_name'] === $other_data['client_short_name']) {
|
if ($latest_data['client_short_name'] === $other_data['client_short_name']) {
|
||||||
return [
|
log_message('error', 'client_short_name match. Returning both records.');
|
||||||
'pre' => $pre_data,
|
return ['pre' => $pre_data, 'post' => $post_data];
|
||||||
'post' => $post_data,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If one of the data is missing or client names mismatch
|
$result = [
|
||||||
return [
|
|
||||||
'pre' => $latest_key === 'pre' ? $pre_data : [],
|
'pre' => $latest_key === 'pre' ? $pre_data : [],
|
||||||
'post' => $latest_key === 'post' ? $post_data : [],
|
'post' => $latest_key === 'post' ? $post_data : [],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
log_message('error', 'Returning data: ' . json_encode($result));
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getPreEmployeeData(array $params)
|
public static function getPreEmployeeData(array $params)
|
||||||
{
|
{
|
||||||
|
log_message('error', 'Function getPreEmployeeData called with: ' . json_encode($params));
|
||||||
|
|
||||||
$employeeModel = new EmployeeModel();
|
$employeeModel = new EmployeeModel();
|
||||||
|
|
||||||
$mobile_number = $params['mobile_number'] ?? null;
|
$mobile_number = $params['mobile_number'] ?? null;
|
||||||
$email_id = $params['email_id'] ?? null;
|
$email_id = $params['email_id'] ?? null;
|
||||||
$otp = $params['otp'] ?? null;
|
$otp = $params['otp'] ?? null;
|
||||||
$old_mpin = $params['old_mpin'] ?? null;
|
$old_mpin = $params['old_mpin'] ?? null;
|
||||||
|
|
||||||
|
$employeeData = null;
|
||||||
|
|
||||||
if (!empty($mobile_number)) {
|
if (!empty($mobile_number)) {
|
||||||
|
log_message('error', 'Searching employee by mobile_number: ' . $mobile_number);
|
||||||
|
|
||||||
$builder = $employeeModel
|
$builder = $employeeModel
|
||||||
->select('employees.id as employee_id, employees.*')
|
->select('employees.id as employee_id, employees.*')
|
||||||
->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner')
|
->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner')
|
||||||
@ -93,8 +262,9 @@ class RestAuthHelper
|
|||||||
}
|
}
|
||||||
|
|
||||||
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
|
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
|
||||||
|
|
||||||
} elseif (!empty($email_id)) {
|
} elseif (!empty($email_id)) {
|
||||||
|
log_message('error', 'Searching employee by email_id: ' . $email_id);
|
||||||
|
|
||||||
$builder = $employeeModel
|
$builder = $employeeModel
|
||||||
->select('employees.id as employee_id, employees.*')
|
->select('employees.id as employee_id, employees.*')
|
||||||
->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner')
|
->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner')
|
||||||
@ -114,15 +284,18 @@ class RestAuthHelper
|
|||||||
}
|
}
|
||||||
|
|
||||||
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
|
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
log_message('warning', 'No mobile or email found. Returning null.');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($employeeData){
|
log_message('error', 'Pre employee data fetched: ' . json_encode($employeeData));
|
||||||
|
|
||||||
|
if ($employeeData) {
|
||||||
$clientModel = new ClientModel;
|
$clientModel = new ClientModel;
|
||||||
$client_data = $clientModel->where('is_active', 1)->where('id', $employeeData['client_id'])->first();
|
$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;
|
return $employeeData;
|
||||||
@ -130,15 +303,14 @@ class RestAuthHelper
|
|||||||
|
|
||||||
public static function getPostEmployeeData(array $params)
|
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;
|
$mobile_number = $params['mobile_number'] ?? null;
|
||||||
$email_id = $params['email_id'] ?? null;
|
$email_id = $params['email_id'] ?? null;
|
||||||
$otp = $params['otp'] ?? null;
|
$otp = $params['otp'] ?? null;
|
||||||
$old_mpin = $params['old_mpin'] ?? null;
|
$old_mpin = $params['old_mpin'] ?? null;
|
||||||
|
|
||||||
|
|
||||||
if (!empty($mobile_number) || !empty($email_id)) {
|
if (!empty($mobile_number) || !empty($email_id)) {
|
||||||
|
|
||||||
$client = \Config\Services::curlrequest();
|
$client = \Config\Services::curlrequest();
|
||||||
$endPoint = 'getPostEmployeeDataForAuth';
|
$endPoint = 'getPostEmployeeDataForAuth';
|
||||||
$url = env('POST_ENROLLMENT_BASEURL') . $endPoint;
|
$url = env('POST_ENROLLMENT_BASEURL') . $endPoint;
|
||||||
@ -161,11 +333,23 @@ class RestAuthHelper
|
|||||||
$postData['old_mpin'] = $old_mpin;
|
$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();
|
$post_json = $response->getBody();
|
||||||
|
log_message('error', 'Response from API: ' . $post_json);
|
||||||
|
|
||||||
$post_data = json_decode($post_json, true);
|
$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.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user