Pārlūkot izejas kodu

feat: :sparkles: feat (login) permitindo 1 cliente e 1 prestador por email

alterada validacao para permitir criar 1 prestador e 1 cliente com mesmo email, diferenciando por qual app esta usando

fase:staging | origin:escopo
Gustavo Zanatta 3 nedēļas atpakaļ
vecāks
revīzija
527a6268e3

+ 14 - 23
app/Services/AuthService.php

@@ -12,6 +12,7 @@ use App\Models\User;
 use Carbon\Carbon;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Hash;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Str;
 
@@ -23,17 +24,14 @@ class AuthService
 
     public function login(string $email, string $password): ?array
     {
-        $user = User::where('email', $email)->first();
-
-        if (! $user || ! in_array($user->type, [UserTypeEnum::ADMIN, UserTypeEnum::USER])) {
-            return null;
-        }
+        $user = User::where('email', $email)
+            ->whereIn('type', [UserTypeEnum::ADMIN->value, UserTypeEnum::USER->value])
+            ->first();
 
-        if (! Auth::attempt(['email' => $email, 'password' => $password])) {
+        if (! $user || ! $user->password || ! Hash::check($password, $user->password)) {
             return null;
         }
 
-        // $user = User::where('email', $email)->first();
         $deviceId = Str::uuid()->toString();
 
         $accessToken = $user->createAccessToken($deviceId);
@@ -140,17 +138,12 @@ class AuthService
                         $q->where('phone', $data['phone']);
                     });
             })
+                ->where('type', UserTypeEnum::CLIENT->value)
                 ->first();
 
             $isLogin = false;
 
             if ($user) {
-                if ($user->type->value !== UserTypeEnum::CLIENT->value) {
-                    DB::rollBack();
-
-                    return ['error' => 'wrong_user_type'];
-                }
-
                 $user->code = $code;
 
                 $user->validated_code = false;
@@ -218,15 +211,10 @@ class AuthService
                         $q->where('phone', $data['phone']);
                     });
             })
+                ->where('type', UserTypeEnum::PROVIDER->value)
                 ->first();
             $isLogin = false;
             if ($user) {
-                if ($user->type->value !== UserTypeEnum::PROVIDER->value) {
-                    DB::rollBack();
-
-                    return ['error' => 'wrong_user_type'];
-                }
-
                 // if ($user->registration_complete) {
                 $provider = Provider::where('user_id', $user->id)->first();
                 if ($provider && $provider->approval_status->value !== ApprovalStatusEnum::ACCEPTED->value) {
@@ -285,6 +273,7 @@ class AuthService
             $query->when($email, fn ($q) => $q->where('email', $email))
                 ->when($phone, fn ($q) => $q->where('phone', $phone));
         })
+            ->where('type', UserTypeEnum::CLIENT->value)
             ->where('code', $code)
             ->first();
 
@@ -303,7 +292,7 @@ class AuthService
                 return ['error' => 'registration_incomplete'];
             }
 
-            return $this->loginWithEmail($user->email, $code);
+            return $this->loginWithEmail($user->email, $code, UserTypeEnum::CLIENT->value);
         }
 
         return true;
@@ -319,6 +308,7 @@ class AuthService
             $query->when($email, fn ($q) => $q->where('email', $email))
                 ->when($phone, fn ($q) => $q->where('phone', $phone));
         })
+            ->where('type', UserTypeEnum::PROVIDER->value)
             ->where('code', $code)
             ->first();
 
@@ -342,7 +332,7 @@ class AuthService
                 return ['error' => 'provider_rejected'];
             }
 
-            return $this->loginWithEmail($user->email, $code);
+            return $this->loginWithEmail($user->email, $code, UserTypeEnum::PROVIDER->value);
         }
 
         return true;
@@ -370,7 +360,7 @@ class AuthService
         }
 
         if ($isLogin) {
-            $resultLogin = $this->loginWithEmail($user->email, $code);
+            $resultLogin = $this->loginWithEmail($user->email, $code, $user->type->value);
 
             return $resultLogin;
         }
@@ -378,10 +368,11 @@ class AuthService
         return true;
     }
 
-    public function loginWithEmail(string $email, string $code): ?array
+    public function loginWithEmail(string $email, string $code, ?string $type = null): ?array
     {
         $user = User::where('email', $email)
             ->where('code', $code)
+            ->when($type, fn ($q) => $q->where('type', $type))
             ->first();
 
         if (! $user) {

+ 2 - 0
app/Services/ClientService.php

@@ -2,6 +2,7 @@
 
 namespace App\Services;
 
+use App\Enums\UserTypeEnum;
 use App\Models\Address;
 use App\Models\City;
 use App\Models\Client;
@@ -151,6 +152,7 @@ class ClientService
             $result = $this->authService->loginWithEmail(
                 email: $user->email,
                 code: $user->code,
+                type: UserTypeEnum::CLIENT->value,
             );
 
             DB::commit();

+ 1 - 0
app/Services/ProviderService.php

@@ -254,6 +254,7 @@ class ProviderService
             $result = $this->authService->loginWithEmail(
                 email: $user->email,
                 code: $user->code,
+                type: UserTypeEnum::PROVIDER->value,
             );
 
             DB::commit();

+ 24 - 0
database/migrations/2026_07_02_000000_change_users_email_unique_to_email_type.php

@@ -0,0 +1,24 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    public function up(): void
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->dropUnique('users_email_unique');
+            $table->unique(['email', 'type'], 'users_email_type_unique');
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->dropUnique('users_email_type_unique');
+            $table->unique('email', 'users_email_unique');
+        });
+    }
+};