Compare commits

..

No commits in common. "cf710733689a44e3be56a0cb0e4bc9a11951012a" and "f733384fa22970451bb61341f4647b6246af057b" have entirely different histories.

5 changed files with 8 additions and 102 deletions

View File

@ -67,7 +67,6 @@ 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'])
]);
}

View File

@ -298,7 +298,7 @@ public function setupProfile(Request $request)
public function register(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'nullable|string|max:255',
'name' => 'required|string|max:255',
'phone' => 'required|string|max:50|unique:workers,phone',
'salary' => 'required|numeric|min:0',
'password' => 'required|string|min:6',
@ -352,10 +352,6 @@ 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);
@ -368,50 +364,8 @@ 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(),
@ -440,18 +394,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, $workerName, $workerNationality, $workerAge) {
$result = DB::transaction(function () use ($request, $email, $skillsArray, $passportPath, $visaPath, $apiToken, $inCountry, $passportData, $visaData) {
$worker = Worker::create([
'name' => $workerName,
'name' => $request->name,
'email' => $email,
'phone' => $request->phone,
'language' => $request->language ?? 'HI',
'password' => Hash::make($request->password),
'nationality' => $workerNationality,
'nationality' => $request->nationality ?? 'Not Specified',
'gender' => $request->gender,
'live_in_out' => $request->live_in_out,
'preferred_location' => $request->preferred_location,
'age' => $workerAge,
'age' => $request->age ?? 25,
'salary' => $request->salary,
'availability' => 'Immediate',
'experience' => $request->experience ?? 'Not Specified',

View File

@ -29,19 +29,15 @@ 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',
$contents,
file_get_contents($file->getRealPath()),
$file->getClientOriginalName()
)->post($apiUrl);
if ($response->successful()) {
$json = $response->json();
if (!empty($json['success']) || isset($json['data'])) {
if (!empty($json['success'])) {
$extracted['success'] = true;
$data = $json['data'] ?? [];

View File

@ -50,8 +50,7 @@ import {
} from "@/components/ui/dialog";
import { Badge } from '@/components/ui/badge';
export default function EmployersIndex({ employers: initialEmployers, sponsors, filters = {} }) {
const employers = initialEmployers || sponsors || { data: [], total: 0 };
export default function EmployersIndex({ employers, filters = {} }) {
const [searchTerm, setSearchTerm] = useState(filters.search || '');
const [statusFilter, setStatusFilter] = useState(filters.status || 'All');
const [locationFilter, setLocationFilter] = useState(filters.location || 'All');

View File

@ -406,48 +406,6 @@ 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.
*/