From 6d066beea3ab7b7c2d314a46e0d7e151d07a8136 Mon Sep 17 00:00:00 2001 From: ncoronel Date: Fri, 3 Jul 2026 16:24:56 -0300 Subject: [PATCH] feat: add optional 'dni' and 'telefono' fields to User model and registration process --- app/Domains/Auth/Models/User.php | 2 +- .../Auth/Requests/RegisterUserRequest.php | 2 ++ app/Domains/Auth/Resources/UserResource.php | 2 ++ .../Auth/Services/RegisterUserService.php | 4 ++- ...39_add_dni_and_telefono_to_users_table.php | 29 ++++++++++++++++ tests/Feature/Auth/LoginControllerTest.php | 4 +-- tests/Feature/Auth/RegisterControllerTest.php | 33 ++++++++++++++++++- 7 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 database/migrations/2026_07_03_192239_add_dni_and_telefono_to_users_table.php diff --git a/app/Domains/Auth/Models/User.php b/app/Domains/Auth/Models/User.php index 719f5ed..fa2508d 100644 --- a/app/Domains/Auth/Models/User.php +++ b/app/Domains/Auth/Models/User.php @@ -10,7 +10,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; -#[Fillable(['nombre_apellido', 'email', 'password'])] +#[Fillable(['nombre_apellido', 'email', 'password', 'dni', 'telefono'])] #[Hidden(['password', 'remember_token'])] class User extends Authenticatable { diff --git a/app/Domains/Auth/Requests/RegisterUserRequest.php b/app/Domains/Auth/Requests/RegisterUserRequest.php index 0438c27..1c74a6a 100644 --- a/app/Domains/Auth/Requests/RegisterUserRequest.php +++ b/app/Domains/Auth/Requests/RegisterUserRequest.php @@ -21,6 +21,8 @@ class RegisterUserRequest extends FormRequest 'nombre_apellido' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users', 'email')], 'password' => ['required', 'string', 'confirmed'], + 'dni' => ['nullable', 'string', 'max:255'], + 'telefono' => ['nullable', 'string', 'max:255'], ]; } } diff --git a/app/Domains/Auth/Resources/UserResource.php b/app/Domains/Auth/Resources/UserResource.php index 0748df6..d666514 100644 --- a/app/Domains/Auth/Resources/UserResource.php +++ b/app/Domains/Auth/Resources/UserResource.php @@ -19,6 +19,8 @@ class UserResource extends JsonResource 'id' => $this->id, 'nombre_apellido' => $this->nombre_apellido, 'email' => $this->email, + 'dni' => $this->dni, + 'telefono' => $this->telefono, ]; } } diff --git a/app/Domains/Auth/Services/RegisterUserService.php b/app/Domains/Auth/Services/RegisterUserService.php index 61f9238..02bfe86 100644 --- a/app/Domains/Auth/Services/RegisterUserService.php +++ b/app/Domains/Auth/Services/RegisterUserService.php @@ -7,7 +7,7 @@ use App\Domains\Auth\Models\User; class RegisterUserService { /** - * @param array{nombre_apellido: string, email: string, password: string} $data + * @param array{nombre_apellido: string, email: string, password: string, dni?: string|null, telefono?: string|null} $data */ public function register(array $data): User { @@ -15,6 +15,8 @@ class RegisterUserService 'nombre_apellido' => $data['nombre_apellido'], 'email' => $data['email'], 'password' => $data['password'], + 'dni' => $data['dni'] ?? null, + 'telefono' => $data['telefono'] ?? null, ]); } } diff --git a/database/migrations/2026_07_03_192239_add_dni_and_telefono_to_users_table.php b/database/migrations/2026_07_03_192239_add_dni_and_telefono_to_users_table.php new file mode 100644 index 0000000..ca4f0e3 --- /dev/null +++ b/database/migrations/2026_07_03_192239_add_dni_and_telefono_to_users_table.php @@ -0,0 +1,29 @@ +string('dni')->nullable()->after('email'); + $table->string('telefono')->nullable()->after('dni'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn(['dni', 'telefono']); + }); + } +}; diff --git a/tests/Feature/Auth/LoginControllerTest.php b/tests/Feature/Auth/LoginControllerTest.php index b5198a5..dc4e3a4 100644 --- a/tests/Feature/Auth/LoginControllerTest.php +++ b/tests/Feature/Auth/LoginControllerTest.php @@ -36,7 +36,7 @@ class LoginControllerTest extends TestCase $this->assertNotEmpty($response->json('token')); $this->withHeader('Authorization', 'Bearer '.$response->json('token')) - ->getJson('/api/user') + ->getJson('/api/me') ->assertOk() ->assertJsonPath('id', $user->id) ->assertJsonPath('email', 'grace@example.com'); @@ -44,7 +44,7 @@ class LoginControllerTest extends TestCase public function test_it_rejects_access_to_the_current_user_endpoint_without_a_token(): void { - $this->getJson('/api/user')->assertUnauthorized(); + $this->getJson('/api/me')->assertUnauthorized(); } public function test_it_logs_out_the_current_token(): void diff --git a/tests/Feature/Auth/RegisterControllerTest.php b/tests/Feature/Auth/RegisterControllerTest.php index 926f491..e4351b7 100644 --- a/tests/Feature/Auth/RegisterControllerTest.php +++ b/tests/Feature/Auth/RegisterControllerTest.php @@ -17,17 +17,23 @@ class RegisterControllerTest extends TestCase 'email' => 'ada@example.com', 'password' => 'secret123', 'password_confirmation' => 'secret123', + 'dni' => '12345678A', + 'telefono' => '+541122334455', ]); $response ->assertCreated() ->assertJsonPath('message', 'Usuario registrado correctamente.') ->assertJsonPath('data.nombre_apellido', 'Ada Lovelace') - ->assertJsonPath('data.email', 'ada@example.com'); + ->assertJsonPath('data.email', 'ada@example.com') + ->assertJsonPath('data.dni', '12345678A') + ->assertJsonPath('data.telefono', '+541122334455'); $this->assertDatabaseHas('users', [ 'nombre_apellido' => 'Ada Lovelace', 'email' => 'ada@example.com', + 'dni' => '12345678A', + 'telefono' => '+541122334455', ]); $user = User::query()->where('email', 'ada@example.com')->firstOrFail(); @@ -35,6 +41,31 @@ class RegisterControllerTest extends TestCase $this->assertNotSame('secret123', $user->password); } + public function test_it_registers_a_user_without_optional_fields(): void + { + $response = $this->postJson('/api/register', [ + 'nombre_apellido' => 'Alan Turing', + 'email' => 'alan@example.com', + 'password' => 'secret123', + 'password_confirmation' => 'secret123', + ]); + + $response + ->assertCreated() + ->assertJsonPath('message', 'Usuario registrado correctamente.') + ->assertJsonPath('data.nombre_apellido', 'Alan Turing') + ->assertJsonPath('data.email', 'alan@example.com') + ->assertJsonPath('data.dni', null) + ->assertJsonPath('data.telefono', null); + + $this->assertDatabaseHas('users', [ + 'nombre_apellido' => 'Alan Turing', + 'email' => 'alan@example.com', + 'dni' => null, + 'telefono' => null, + ]); + } + public function test_it_validates_required_fields_and_unique_email(): void { User::factory()->create([