FIX_COUNT_API_CHANGE : RV
This commit is contained in:
parent
a5d2cd2623
commit
d2361cdaf5
@ -3626,56 +3626,135 @@ class EmployeeRestController extends AdminController
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function getPreEmployeePolicyCount($mobile_no, $clientId)
|
||||
// public function getPreEmployeePolicyCount($mobile_no, $clientId = null)
|
||||
// {
|
||||
// log_message('error', 'getPreEmployeePolicyCount called with params : ' . json_encode(['mobile_no' => $mobile_no, 'client_id' => $clientId], true));
|
||||
|
||||
// if (empty($mobile_no)) {
|
||||
// log_message('error', 'Mobile number is empty or null.');
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// $client_short_name = [];
|
||||
// if(!empty($clientId)){
|
||||
// $client_data = $this->clientModel->where('is_active', 1)->where('id', $clientId)->first();
|
||||
// $client_short_name = $client_data['short_name'] ?? null;
|
||||
// }
|
||||
|
||||
// $post_data = [
|
||||
// 'mobile_number' => $mobile_no,
|
||||
// 'client_data' => $client_short_name
|
||||
// ];
|
||||
|
||||
// $response = $this->callThirdPartyAPI($post_data, 'getPreEmployeePolicyCount');
|
||||
// print_r( $response); die;
|
||||
|
||||
// return $response['data'] ?? 0 ;
|
||||
|
||||
|
||||
// try {
|
||||
// log_message('error', 'Attempting to connect to preDB...');
|
||||
// $db2 = \Config\Database::connect('preDB');
|
||||
// log_message('error', 'Connection to preDB successful.');
|
||||
// } catch (\Throwable $e) {
|
||||
// log_message('error', 'DB connection to preDB failed: ' . $e->getMessage());
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// try {
|
||||
// log_message('error', 'Building query to count employee policies...');
|
||||
// $builder = $db2->table('employees')
|
||||
// ->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
||||
// ->join('client_policy cp', 'employee_polices.client_policy_id = cp.id')
|
||||
// ->where('employees.is_active', 1)
|
||||
// ->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
||||
// ->where('employee_polices.is_active', 1)
|
||||
// ->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
||||
// ->where('cp.enrolment_visibility', 1)
|
||||
// ->where('cp.open_for_enrollment', 1)
|
||||
// ->whereIn('cp.policy_type_id', [1,2,6,7])
|
||||
// ->where('employees.mobile', $mobile_no)
|
||||
// ->orderBy('employees.created_at', 'desc')
|
||||
// ->groupBy('employee_polices.client_policy_id');
|
||||
|
||||
// if(!empty($clientId)){
|
||||
// $builder->where('employees.client_id', $clientId);
|
||||
// }
|
||||
|
||||
// log_message('error', 'Executing policy count query...');
|
||||
// $query = $builder->get();
|
||||
// $count = $query->getNumRows();
|
||||
// // dd($db2->getLastQuery());
|
||||
|
||||
// log_message('error', 'Policy count result: ' . $count);
|
||||
// return $count;
|
||||
|
||||
// } catch (\Throwable $e) {
|
||||
// log_message('error', 'Query failed in getPreEmployeePolicyCount: ' . $e->getMessage());
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
public function getPreEmployeePolicyCount($mobile_no, $clientId = null)
|
||||
{
|
||||
log_message('error', 'getPreEmployeePolicyCount called with mobile: ' . var_export($mobile_no, true));
|
||||
log_message('error', 'STEP 1: getPreEmployeePolicyCount called with params: ' . json_encode(['mobile_no' => $mobile_no, 'client_id' => $clientId]));
|
||||
|
||||
if (empty($mobile_no)) {
|
||||
log_message('error', 'Mobile number is empty or null.');
|
||||
log_message('error', 'STEP 2: Mobile number is empty or null. Returning 0.');
|
||||
return 0;
|
||||
}
|
||||
|
||||
try {
|
||||
log_message('error', 'Attempting to connect to preDB...');
|
||||
$db2 = \Config\Database::connect('preDB');
|
||||
log_message('error', 'Connection to preDB successful.');
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'DB connection to preDB failed: ' . $e->getMessage());
|
||||
return 0;
|
||||
$client_short_name = null;
|
||||
|
||||
if (!empty($clientId)) {
|
||||
log_message('error', 'STEP 3: Fetching client short name for client ID: ' . $clientId);
|
||||
$client_data = $this->clientModel->where('is_active', 1)->where('id', $clientId)->first();
|
||||
|
||||
if ($client_data) {
|
||||
$client_short_name = $client_data['short_name'] ?? null;
|
||||
log_message('error', 'STEP 4: Found client short name: ' . $client_short_name);
|
||||
} else {
|
||||
log_message('error', 'STEP 4: No client found for ID: ' . $clientId);
|
||||
}
|
||||
} else {
|
||||
log_message('error', 'STEP 3: No clientId provided. Skipping client lookup.');
|
||||
}
|
||||
|
||||
$post_data = [
|
||||
'mobile_number' => $mobile_no,
|
||||
'client_short_name' => $client_short_name
|
||||
];
|
||||
|
||||
log_message('error', 'STEP 5: Calling third-party API with payload: ' . json_encode($post_data));
|
||||
|
||||
try {
|
||||
log_message('error', 'Building query to count employee policies...');
|
||||
$builder = $db2->table('employees')
|
||||
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
||||
->join('client_policy cp', 'employee_polices.client_policy_id = cp.id')
|
||||
->where('employees.is_active', 1)
|
||||
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
||||
->where('employee_polices.is_active', 1)
|
||||
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
||||
->where('cp.enrolment_visibility', 1)
|
||||
->where('cp.open_for_enrollment', 1)
|
||||
->whereIn('cp.policy_type_id', [1,2,6,7])
|
||||
->where('employees.mobile', $mobile_no)
|
||||
->orderBy('employees.created_at', 'desc')
|
||||
->groupBy('employee_polices.client_policy_id');
|
||||
|
||||
if(!empty($clientId)){
|
||||
$builder->where('employees.client_id', $clientId);
|
||||
$response = $this->callThirdPartyAPI($post_data, 'getPreEmployeePolicyCount');
|
||||
log_message('error', 'STEP 6: API raw response: ' . $response);
|
||||
|
||||
$response = json_decode($response, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
log_message('error', 'STEP 7: JSON decoding failed: ' . json_last_error_msg());
|
||||
return 0;
|
||||
}
|
||||
|
||||
log_message('error', 'Executing policy count query...');
|
||||
$query = $builder->get();
|
||||
$count = $query->getNumRows();
|
||||
$count = $response['data'] ?? 0;
|
||||
log_message('error', 'STEP 8: Final count extracted: ' . $count);
|
||||
|
||||
log_message('error', 'Policy count result: ' . $count);
|
||||
return $count;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'Query failed in getPreEmployeePolicyCount: ' . $e->getMessage());
|
||||
log_message('error', 'STEP 6: API call failed: ' . $e->getMessage());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private function callThirdPartyAPI($postData , $endPoint)
|
||||
{
|
||||
$client = \Config\Services::curlrequest();
|
||||
$url = env('PRE_ENROLLMENT_BASEURL').$endPoint;
|
||||
$response = $client->post( $url, ['json' => $postData, 'http_errors' => false ] );
|
||||
// return json_decode($response->getBody(), true);
|
||||
return $response->getBody();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user