diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 457388ff..ddb9af3f 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -488,6 +488,7 @@ $routes->group("/api", ["filter" => "authJWT"], function ($routes) { $routes->post("employeeRest/saveMpin", "RestAuthenticationController::saveMpin"); $routes->post("employeeRest/updateMpin", "RestAuthenticationController::updateMpin"); +$routes->post("employeeRest/getPostEmployeeDataForAuth", "RestAuthenticationController::getPostEmployeeDataForAuth"); $routes->group("employeeRest", ["filter" => "authJWT"], function ($routes) { diff --git a/app/Controllers/RestAuthenticationController.php b/app/Controllers/RestAuthenticationController.php index e31ecb2a..2e234f5c 100755 --- a/app/Controllers/RestAuthenticationController.php +++ b/app/Controllers/RestAuthenticationController.php @@ -22,6 +22,7 @@ use App\Models\EmployeeModel; use App\Models\AuthHistoryModel; use App\Models\LevelContactModel; use App\Models\HRAccessControlModel; +use App\Models\ClientModel; use Firebase\JWT\JWT; // require_once('../vendor/autoload.php'); @@ -37,6 +38,7 @@ class RestAuthenticationController extends AdminController protected $authHistoryModel; protected $hrModel; protected $hrAccessControlModel; + protected $clientModel; public function __construct() @@ -48,6 +50,7 @@ class RestAuthenticationController extends AdminController $this->authHistoryModel = new AuthHistoryModel(); $this->hrModel = new LevelContactModel(); $this->hrAccessControlModel = new HRAccessControlModel(); + $this->clientModel = new ClientModel(); } @@ -103,6 +106,7 @@ class RestAuthenticationController extends AdminController } } catch (\Throwable $th) { + $this->myLogger->logme("error", ($th->getMessage().' --- '.$th->getLine())); return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500); } } @@ -110,10 +114,13 @@ class RestAuthenticationController extends AdminController public function verifyEmployeeWithEmailId() { try { + + log_message('error', json_encode($this->request->getJSON())); $email = $this->request->getJSON()->email; + $client_id = $this->request->getJSON()->client_id ?? null; - $employeeData = $this->employeeModel->select(' + $builder = $this->employeeModel->select(' employees.relationship, EP.employee_id, employees.client_id, @@ -124,19 +131,38 @@ class RestAuthenticationController extends AdminController ->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner') ->where('employees.is_active', 1) ->where('employees.relationship', 'Self') - ->where('employees.emp_status !=', 'truncated') + ->whereIn('employees.emp_status', ['active', 'expired']) ->where('employees.email_corporate', $email) ->where('EP.is_active', 1) - ->whereIn('EP.status', ['active', 'expired']) - ->first(); + ->whereIn('EP.status', ['active', 'expired']); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $builder->orderBy('employees.id', 'desc'); + $employeeData = $builder->first(); if (isset($employeeData['employee_id'])) { $otp = random_int(100000, 999999); - $update = $this->employeeModel->where('email_corporate', $email)->where('relationship', 'Self') - ->where('is_active', 1)->set(array('otp' => $otp)) - ->update(); + // $update = $this->employeeModel->where('email_corporate', $email)->where('relationship', 'Self') + // ->where('is_active', 1)->set(array('otp' => $otp)) + // ->update(); + + $builder = $this->employeeModel + ->where('email_corporate', $email) + ->where('relationship', 'Self') + ->where('is_active', 1) + ->where('id', $employeeData['employee_id']); + + if (!empty($client_id)) { + $builder->where('client_id', $client_id); + } + + $update = $builder->set(['otp' => $otp])->update(); + if ($update) { $common = [ @@ -164,11 +190,14 @@ class RestAuthenticationController extends AdminController $result = ['user_verification' => false, 'message' => "Verification failed , try again"]; return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200); } + } else { $result = ['user_verification' => false, 'message' => "User not found"]; return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200); } + } catch (\Throwable $th) { + $this->myLogger->logme("error", ($th->getMessage().' --- '.$th->getLine())); return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $th], 500); } } @@ -178,10 +207,24 @@ class RestAuthenticationController extends AdminController $email = $this->request->getJSON()->email; $otp = $this->request->getJSON()->otp; - - $this->employeeModel->where('email_corporate', $email)->where('relationship', 'Self') - ->where('is_active', 1)->set(array('otp' => $otp)) - ->update(); + $client_id = $this->request->getJSON()->client_id ?? null; + $employee_id = $this->request->getJSON()->employee_id ?? null; + + $builder = $this->employeeModel + ->where('email_corporate', $email) + ->where('relationship', 'Self') + ->where('is_active', 1); + + if (!empty($client_id)) { + $builder->where('client_id', $client_id); + } + + if (!empty($employee_id)) { + $builder->where('id', $employee_id); + } + + $builder->set(['otp' => $otp])->update(); + return true; @@ -333,6 +376,7 @@ class RestAuthenticationController extends AdminController $otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null; $email_id = isset($this->request->getJSON()->email_id) ? $this->request->getJSON()->email_id : null; + $client_id = $this->request->getJSON()->client_id ?? null; if (isset($this->request->getJSON()->login_by_hr)) { @@ -341,7 +385,7 @@ class RestAuthenticationController extends AdminController if (isset($mobile_number)) { // $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->where('emp_status !=', 'truncated')->where('is_active', 1)->first(); - $employeeData = $this->employeeModel + $builder = $this->employeeModel ->select('employees.*') ->join('employee_polices', 'employees.id = employee_polices.employee_id') ->where('employees.mobile', $mobile_number) @@ -349,22 +393,32 @@ class RestAuthenticationController extends AdminController ->where('employee_polices.is_active', 1) ->whereIn('employee_polices.status', ['active', 'expired']) ->where('employees.is_active', 1) - ->whereIn('employees.emp_status', ['active', 'expired']) - ->where('employees.emp_status !=', 'truncated') - ->first(); + ->whereIn('employees.emp_status', ['active', 'expired']); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); + } else { // $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('otp', $otp)->where('relationship', 'self')->where('emp_status !=', 'truncated')->where('is_active', 1)->first(); - $employeeData = $this->employeeModel + $builder = $this->employeeModel ->select('employees.*') ->join('employee_polices', 'employees.id = employee_polices.employee_id') - ->where('employees.email_corporate', $email_id) + ->where('employees.email_corporate', $email_id) ->where('employees.relationship', 'Self') ->where('employee_polices.is_active', 1) ->whereIn('employee_polices.status', ['active', 'expired']) ->where('employees.is_active', 1) ->whereIn('employees.emp_status', ['active', 'expired']) - ->where('otp', $otp) - ->first(); + ->where('otp', $otp); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); } } @@ -622,23 +676,29 @@ class RestAuthenticationController extends AdminController $mobile_number = isset($this->request->getJSON()->mobile_number) ? $this->request->getJSON()->mobile_number : null; $email_id = isset($this->request->getJSON()->email_id) ? $this->request->getJSON()->email_id : null; + $client_id = $this->request->getJSON()->client_id ?? null; if (isset($mobile_number)) { // $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first(); - $employeeData = $this->employeeModel - ->select('employees.*') - ->join('employee_polices', 'employees.id = employee_polices.employee_id') - ->where('employees.mobile', $mobile_number) - ->where('employees.relationship', 'Self') - ->where('employee_polices.is_active', 1) - ->whereIn('employee_polices.status', ['active', 'expired']) - ->where('employees.is_active', 1) - ->whereIn('employees.emp_status', ['active', 'expired']) - ->orderBy('id', 'desc') - ->first(); + $builder = $this->employeeModel + ->select('employees.*') + ->join('employee_polices', 'employees.id = employee_polices.employee_id') + ->where('employees.mobile', $mobile_number) + ->where('employees.relationship', 'Self') + ->where('employee_polices.is_active', 1) + ->whereIn('employee_polices.status', ['active', 'expired']) + ->where('employees.is_active', 1) + ->whereIn('employees.emp_status', ['active', 'expired']); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); + } else { // $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first(); - $employeeData = $this->employeeModel + $builder = $this->employeeModel ->select('employees.*') ->join('employee_polices', 'employees.id = employee_polices.employee_id') ->where('employees.email_corporate', $email_id) @@ -646,9 +706,13 @@ class RestAuthenticationController extends AdminController ->where('employee_polices.is_active', 1) ->whereIn('employee_polices.status', ['active', 'expired']) ->where('employees.is_active', 1) - ->whereIn('employees.emp_status', ['active', 'expired']) - ->orderBy('id', 'desc') - ->first(); + ->whereIn('employees.emp_status', ['active', 'expired']); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); } $mpin = $this->request->getJSON()->mpin; @@ -681,6 +745,7 @@ class RestAuthenticationController extends AdminController } } catch (\Throwable $th) { + $this->myLogger->logme("error", ($th->getMessage().' --- '.$th->getLine())); return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500); } } @@ -693,6 +758,7 @@ class RestAuthenticationController extends AdminController $email_id = isset($this->request->getJSON()->email_id) ? $this->request->getJSON()->email_id : null; $old_mpin = $this->request->getJSON()->old_mpin; $mpin = $this->request->getJSON()->new_mpin; + $client_id = $this->request->getJSON()->client_id ?? null; // if (isset($mobile_number)) // { @@ -703,7 +769,7 @@ class RestAuthenticationController extends AdminController if (isset($mobile_number)) { // $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('mpin', $old_mpin)->where('relationship', 'self')->first(); - $employeeData = $this->employeeModel + $builder = $this->employeeModel ->select('employees.*') ->join('employee_polices', 'employees.id = employee_polices.employee_id') ->where('employees.mobile', $mobile_number) @@ -712,12 +778,17 @@ class RestAuthenticationController extends AdminController ->whereIn('employee_polices.status', ['active']) ->where('employees.is_active', 1) ->whereIn('employees.emp_status', ['active']) - ->where('employees.mpin', $old_mpin) - ->orderBy('id', 'desc') - ->first(); + ->where('employees.mpin', $old_mpin); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); + } else { // $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('mpin', $old_mpin)->where('relationship', 'self')->first(); - $employeeData = $this->employeeModel + $builder = $this->employeeModel ->select('employees.*') ->join('employee_polices', 'employees.id = employee_polices.employee_id') ->where('employees.email_corporate', $email_id) @@ -726,9 +797,13 @@ class RestAuthenticationController extends AdminController ->whereIn('employee_polices.status', ['active']) ->where('employees.is_active', 1) ->whereIn('employees.emp_status', ['active']) - ->where('employees.mpin', $old_mpin) - ->orderBy('id', 'desc') - ->first(); + ->where('employees.mpin', $old_mpin); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); } @@ -750,6 +825,7 @@ class RestAuthenticationController extends AdminController } } catch (\Throwable $th) { + $this->myLogger->logme("error", ($th->getMessage().' --- '.$th->getLine())); return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500); } } @@ -823,6 +899,7 @@ class RestAuthenticationController extends AdminController $mobile_number = $requestData['mobile_number'] ?? null; $email_id = $requestData['email_id'] ?? null; $mpin = $requestData['mpin'] ?? null; + $client_id = $requestData['client_id'] ?? null; // print_r($mpin); if (!$mpin) { @@ -830,7 +907,8 @@ class RestAuthenticationController extends AdminController } if ($mobile_number) { - $employeeData = $this->employeeModel + + $builder = $this->employeeModel ->select('employees.*') ->join('employee_polices', 'employees.id = employee_polices.employee_id') ->where('employees.mobile', $mobile_number) @@ -838,10 +916,18 @@ class RestAuthenticationController extends AdminController ->where('employee_polices.is_active', 1) ->whereIn('employee_polices.status', ['active']) ->where('employees.is_active', 1) - ->whereIn('employees.emp_status', ['active']) - ->first(); + ->whereIn('employees.emp_status', ['active']); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); + + } elseif ($email_id) { - $employeeData = $this->employeeModel + + $builder = $this->employeeModel ->select('employees.*') ->join('employee_polices', 'employees.id = employee_polices.employee_id') ->where('employees.email_corporate', $email_id) @@ -849,8 +935,14 @@ class RestAuthenticationController extends AdminController ->where('employee_polices.is_active', 1) ->whereIn('employee_polices.status', ['active']) ->where('employees.is_active', 1) - ->whereIn('employees.emp_status', ['active']) - ->first(); + ->whereIn('employees.emp_status', ['active']); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); + } else { return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'Mobile number or Email ID is required'], 400); } @@ -885,6 +977,7 @@ class RestAuthenticationController extends AdminController ], 200); } } catch (\Exception $e) { + $this->myLogger->logme("error", ($e->getMessage().' --- '.$e->getLine())); return $this->respond([ 'status' => 'failed', 'code' => 500, @@ -899,11 +992,13 @@ class RestAuthenticationController extends AdminController $mobile_number = isset($this->request->getJSON()->mobile_number) ? $this->request->getJSON()->mobile_number : null; $email_id = isset($this->request->getJSON()->email_id) ? $this->request->getJSON()->email_id : null; // $mpin = $this->request->getJSON()->mpin; + $client_id = $this->request->getJSON()->client_id ?? null; + if (isset($mobile_number)) { // $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first(); - $employeeData = $this->employeeModel + $builder = $this->employeeModel ->select('employees.*') ->join('employee_polices', 'employees.id = employee_polices.employee_id') ->where('employees.mobile', $mobile_number) @@ -911,12 +1006,16 @@ class RestAuthenticationController extends AdminController ->where('employee_polices.is_active', 1) ->whereIn('employee_polices.status', ['active', 'expired']) ->where('employees.is_active', 1) - ->whereIn('employees.emp_status', ['active', 'expired']) - ->orderBy('id', 'desc') - ->first(); + ->whereIn('employees.emp_status', ['active', 'expired']); + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); + }else{ // $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first(); - $employeeData = $this->employeeModel + $builder = $this->employeeModel ->select('employees.*') ->join('employee_polices', 'employees.id = employee_polices.employee_id') ->where('employees.email_corporate', $email_id) @@ -924,9 +1023,13 @@ class RestAuthenticationController extends AdminController ->where('employee_polices.is_active', 1) ->whereIn('employee_polices.status', ['active', 'expired']) ->where('employees.is_active', 1) - ->whereIn('employees.emp_status', ['active', 'expired']) - ->orderBy('id', 'desc') - ->first(); + ->whereIn('employees.emp_status', ['active', 'expired']); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); } @@ -937,6 +1040,7 @@ class RestAuthenticationController extends AdminController return $this->respond(['status' => 'failed','code' => 404,'data' => "Mpin - not found", 'Mpin' =>null],200); } } catch (\Exception $e) { + $this->myLogger->logme("error", ($e->getMessage().' --- '.$e->getLine())); return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()],500); } } @@ -949,6 +1053,7 @@ class RestAuthenticationController extends AdminController $json = $this->request->getJSON(); $mobile_number = $json->mobile_number ?? null; $email_id = $json->email_id ?? null; + $client_id = $json->client_id ?? null; log_message('info', 'Received input - Mobile: ' . var_export($mobile_number, true) . ', Email: ' . var_export($email_id, true)); @@ -964,20 +1069,28 @@ class RestAuthenticationController extends AdminController // Step 2: Fetch employee data if ($mobile_number) { + log_message('info', 'Looking up employee by mobile number: ' . $mobile_number); - $employeeData = $this->employeeModel + $builder = $this->employeeModel ->select('employees.*') ->join('employee_polices', 'employees.id = employee_polices.employee_id') - ->where('employees.mobile', $mobile_number) + ->where('employees.mobile', $mobile_number) ->where('employees.relationship', 'Self') ->where('employee_polices.is_active', 1) ->where('employee_polices.status', ['active']) ->where('employees.is_active', 1) - ->where('employees.emp_status', ['active']) - ->first(); + ->where('employees.emp_status', ['active']); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); + } else { + log_message('info', 'Looking up employee by email: ' . $email_id); - $employeeData = $this->employeeModel + $builder = $this->employeeModel ->select('employees.*') ->join('employee_polices', 'employees.id = employee_polices.employee_id') ->where('employees.email_corporate', $email_id) @@ -985,8 +1098,13 @@ class RestAuthenticationController extends AdminController ->where('employee_polices.is_active', 1) ->where('employee_polices.status', ['active']) ->where('employees.is_active', 1) - ->where('employees.emp_status', ['active']) - ->first(); + ->where('employees.emp_status', ['active']); + + if (!empty($client_id)) { + $builder->where('employees.client_id', $client_id); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); } log_message('info', '$employeeData : ' . json_encode($employeeData)); @@ -1028,7 +1146,7 @@ class RestAuthenticationController extends AdminController log_message('warning', 'Employee not found locally. Falling back to third-party API.'); return $this->respond(['status' => 'failed','code' => 404,'data' => "Employee not found"],200); } } catch (\Throwable $e) { - log_message('error', 'Exception in forgotMPIN(): ' . $e->getMessage()); + $this->myLogger->logme("error", ($e->getMessage().' --- '.$e->getLine())); return $this->respond([ 'status' => 'failed', 'code' => 500, @@ -1063,6 +1181,81 @@ class RestAuthenticationController extends AdminController return redirect()->to($url); } + public function getPostEmployeeDataForAuth() + { + $params = $this->request->getJSON(true); + // 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)) { + return $this->respond([ + 'status' => 'failed', + 'message' => 'Mobile number or Email ID is required.', + 'data' => [], + ], 400); + } + + if (!empty($mobile_number)) { + + $builder = $this->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', ['active', 'expired']) + ->where('employees.mobile', $mobile_number) + ->where('EP.is_active', 1) + ->whereIn('EP.status', ['active', 'expired']); + + if (!empty($old_mpin)) { + $builder->where('employees.mpin', $old_mpin); + } + + $employeeData = $builder->orderBy('employees.id', 'desc')->first(); + + } else { + $builder = $this->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', ['active', 'expired']) + ->where('employees.email_corporate', $email_id) + ->where('EP.is_active', 1) + ->whereIn('EP.status', ['active', 'expired']); + + 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(); + } + + if ($employeeData) { + + $client_data = $this->clientModel->where('is_active', 1)->where('id', $employeeData['client_id'])->first(); + $employeeData['client_short_name'] = $client_data['short_name']; + + return $this->respond([ + 'status' => 'success', + 'data' => $employeeData, + ], 200); + } + + return $this->respond([ + 'status' => 'failed', + 'message' => 'No employee found.', + 'data' => [], + ], 404); + }