500 lines
18 KiB
PHP
500 lines
18 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Models\ClientModel;
|
|
use App\Models\ClientPolicyModel;
|
|
use App\Models\EmployeeModel;
|
|
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)
|
|
{
|
|
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;
|
|
$check_mpin = $params['check_mpin'] ?? null;
|
|
|
|
if (empty($mobile_number) && empty($email_id)) {
|
|
log_message('error', 'Both mobile_number and email_id are empty. Returning null.');
|
|
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);
|
|
|
|
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 [];
|
|
}
|
|
|
|
$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';
|
|
}
|
|
|
|
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']) {
|
|
log_message('error', 'client_short_name match. Returning both records.');
|
|
|
|
if ($check_mpin !== null && ($pre_data['mpin'] !== null || $post_data['mpin'] !== null)) {
|
|
|
|
// Case: Pre MPIN is missing, update it from Post
|
|
if ($pre_data['mpin'] === null && $post_data['mpin'] !== null) {
|
|
$data = [
|
|
'client_id' => $pre_data['client_id'],
|
|
'employee_id' => $pre_data['id'],
|
|
'mpin' => $post_data['mpin'],
|
|
'is_mpin_skipped' => $post_data['is_mpin_skipped'],
|
|
'is_biometric_enabled' => $post_data['is_biometric_enabled'],
|
|
];
|
|
$response = self::updatePreMpin($data);
|
|
|
|
// Case: Post MPIN is missing, update it from Pre
|
|
} elseif ($post_data['mpin'] === null && $pre_data['mpin'] !== null) {
|
|
$data = [
|
|
'client_id' => $post_data['client_id'],
|
|
'employee_id' => $post_data['id'],
|
|
'mpin' => $pre_data['mpin'],
|
|
'is_mpin_skipped' => $pre_data['is_mpin_skipped'],
|
|
'is_biometric_enabled' => $pre_data['is_biometric_enabled'],
|
|
];
|
|
$response = self::updatePostMpin($data);
|
|
}
|
|
}
|
|
|
|
return ['pre' => $pre_data, 'post' => $post_data];
|
|
}
|
|
}
|
|
|
|
$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;
|
|
$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')
|
|
->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($otp)) {
|
|
$builder->where('employees.otp', $otp);
|
|
}
|
|
|
|
if (!empty($old_mpin)) {
|
|
$builder->where('employees.mpin', $old_mpin);
|
|
}
|
|
|
|
$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')
|
|
->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 {
|
|
log_message('warning', 'No mobile or email found. Returning null.');
|
|
return null;
|
|
}
|
|
|
|
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'] ?? '';
|
|
log_message('error', 'Client short name attached: ' . $employeeData['client_short_name']);
|
|
}
|
|
|
|
return $employeeData;
|
|
}
|
|
|
|
public static function getPostEmployeeData(array $params)
|
|
{
|
|
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;
|
|
$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;
|
|
$headers = [
|
|
'App-Signature' => getenv('APP_SIGNATURE'),
|
|
];
|
|
|
|
$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;
|
|
}
|
|
|
|
log_message('error', 'Sending POST to external API: ' . $url);
|
|
log_message('error', 'POST payload: ' . json_encode($postData));
|
|
|
|
$response = $client->post($url, ['json' => $postData, 'headers' => $headers , 'http_errors' => false]);
|
|
|
|
$post_json = $response->getBody();
|
|
// log_message('error', 'Response from API: ' . $post_json);
|
|
|
|
$post_data = json_decode($post_json, true);
|
|
|
|
$data = $post_data['data'] ?? [];
|
|
log_message('error', 'Parsed post_data: ' . json_encode($data));
|
|
return $data;
|
|
}
|
|
|
|
log_message('error', 'No mobile or email present in params for post fetch.');
|
|
}
|
|
|
|
public static function updatePreMpin(array $params)
|
|
{
|
|
log_message('info', 'Function updatePostMpin called with: ' . json_encode($params));
|
|
|
|
$employee_id = $params['employee_id'] ?? null;
|
|
$mpin = $params['mpin'] ?? null;
|
|
|
|
if (empty($employee_id) || empty($mpin)) {
|
|
log_message('error', 'Missing employee_id or mpin in updatePreMpin');
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
$employeeModel = new EmployeeModel();
|
|
$result = $employeeModel
|
|
->where('id', $employee_id)
|
|
->set('mpin', $mpin)
|
|
->update();
|
|
|
|
log_message('info', 'MPIN update status (Pre): ' . var_export($result, true));
|
|
return $result;
|
|
} catch (\Exception $e) {
|
|
log_message('error', 'Exception in updatePreMpin: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function updatePostMpin(array $params)
|
|
{
|
|
if (isset($params['mpin']) && isset($params['employee_id'])) {
|
|
|
|
$client = \Config\Services::curlrequest();
|
|
$endPoint = 'updateEmpMPIN';
|
|
$url = env('POST_ENROLLMENT_BASEURL') . $endPoint;
|
|
|
|
log_message('info', '[updatePostMpin] Sending POST to external API: ' . $url);
|
|
log_message('debug', '[updatePostMpin] Request Payload: ' . json_encode($params));
|
|
|
|
$response = $client->post($url, ['json' => $params, 'http_errors' => false]);
|
|
|
|
$post_json = $response->getBody();
|
|
log_message('info', '[updatePostMpin] Raw API Response: ' . $post_json);
|
|
|
|
$post_data = json_decode($post_json, true);
|
|
|
|
$data = $post_data['data'] ?? [];
|
|
log_message('debug', '[updatePostMpin] Parsed Response Data: ' . json_encode($data));
|
|
|
|
return $data;
|
|
}
|
|
|
|
log_message('warning', '[updatePostMpin] Missing required parameters: employee_id or mpin');
|
|
return false;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------------------------
|
|
|
|
public static function getRetailUserData(array $params)
|
|
{
|
|
log_message('error', 'Function getRetailUserData 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;
|
|
|
|
if (!empty($mobile_number) || !empty($email_id)) {
|
|
|
|
$client = \Config\Services::curlrequest();
|
|
$endPoint = 'getRetailUserData';
|
|
$url = env('POST_ENROLLMENT_BASEURL') . $endPoint;
|
|
$headers = [
|
|
'App-Signature' => getenv('APP_SIGNATURE'),
|
|
];
|
|
|
|
$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;
|
|
}
|
|
|
|
log_message('error', 'Sending POST to external API: ' . $url);
|
|
log_message('error', 'POST payload: ' . json_encode($postData));
|
|
|
|
$response = $client->post($url, ['json' => $postData, 'headers' => $headers, 'http_errors' => false]);
|
|
|
|
$post_json = $response->getBody();
|
|
// print_r($post_json);
|
|
|
|
$post_data = json_decode($post_json, true);
|
|
|
|
$data = $post_data['data'] ?? [];
|
|
log_message('error', 'Parsed post_data: ' . json_encode($data));
|
|
return $data;
|
|
}
|
|
|
|
return [];
|
|
}
|
|
}
|