mohan #26
@ -58,11 +58,10 @@ public function skills()
|
||||
public function sendOtp(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'phone' => 'required_without:email|nullable|string|max:50|unique:workers,phone',
|
||||
'phone' => 'required_without:email|nullable|string|max:50',
|
||||
'email' => 'required_without:phone|nullable|email|max:255|unique:workers,email',
|
||||
], [
|
||||
'phone.required_without' => 'Please provide a mobile number or email address.',
|
||||
'phone.unique' => 'This mobile number is already registered.',
|
||||
'email.required_without' => 'Please provide a mobile number or email address.',
|
||||
'email.unique' => 'This email address is already registered.',
|
||||
]);
|
||||
@ -75,6 +74,17 @@ public function sendOtp(Request $request)
|
||||
], 422);
|
||||
}
|
||||
|
||||
if ($request->phone) {
|
||||
$exists = Worker::findByPhone($request->phone);
|
||||
if ($exists) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Validation error.',
|
||||
'errors' => ['phone' => ['This mobile number is already registered.']]
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
|
||||
// Determine identifier (phone takes priority)
|
||||
$identifier = $request->phone ?? $request->email;
|
||||
$isPhone = !empty($request->phone);
|
||||
@ -205,7 +215,7 @@ public function setupProfile(Request $request)
|
||||
}
|
||||
|
||||
// Check if already registered
|
||||
if ($request->phone && Worker::where('phone', $request->phone)->exists()) {
|
||||
if ($request->phone && Worker::findByPhone($request->phone)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'This mobile number is already registered.'
|
||||
@ -309,7 +319,7 @@ public function register(Request $request)
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required|string|max:255',
|
||||
'phone' => 'required|string|max:50|unique:workers,phone',
|
||||
'phone' => 'required|string|max:50',
|
||||
'salary' => 'required|numeric|min:0',
|
||||
'password' => 'required|string|min:6',
|
||||
'skills' => 'nullable',
|
||||
@ -376,7 +386,6 @@ function ($attribute, $value, $fail) {
|
||||
'visa.sponsor_name' => 'nullable|string|max:255',
|
||||
'visa.ocr_accuracy' => 'nullable|numeric',
|
||||
], [
|
||||
'phone.unique' => 'This mobile number is already registered.',
|
||||
'passport.passport_number.unique' => 'This passport number is already registered.',
|
||||
]);
|
||||
|
||||
@ -388,6 +397,17 @@ function ($attribute, $value, $fail) {
|
||||
], 422);
|
||||
}
|
||||
|
||||
if ($request->phone) {
|
||||
$exists = Worker::findByPhone($request->phone);
|
||||
if ($exists) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Validation error.',
|
||||
'errors' => ['phone' => ['This mobile number is already registered.']]
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$phoneClean = preg_replace('/[^0-9]/', '', $request->phone);
|
||||
$email = "worker.{$phoneClean}@migrant.ae";
|
||||
@ -589,7 +609,7 @@ function ($attribute, $value, $fail) {
|
||||
|
||||
try {
|
||||
$worker = $request->phone
|
||||
? Worker::where('phone', $request->phone)->first()
|
||||
? Worker::findByPhone($request->phone)
|
||||
: Worker::where('email', $request->email)->first();
|
||||
|
||||
if (!$worker || !Hash::check($request->password, $worker->password)) {
|
||||
@ -1360,7 +1380,7 @@ public function forgotPassword(Request $request)
|
||||
], 422);
|
||||
}
|
||||
|
||||
$worker = Worker::where('phone', $request->phone)->first();
|
||||
$worker = Worker::findByPhone($request->phone);
|
||||
|
||||
if (!$worker) {
|
||||
return response()->json([
|
||||
@ -1436,7 +1456,7 @@ public function resetPassword(Request $request)
|
||||
], 422);
|
||||
}
|
||||
|
||||
$worker = Worker::where('phone', $request->phone)->first();
|
||||
$worker = Worker::findByPhone($request->phone);
|
||||
|
||||
if (!$worker) {
|
||||
return response()->json([
|
||||
|
||||
@ -69,7 +69,7 @@ public function updateProfile(Request $request)
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'nullable|string|max:255',
|
||||
'phone' => 'nullable|string|max:50|unique:workers,phone,' . $worker->id,
|
||||
'phone' => 'nullable|string|max:50',
|
||||
'main_profession' => 'nullable|string|max:100',
|
||||
'age' => [
|
||||
'nullable',
|
||||
@ -109,7 +109,6 @@ function ($attribute, $value, $fail) {
|
||||
],
|
||||
'visa' => 'nullable|array',
|
||||
], [
|
||||
'phone.unique' => 'This mobile number is already registered.',
|
||||
'passport.passport_number.unique' => 'This passport number is already registered.',
|
||||
]);
|
||||
|
||||
@ -121,6 +120,17 @@ function ($attribute, $value, $fail) {
|
||||
], 422);
|
||||
}
|
||||
|
||||
if ($request->phone) {
|
||||
$exists = Worker::findByPhone($request->phone);
|
||||
if ($exists && $exists->id !== $worker->id) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Validation error.',
|
||||
'errors' => ['phone' => ['This mobile number is already registered.']]
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
|
||||
// Document Update Check: Verify passport number matches existing passport number if already verified/saved
|
||||
$existingPassport = $worker->documents()->where('type', 'passport')->first();
|
||||
if ($existingPassport && $request->has('passport')) {
|
||||
|
||||
@ -236,4 +236,36 @@ public function profileViews()
|
||||
{
|
||||
return $this->hasMany(ProfileView::class);
|
||||
}
|
||||
|
||||
public static function findByPhone($phone)
|
||||
{
|
||||
if (empty($phone)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$cleanInput = ltrim(preg_replace('/\D/', '', $phone), '0');
|
||||
if (empty($cleanInput)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// If input is short, fall back to direct query
|
||||
if (strlen($cleanInput) < 7) {
|
||||
return self::where('phone', $phone)->first();
|
||||
}
|
||||
|
||||
$suffix = substr($cleanInput, -7);
|
||||
$candidates = self::whereRaw("REPLACE(REPLACE(phone, ' ', ''), '+', '') LIKE ?", ['%' . $suffix])->get();
|
||||
|
||||
foreach ($candidates as $candidate) {
|
||||
$cleanCandidate = ltrim(preg_replace('/\D/', '', $candidate->phone), '0');
|
||||
if ($cleanInput === $cleanCandidate ||
|
||||
str_ends_with($cleanInput, $cleanCandidate) ||
|
||||
str_ends_with($cleanCandidate, $cleanInput)) {
|
||||
return $candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user