feat(users): add event_id to users table and update related resources
This commit is contained in:
@@ -18,6 +18,7 @@ class AdministratorResource extends JsonResource
|
|||||||
'dni' => $this->dni,
|
'dni' => $this->dni,
|
||||||
'email' => $this->email,
|
'email' => $this->email,
|
||||||
'rol_codigo' => $this->rol_codigo,
|
'rol_codigo' => $this->rol_codigo,
|
||||||
|
'event_id' => $this->event_id,
|
||||||
'role' => $this->whenLoaded('role', fn () => [
|
'role' => $this->whenLoaded('role', fn () => [
|
||||||
'codigo' => $this->role?->codigo,
|
'codigo' => $this->role?->codigo,
|
||||||
'nombre' => $this->role?->nombre,
|
'nombre' => $this->role?->nombre,
|
||||||
|
|||||||
@@ -2,11 +2,12 @@
|
|||||||
|
|
||||||
namespace App\Domains\Core\Auth\Models;
|
namespace App\Domains\Core\Auth\Models;
|
||||||
|
|
||||||
|
use App\Domains\Commerce\Catalog\Models\Category;
|
||||||
use App\Domains\Core\Authorization\Enums\RoleCode;
|
use App\Domains\Core\Authorization\Enums\RoleCode;
|
||||||
use App\Domains\Core\Authorization\Models\Role;
|
use App\Domains\Core\Authorization\Models\Role;
|
||||||
use App\Domains\Commerce\Catalog\Models\Category;
|
|
||||||
use App\Domains\Ticketing\Event\Models\EventDateChangeView;
|
|
||||||
use App\Domains\Core\Tenant\Models\Tenant;
|
use App\Domains\Core\Tenant\Models\Tenant;
|
||||||
|
use App\Domains\Ticketing\Event\Models\Event;
|
||||||
|
use App\Domains\Ticketing\Event\Models\EventDateChangeView;
|
||||||
use App\Domains\Ticketing\Ticket\Models\ScanAttempt;
|
use App\Domains\Ticketing\Ticket\Models\ScanAttempt;
|
||||||
use Database\Factories\UserFactory;
|
use Database\Factories\UserFactory;
|
||||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
@@ -20,7 +21,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
|
|||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
use Laravel\Sanctum\HasApiTokens;
|
use Laravel\Sanctum\HasApiTokens;
|
||||||
|
|
||||||
#[Fillable(['nombre_apellido', 'email', 'password', 'dni', 'telefono', 'google_id', 'rol_codigo', 'tenant_codigo'])]
|
#[Fillable(['nombre_apellido', 'email', 'password', 'dni', 'telefono', 'google_id', 'rol_codigo', 'tenant_codigo', 'event_id'])]
|
||||||
#[Hidden(['password', 'remember_token', 'active_email', 'active_google_id'])]
|
#[Hidden(['password', 'remember_token', 'active_email', 'active_google_id'])]
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable
|
||||||
{
|
{
|
||||||
@@ -86,6 +87,12 @@ class User extends Authenticatable
|
|||||||
return $this->belongsTo(Tenant::class, 'tenant_codigo', 'codigo');
|
return $this->belongsTo(Tenant::class, 'tenant_codigo', 'codigo');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return BelongsTo<Event, $this> */
|
||||||
|
public function event(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Event::class);
|
||||||
|
}
|
||||||
|
|
||||||
/** @return BelongsToMany<Category, $this> */
|
/** @return BelongsToMany<Category, $this> */
|
||||||
public function scanCategories(): BelongsToMany
|
public function scanCategories(): BelongsToMany
|
||||||
{
|
{
|
||||||
@@ -103,6 +110,7 @@ class User extends Authenticatable
|
|||||||
protected function casts(): array
|
protected function casts(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
'event_id' => 'integer',
|
||||||
'email_verified_at' => 'datetime',
|
'email_verified_at' => 'datetime',
|
||||||
'password' => 'hashed',
|
'password' => 'hashed',
|
||||||
'failed_login_attempts' => 'integer',
|
'failed_login_attempts' => 'integer',
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ class UserResource extends JsonResource
|
|||||||
'telefono' => $this->telefono,
|
'telefono' => $this->telefono,
|
||||||
'rol_codigo' => $this->rol_codigo,
|
'rol_codigo' => $this->rol_codigo,
|
||||||
'tenant_codigo' => $this->tenant_codigo,
|
'tenant_codigo' => $this->tenant_codigo,
|
||||||
|
'event_id' => $this->event_id,
|
||||||
'categories' => $this->whenLoaded('scanCategories', fn () => $this->scanCategories
|
'categories' => $this->whenLoaded('scanCategories', fn () => $this->scanCategories
|
||||||
->map(fn ($category) => [
|
->map(fn ($category) => [
|
||||||
'id' => $category->id,
|
'id' => $category->id,
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?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): void {
|
||||||
|
$table->foreignId('event_id')
|
||||||
|
->nullable()
|
||||||
|
->after('tenant_codigo')
|
||||||
|
->constrained('events')
|
||||||
|
->cascadeOnUpdate()
|
||||||
|
->restrictOnDelete();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table): void {
|
||||||
|
$table->dropForeign(['event_id']);
|
||||||
|
$table->dropColumn('event_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -17,6 +17,7 @@ class UserAuthorizationRelationsTest extends TestCase
|
|||||||
$this->assertTrue(Schema::hasColumns('users', [
|
$this->assertTrue(Schema::hasColumns('users', [
|
||||||
'rol_codigo',
|
'rol_codigo',
|
||||||
'tenant_codigo',
|
'tenant_codigo',
|
||||||
|
'event_id',
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,5 +29,7 @@ class UserAuthorizationRelationsTest extends TestCase
|
|||||||
$this->assertNull($user->tenant_codigo);
|
$this->assertNull($user->tenant_codigo);
|
||||||
$this->assertSame(RoleCode::User->value, $user->role->codigo);
|
$this->assertSame(RoleCode::User->value, $user->role->codigo);
|
||||||
$this->assertNull($user->tenant);
|
$this->assertNull($user->tenant);
|
||||||
|
$this->assertNull($user->event_id);
|
||||||
|
$this->assertNull($user->event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
57
tests/Feature/Migrations/AddEventIdToUsersTest.php
Normal file
57
tests/Feature/Migrations/AddEventIdToUsersTest.php
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Migrations;
|
||||||
|
|
||||||
|
use App\Domains\Core\Administrator\Resources\AdministratorResource;
|
||||||
|
use App\Domains\Core\Auth\Models\User;
|
||||||
|
use App\Domains\Core\Auth\Resources\UserResource;
|
||||||
|
use Illuminate\Database\QueryException;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class AddEventIdToUsersTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_event_assignment_preserves_existing_users_and_restricts_event_deletion(): void
|
||||||
|
{
|
||||||
|
Schema::create('events', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
});
|
||||||
|
Schema::create('users', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->string('tenant_codigo')->nullable();
|
||||||
|
$table->softDeletes();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
DB::table('users')->insert(['id' => 1, 'tenant_codigo' => 'legacy']);
|
||||||
|
DB::table('events')->insert(['id' => 42]);
|
||||||
|
|
||||||
|
$migration = require database_path('migrations/2026_10_01_000000_add_event_id_to_users_table.php');
|
||||||
|
$migration->up();
|
||||||
|
|
||||||
|
$legacy = User::query()->findOrFail(1);
|
||||||
|
$this->assertNull($legacy->event_id);
|
||||||
|
$this->assertNull($legacy->event);
|
||||||
|
$this->assertSame('legacy', $legacy->tenant_codigo);
|
||||||
|
|
||||||
|
$legacy->update(['event_id' => '42']);
|
||||||
|
$user = $legacy->fresh();
|
||||||
|
$this->assertSame(42, $user->event_id);
|
||||||
|
$this->assertSame(42, $user->event->id);
|
||||||
|
$this->assertSame(42, UserResource::make($user)->resolve(new Request)['event_id']);
|
||||||
|
$this->assertSame(42, AdministratorResource::make($user)->resolve(new Request)['event_id']);
|
||||||
|
|
||||||
|
try {
|
||||||
|
DB::table('events')->where('id', 42)->delete();
|
||||||
|
$this->fail('An assigned event must not be deleted.');
|
||||||
|
} catch (QueryException $exception) {
|
||||||
|
$this->assertStringContainsString('FOREIGN KEY', $exception->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
$migration->down();
|
||||||
|
$this->assertFalse(Schema::hasColumn('users', 'event_id'));
|
||||||
|
$this->assertSame('legacy', DB::table('users')->where('id', 1)->value('tenant_codigo'));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user