diff --git a/app/Config/Routes.php b/app/Config/Routes.php index d640d843..10fb373c 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -172,6 +172,7 @@ $routes->group("/client", ["filter" => "authMVC"], function ($routes) { $routes->group("others", ["filter" => "authMVC"], function ($routes) { $routes->post("create", "ClientController::createOtherTabContent"); + $routes->post('check-duplicate', 'ClientController::validateDuplicateByClientBranch'); }); }); diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 63ddac11..fb0c5d6f 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -132,6 +132,15 @@ 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); + return $this->response->setJSON(['isDuplicate' => $isDuplicate]); + } public function checkDuplicateTableFieldValue() { $table = $this->request->getPost('table'); diff --git a/app/Models/ClientModel.php b/app/Models/ClientModel.php index 9abe8929..09db986d 100755 --- a/app/Models/ClientModel.php +++ b/app/Models/ClientModel.php @@ -226,5 +226,18 @@ class ClientModel extends Model return $result; } + public function isDuplicateByClientBranch($email, $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.contact_type', 'client') + ->where('cb.client_id', $clientId) + ->where('lc.ref_id', $branchId) + ->get(); + + return $builder->getNumRows() > 0 ? true : false; + } } diff --git a/app/Views/client_branch.php b/app/Views/client_branch.php index f35d5f99..4db64b3b 100755 --- a/app/Views/client_branch.php +++ b/app/Views/client_branch.php @@ -649,7 +649,7 @@ function appendContactHtml(contact = false, reset = false) {
- +
@@ -972,6 +972,57 @@ function validateInput(input, table, field, submitButId){ } +function validateDuplicateByClientBranch(input, 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}`); + + // 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 !== '') { + isLocalDuplicate = true; + return false; // break loop + } + }); + + if (isLocalDuplicate) { + console.log(`r u n Local`); + toastr.warning("Email is duplicate!", 'WARNING'); + $('#' + submitButId).prop('disabled', true); + return; // don’t call server if duplicate in UI + } + + // 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); + } + }); + } +} + function getContactsData() { const contacts = [];