diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 7d412c5..d2eb167 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -153,11 +153,11 @@ class ClientController extends AdminController public function validateDuplicateByClientBranch() { - $request = service('request'); - $value = $request->getPost('value'); - $clientId = $request->getPost('client_id'); - $branchId = $request->getPost('branch_id'); - $isDuplicate = $this->clientModel->isDuplicateByClientBranch($value, $clientId, $branchId); + $value = $this->request->getPost('value'); + $clientId = $this->request->getPost('client_id'); + $branchId = $this->request->getPost('branch_id'); + $field = $this->request->getPost('field'); + $isDuplicate = $this->clientModel->isDuplicateByClientBranch($value, $field, $clientId, $branchId); return $this->response->setJSON(['isDuplicate' => $isDuplicate]); } diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index 9bab925..904f54a 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -1774,53 +1774,28 @@ class EmployeeRestController extends AdminController public function getClientDetails() { - log_message('error', 'STEP 1: getClientDetails called'); - try { - // STEP 2: Extract Authorization header - $jwt = $this->request->getHeaderLine('Authorization'); - log_message('error', 'STEP 2: Authorization header: ' . $jwt); + $pre_client_id = $this->request->getGet('pre_client_id'); + $pre_branch_id = $this->request->getGet('pre_branch_id'); + $post_client_id = $this->request->getGet('post_client_id'); + $post_branch_id = $this->request->getGet('post_branch_id'); + - $jwtParts = explode(' ', $jwt); - if (count($jwtParts) < 2) { - log_message('error', 'STEP 2.1: Invalid Authorization header format'); - return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'Invalid Authorization header'], 400); - } + if ($pre_client_id != null) { + - $token = $jwtParts[1]; - log_message('error', 'STEP 3: Extracted JWT token'); - - $tokenParts = explode('.', $token); - if (count($tokenParts) !== 3) { - log_message('error', 'STEP 3.1: Invalid JWT structure'); - return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'Invalid JWT token'], 400); - } - - $decodedPayload = json_decode(base64_decode($tokenParts[1]), true); - log_message('error', 'STEP 4: Decoded JWT payload: ' . json_encode($decodedPayload)); - - $token_type = $decodedPayload['token_type'] ?? null; - log_message('error', 'STEP 5: Token type = ' . $token_type); - - $client_id = $this->request->getGet('client_id'); - $client_branch_id = $this->request->getGet('client_branch_id'); - log_message('error', 'STEP 6: Query params - client_id: ' . $client_id . ', client_branch_id: ' . $client_branch_id); - - if ($token_type === 'pre') { - log_message('error', 'STEP 7: Handling "pre" token type'); - - $client = $this->clientModel->where('id', $client_id)->first(); + $client = $this->clientModel->where('id', $pre_client_id)->first(); if ($client) { - log_message('error', 'STEP 8: Found client: ' . json_encode($client)); + $client['client_logo'] = base_url() . 'public/uploads/logo/' . $client['client_logo']; $clientPolicy = $this->clientPolicyModel - ->where('client_id', $client_id) - ->where('client_branch_id', $client_branch_id) + ->where('client_id', $pre_client_id) + ->where('client_branch_id', $pre_branch_id) ->findAll(); - log_message('error', 'STEP 9: Found client policy count: ' . count($clientPolicy)); + return $this->respond([ 'status' => 'success', @@ -1831,33 +1806,34 @@ class EmployeeRestController extends AdminController ] ], 200); } else { - log_message('error', 'STEP 10: No client found for client_id: ' . $client_id); + return $this->respond(['status' => 'failed', 'code' => 404, 'data' => []], 404); } - } elseif ($token_type === 'post') { - log_message('error', 'STEP 11: Handling "post" token type'); + } elseif ($post_client_id != null) { + $restAuthController = new RestAuthenticationController; $queryParams = [ - 'client_id' => $client_id, - 'client_branch_id' => $client_branch_id + 'client_id' => $post_client_id, + 'client_branch_id' => $post_branch_id ]; - log_message('error', 'STEP 12: Calling post enrollment API with params: ' . json_encode($queryParams)); + return $restAuthController->callThirdPartyGETAPI($queryParams, 'getClientDetails'); - return $restAuthController->callThirdPartyGETAPI($queryParams, 'getClientDetails', ['token' => $jwt]); } else { - log_message('error', 'STEP 13: Unknown token_type: ' . $token_type); + return $this->respond(['status' => 'failed', 'code' => 404, 'data' => []], 404); } } catch (\Exception $e) { - log_message('error', 'STEP 14: Exception occurred - ' . $e->getMessage()); + return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500); } } + + public function getSiMappedArray($policy_id,$base_policy_id) { $selfGMC = $this->employeeModel->select('employees.name , employee_polices.basic_cover_si') diff --git a/app/Models/ClientModel.php b/app/Models/ClientModel.php index d7708b7..89c77d6 100755 --- a/app/Models/ClientModel.php +++ b/app/Models/ClientModel.php @@ -214,12 +214,12 @@ class ClientModel extends Model return $result; } - public function isDuplicateByClientBranch($email, $clientId, $branchId) + public function isDuplicateByClientBranch($value, $field, $clientId, $branchId) { $builder = $this->db->table('level_contacts lc') ->select('lc.id') ->join('client_branch cb', 'lc.ref_id = cb.id', 'left') - ->where('lc.email', $email) + ->where('lc.'.$field, $value) ->where('lc.contact_type', 'client') ->where('cb.client_id', $clientId) ->where('lc.ref_id', $branchId) diff --git a/app/Views/client_branch.php b/app/Views/client_branch.php index fe817b3..449d1e8 100755 --- a/app/Views/client_branch.php +++ b/app/Views/client_branch.php @@ -73,7 +73,7 @@
+ class="btn btn-primary waves-effect waves-light btn-sm "> Fetch Client Branch (Post-Enrolment)
@@ -207,12 +207,12 @@
+ name="email[]" id="email" data-parsley-trigger="change" data-parsley-type="email" onchange="validateDuplicateByClientBranch(this, 'email','branchBtnSubmit')" required>
- +
- +
@@ -945,54 +948,116 @@ function validateInput(input, table, field, submitBtnId){ } -function validateDuplicateByClientBranch(input, submitButId) { +function validateDuplicateByClientBranch(input, field, submitButId) { let value = $(input).val().trim(); let clientId = $('#client_id_branch').val(); let branchId = $('#branch_id_primarykey').val(); - console.log(`Ln968 cId: ${clientId} | bId: ${branchId}`); - + let label = $(input).closest('.form-group').find('label').text().replace('*', '').trim(); + let message = label ? label + " is duplicate!" : "Value is duplicate!"; + + console.log(`cId: ${clientId} | bId: ${branchId}`); + // Don't forgot be careful // 1 Local duplication check (User entered) let isLocalDuplicate = false; - $('input[name="email[]"]').each(function() { - if (this !== input && $(this).val().trim() === value && value !== '') { + $('input[name="' + field + '[]"]').each(function(index) { + let compareVal = $(this).val().trim(); + console.log(`Entered value: ${value} | Contact ${index+1} value: ${$(this).val()}`); + if (this !== input && compareVal !== '' && compareVal === value) { isLocalDuplicate = true; - return false; // break loop + return false; } }); if (isLocalDuplicate) { console.log(`r u n Local`); - toastr.warning("Email is duplicate!", 'WARNING'); + console.log(`duplicate found for ${field}`); + toastr.warning(message, 'WARNING'); $('#' + submitButId).prop('disabled', true); - return; // don’t call server if duplicate in UI + return; } - + + // important Skip empty values + if (value === '') { + checkAllFieldsValid(submitButId); + return; + } + // Don't forgot be careful // 2 Server-side duplicate check (DB) - if (!isLocalDuplicate && value !== '') { - console.log(`r u n Server`); - $.ajax({ - url: '', - type: 'POST', - data: { - client_id: clientId, - branch_id: branchId, - value: value - }, - dataType: 'json', - success: function(response) { - if (response.isDuplicate) { - toastr.warning("Email already exists!", 'WARNING'); - $('#' + submitButId).prop('disabled', true); - } else { - $('#' + submitButId).prop('disabled', false); - } - }, - error: function(xhr, status, error) { - console.error('AJAX Error:', error); + $.ajax({ + url: '', + type: 'POST', + data: { + client_id: clientId, + branch_id: branchId, + value: value, + field: field + }, + dataType: 'json', + success: function(response) { + if (response.isDuplicate) { + console.log(`r u n Server`); + console.log(`duplicate found for ${field}`); + toastr.warning(message, 'WARNING'); + $('#' + submitButId).prop('disabled', true); + } else { + console.log(`No duplicate for ${field}`); + checkAllFieldsValid(submitButId); } - }); + }, + error: function(xhr, status, error) { + console.error('AJAX Error:', error); + } + }); +} + + +// recheck all contacts before enabling submit +function checkAllFieldsValid(submitButId) { + let emailDuplicates = false; + let mobileDuplicates = false; + + // cross check all EMAIL duplicates + let emailSeen = []; + $('input[name="email[]"]').each(function() { + let val = $(this).val().trim(); + if (val && emailSeen.includes(val)) { + emailDuplicates = true; + } else if (val) { + emailSeen.push(val); + } + }); + + // cross check all MOBILE duplicates + let mobileSeen = []; + $('input[name="mobile[]"]').each(function() { + let val = $(this).val().trim(); + if (val && mobileSeen.includes(val)) { + mobileDuplicates = true; + } else if (val) { + mobileSeen.push(val); + } + }); + + if (emailDuplicates || mobileDuplicates) { + $('#' + submitButId).prop('disabled', true); + // show correct message based on what’s duplicated + if (emailDuplicates && mobileDuplicates) { + toastr.warning("Email and Mobile values are duplicate!", "WARNING"); + console.log('Both Email and Mobile duplicates'); + } else if (emailDuplicates) { + toastr.warning("Email duplicate!", "WARNING"); + console.log('Cross Check Email duplicates'); + } else if (mobileDuplicates) { + toastr.warning("Mobile duplicate!", "WARNING"); + console.log('Cross Check Mobile duplicates'); + } + console.log(`btn Dis - true`); + } else { + console.log('unique — enable'); + console.log(`btn Dis - false`); + $('#' + submitButId).prop('disabled', false); } } diff --git a/app/Views/client_onboarding.php b/app/Views/client_onboarding.php index 40cf999..dc37b06 100755 --- a/app/Views/client_onboarding.php +++ b/app/Views/client_onboarding.php @@ -115,7 +115,7 @@ body {