diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 91f0964..17527ac 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -94,6 +94,7 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) { $routes->get('enquiry/getQuickQuoteEnquiryList', 'EnquiryController::getQuickQuoteEnquiryList'); $routes->get('enquiry/updateEnquiryInProgress', 'EnquiryController::updateEnquiryInProgress'); $routes->get('enquiry/updateEnquiryStatus', 'EnquiryController::updateEnquiryStatus'); + $routes->get('enquiry/checkDuplicate', 'EnquiryController::checkDuplicate'); //Proposal diff --git a/app/Controllers/EnquiryController.php b/app/Controllers/EnquiryController.php index 6d8fad4..d69ec3c 100644 --- a/app/Controllers/EnquiryController.php +++ b/app/Controllers/EnquiryController.php @@ -664,6 +664,62 @@ class EnquiryController extends ResourceController $this->enquiryModel->update($enquiryId, [ 'is_active' => $isActive]); return $this->respond(['status' => 'success','data' => $enquiryId ], 200); } + + public function checkDuplicate() + { + $request = $this->request->getGet(); + + // Input field => DB column mapping + $fieldMapping = [ + "reg_no" => "vehicle_no", // frontend → database + "mobile" => "mobile", + "email" => "email", + ]; + + $conditions = []; + $inputField = null; // what user sent + $dbField = null; // actual column + + // Identify which field user sent + foreach ($fieldMapping as $input => $column) { + if (!empty($request[$input])) { + $inputField = $input; + $dbField = $column; + $conditions[$column] = $request[$input]; + break; + } + } + + if (!$dbField) { + return $this->response->setJSON([ + 'status' => 'error', + 'message' => 'One of the following is required: email, reg_no, mobile' + ])->setStatusCode(400); + } + + // Query builder + $builder = $this->enquiryModel->where($dbField, $conditions[$dbField]); + + // Exclude this ID if provided + if (!empty($request['id'])) { + $builder->where('id !=', $request['id']); + } + + $enquiry = $builder->first(); + + if ($enquiry) { + return $this->response->setJSON([ + 'status' => 'exists', + 'field' => $inputField // return the field user sent + ]); + } else { + return $this->response->setJSON([ + 'status' => 'not', + 'field' => $inputField + ]); + } + } +