From 78ff10c5bb094eeb311c6035153725491d1ee428 Mon Sep 17 00:00:00 2001 From: mohanmd Date: Mon, 15 Jun 2026 17:58:26 +0530 Subject: [PATCH] employer page issue count --- .../Controllers/Admin/EmployerController.php | 1 + .../Controllers/Api/WorkerAuthController.php | 56 +++++++++++++++++-- app/Services/OcrDocumentService.php | 8 ++- resources/js/Pages/Admin/Employers/Index.jsx | 3 +- tests/Feature/WorkerJourneyApiTest.php | 42 ++++++++++++++ 5 files changed, 102 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Admin/EmployerController.php b/app/Http/Controllers/Admin/EmployerController.php index 2482d1c..162a0ee 100644 --- a/app/Http/Controllers/Admin/EmployerController.php +++ b/app/Http/Controllers/Admin/EmployerController.php @@ -67,6 +67,7 @@ public function index(Request $request) return Inertia::render('Admin/Employers/Index', [ 'employers' => $employers, + 'sponsors' => $employers, 'filters' => $request->only(['search', 'status', 'location', 'sort_field', 'sort_order']) ]); } diff --git a/app/Http/Controllers/Api/WorkerAuthController.php b/app/Http/Controllers/Api/WorkerAuthController.php index d1a14da..6553e43 100644 --- a/app/Http/Controllers/Api/WorkerAuthController.php +++ b/app/Http/Controllers/Api/WorkerAuthController.php @@ -298,7 +298,7 @@ public function setupProfile(Request $request) public function register(Request $request) { $validator = Validator::make($request->all(), [ - 'name' => 'required|string|max:255', + 'name' => 'nullable|string|max:255', 'phone' => 'required|string|max:50|unique:workers,phone', 'salary' => 'required|numeric|min:0', 'password' => 'required|string|min:6', @@ -352,6 +352,10 @@ public function register(Request $request) 'issue_date' => now()->subYears(2)->toDateString(), ]; + $extractedAge = null; + $extractedName = null; + $extractedNationality = null; + if ($request->hasFile('passport_file')) { $file = $request->file('passport_file'); $ocrRes = \App\Services\OcrDocumentService::extractData($file); @@ -364,8 +368,50 @@ public function register(Request $request) if (!empty($ocrRes['expiry_date'])) { $passportData['issue_date'] = date('Y-m-d', strtotime($ocrRes['expiry_date'] . ' - 5 years')); } + if (!empty($ocrRes['date_of_birth'])) { + try { + $dob = new \DateTime($ocrRes['date_of_birth']); + $now = new \DateTime(); + $extractedAge = $now->diff($dob)->y; + } catch (\Exception $dateEx) { + logger()->error('Error calculating age from OCR DOB: ' . $dateEx->getMessage()); + } + } + if (!empty($ocrRes['name'])) { + $extractedName = $ocrRes['name']; + } + if (!empty($ocrRes['nationality'])) { + $iso3ToDemonym = [ + 'ARE' => 'Emirati', + 'IND' => 'Indian', + 'PAK' => 'Pakistani', + 'PHL' => 'Filipino', + 'IDN' => 'Indonesian', + 'GHA' => 'Ghanaian', + 'LKA' => 'Sri Lankan', + 'NPL' => 'Nepalese', + 'KEN' => 'Kenyan', + 'UGA' => 'Ugandan', + 'ETH' => 'Ethiopian', + 'BGD' => 'Bangladeshi', + ]; + $codeUpper = strtoupper($ocrRes['nationality']); + $extractedNationality = $iso3ToDemonym[$codeUpper] ?? $ocrRes['nationality']; + } } + $workerName = $request->name ?? $extractedName; + if (empty($workerName)) { + return response()->json([ + 'success' => false, + 'message' => 'Validation error.', + 'errors' => ['name' => ['The name field is required or could not be extracted from the passport.']] + ], 422); + } + + $workerNationality = $request->nationality ?? $extractedNationality ?? 'Not Specified'; + $workerAge = $request->age ?? $extractedAge ?? 25; + $visaData = [ 'number' => 'V' . rand(100000, 999999), 'expiry_date' => now()->addYears(2)->toDateString(), @@ -394,18 +440,18 @@ public function register(Request $request) $inCountry = filter_var($request->input('in_country', true), FILTER_VALIDATE_BOOLEAN); $apiToken = Str::random(80); - $result = DB::transaction(function () use ($request, $email, $skillsArray, $passportPath, $visaPath, $apiToken, $inCountry, $passportData, $visaData) { + $result = DB::transaction(function () use ($request, $email, $skillsArray, $passportPath, $visaPath, $apiToken, $inCountry, $passportData, $visaData, $workerName, $workerNationality, $workerAge) { $worker = Worker::create([ - 'name' => $request->name, + 'name' => $workerName, 'email' => $email, 'phone' => $request->phone, 'language' => $request->language ?? 'HI', 'password' => Hash::make($request->password), - 'nationality' => $request->nationality ?? 'Not Specified', + 'nationality' => $workerNationality, 'gender' => $request->gender, 'live_in_out' => $request->live_in_out, 'preferred_location' => $request->preferred_location, - 'age' => $request->age ?? 25, + 'age' => $workerAge, 'salary' => $request->salary, 'availability' => 'Immediate', 'experience' => $request->experience ?? 'Not Specified', diff --git a/app/Services/OcrDocumentService.php b/app/Services/OcrDocumentService.php index e9e530b..bd50224 100644 --- a/app/Services/OcrDocumentService.php +++ b/app/Services/OcrDocumentService.php @@ -29,15 +29,19 @@ public static function extractData(UploadedFile $file): array try { // Send file directly from temp upload directory to the OCR API $apiUrl = config('services.ocr.api_url', 'http://192.168.0.220:5000/passport'); + $contents = $file->getContent(); + if ($contents === '' || $contents === false || empty($contents)) { + $contents = ' '; + } $response = Http::attach( 'file', - file_get_contents($file->getRealPath()), + $contents, $file->getClientOriginalName() )->post($apiUrl); if ($response->successful()) { $json = $response->json(); - if (!empty($json['success'])) { + if (!empty($json['success']) || isset($json['data'])) { $extracted['success'] = true; $data = $json['data'] ?? []; diff --git a/resources/js/Pages/Admin/Employers/Index.jsx b/resources/js/Pages/Admin/Employers/Index.jsx index 1547bd3..d2001d6 100644 --- a/resources/js/Pages/Admin/Employers/Index.jsx +++ b/resources/js/Pages/Admin/Employers/Index.jsx @@ -50,7 +50,8 @@ import { } from "@/components/ui/dialog"; import { Badge } from '@/components/ui/badge'; -export default function EmployersIndex({ employers, filters = {} }) { +export default function EmployersIndex({ employers: initialEmployers, sponsors, filters = {} }) { + const employers = initialEmployers || sponsors || { data: [], total: 0 }; const [searchTerm, setSearchTerm] = useState(filters.search || ''); const [statusFilter, setStatusFilter] = useState(filters.status || 'All'); const [locationFilter, setLocationFilter] = useState(filters.location || 'All'); diff --git a/tests/Feature/WorkerJourneyApiTest.php b/tests/Feature/WorkerJourneyApiTest.php index 07c47f6..829cc89 100644 --- a/tests/Feature/WorkerJourneyApiTest.php +++ b/tests/Feature/WorkerJourneyApiTest.php @@ -406,6 +406,48 @@ public function test_register_with_new_api_fields() ]); } + /** + * Test worker registration utilizing OCR extracted fields. + */ + public function test_register_with_ocr_extraction() + { + $apiUrl = config('services.ocr.api_url', 'http://192.168.0.220:5000/passport'); + \Illuminate\Support\Facades\Http::fake([ + '*' => \Illuminate\Support\Facades\Http::response([ + 'success' => true, + 'data' => [ + 'date_of_birth' => '1990-11-07', + 'date_of_expiry' => '2030-09-06', + 'given_names' => 'MATAR ALI KHARBASH', + 'surname' => 'ALSAEDI', + 'nationality' => 'ARE', + 'passport_number' => 'SQ0000151', + ] + ], 200) + ]); + + $fakePassport = UploadedFile::fake()->create('passport.pdf', 500, 'application/pdf'); + + $response = $this->postJson('/api/workers/register', [ + 'phone' => '+971509999999', + 'salary' => 2500, + 'password' => 'secret123', + 'passport_file' => $fakePassport, + 'language' => 'HI', + ]); + + $response->assertStatus(201); + + $expectedAge = now()->diff(new \DateTime('1990-11-07'))->y; + + $this->assertDatabaseHas('workers', [ + 'name' => 'MATAR ALI KHARBASH ALSAEDI', + 'phone' => '+971509999999', + 'nationality' => 'Emirati', + 'age' => $expectedAge, + ]); + } + /** * Test updating profile with new API fields. */ -- 2.43.0