From 3fe86d5352e46b3388916c54aa610d95c8ce2cf8 Mon Sep 17 00:00:00 2001 From: Fenrikur <3359222+Fenrikur@users.noreply.github.com> Date: Thu, 1 Feb 2024 01:12:20 +0100 Subject: [PATCH] fix(db): parent self-reference on applications --- .../Resources/ApplicationResource.php | 8 ++--- .../Applications/ApplicationController.php | 8 ++--- .../Applications/InviteesController.php | 2 +- app/Models/Application.php | 10 +++--- ...000_create_password_reset_tokens_table.php | 28 ---------------- ...01_create_personal_access_tokens_table.php | 33 ------------------- ...02_28_212504_create_applications_table.php | 2 +- resources/views/forms/application.blade.php | 2 +- tests/Feature/ApplicationTest.php | 6 ++-- 9 files changed, 19 insertions(+), 80 deletions(-) delete mode 100644 database/migrations/2014_10_12_100000_create_password_reset_tokens_table.php delete mode 100644 database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php diff --git a/app/Filament/Resources/ApplicationResource.php b/app/Filament/Resources/ApplicationResource.php index f61804f..5bb0974 100755 --- a/app/Filament/Resources/ApplicationResource.php +++ b/app/Filament/Resources/ApplicationResource.php @@ -82,7 +82,7 @@ public static function form(Form $form): Form ->required(), Forms\Components\Select::make('user_id')->searchable()->relationship('user', 'name') ->required(), - Forms\Components\Select::make('parent')->searchable()->options(Application::getEligibleParents()->pluck('name', 'id')) + Forms\Components\Select::make('parent_id')->searchable()->options(Application::getEligibleParents()->pluck('name', 'id')) ->hidden(fn (\Filament\Forms\Get $get) => $get('type') === ApplicationType::Dealer->value) ->required(fn (\Filament\Forms\Get $get) => $get('type') !== ApplicationType::Dealer->value), Forms\Components\Select::make('table_type_requested')->relationship('requestedTable', 'name') @@ -151,7 +151,7 @@ public static function table(Table $table): Table ->boolean(), Tables\Columns\TextColumn::make('dlrshp') ->getStateUsing(function (Application $record) { - return $record->parent ?: $record->id; + return $record->parent_id ?: $record->id; }) ->sortable(query: function (Builder $query, string $direction): Builder { return $query @@ -160,7 +160,7 @@ public static function table(Table $table): Table ->searchable(query: function (Builder $query, string $search): Builder { return $query ->where('id', '=', $search) - ->orWhere('parent', '=', $search); + ->orWhere('parent_id', '=', $search); }), Tables\Columns\TextColumn::make('display_name') ->searchable(), @@ -193,7 +193,7 @@ public static function table(Table $table): Table ->dateTime(), ]) ->filters([ - Tables\Filters\Filter::make('parent') + Tables\Filters\Filter::make('dealers') ->query(fn (Builder $query): Builder => $query->where('type', 'dealer')) ->label('Only Dealerships'), Tables\Filters\Filter::make('assignedTable') diff --git a/app/Http/Controllers/Applications/ApplicationController.php b/app/Http/Controllers/Applications/ApplicationController.php index 30abc19..8e87048 100644 --- a/app/Http/Controllers/Applications/ApplicationController.php +++ b/app/Http/Controllers/Applications/ApplicationController.php @@ -126,7 +126,7 @@ public function store(ApplicationRequest $request) "is_wallseat" => $request->input('wallseat') === "on", "wanted_neighbors" => $request->input('wanted'), "comment" => $request->input('comment'), - "parent" => $parent?->id, + "parent_id" => $parent?->id, "invite_code_shares" => null, "invite_code_assistants" => null, "waiting_at" => null, @@ -192,7 +192,7 @@ public function update(ApplicationRequest $request) "is_wallseat" => $request->input('wallseat') === "on", "wanted_neighbors" => $request->input('wanted'), "comment" => $request->input('comment'), - "parent" => $newParent?->id ?? $application->parent, + "parent_id" => $newParent?->id ?? $application->parent, ]); if ($application->isActive() && $newApplicationType !== ApplicationType::Assistant) { @@ -236,7 +236,7 @@ public function destroy() foreach ($application->children()->get() as $child) { $child->update([ 'canceled_at' => now(), - 'parent' => null, + 'parent_id' => null, 'type' => 'dealer' ]); $child->user()->first()->notify(new CanceledByDealershipNotification()); @@ -244,7 +244,7 @@ public function destroy() $application->update([ 'canceled_at' => now(), - 'parent' => null, + 'parent_id' => null, 'type' => 'dealer' ]); $user->notify(new CanceledBySelfNotification()); diff --git a/app/Http/Controllers/Applications/InviteesController.php b/app/Http/Controllers/Applications/InviteesController.php index a05c00d..713f556 100644 --- a/app/Http/Controllers/Applications/InviteesController.php +++ b/app/Http/Controllers/Applications/InviteesController.php @@ -52,7 +52,7 @@ public function destroy(InviteeRemovalRequest $request) $invitee->update([ "type" => ApplicationType::Dealer, "canceled_at" => now(), - "parent" => null + "parent_id" => null ]); $invitee->user()->first()->notify(new CanceledByDealershipNotification()); return back(); diff --git a/app/Models/Application.php b/app/Models/Application.php index 9bbc46b..436bdf1 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -117,12 +117,12 @@ public function assignedTable() public function parent() { - return $this->belongsTo(__CLASS__, 'parent'); + return $this->belongsTo(self::class, 'parent_id'); } public function children() { - return $this->hasMany(__CLASS__, 'parent'); + return $this->hasMany(self::class, 'parent_id'); } public function profile() @@ -144,7 +144,7 @@ public function type(): Attribute case ApplicationType::Dealer: // Dealer has no parent return [ - 'parent' => null, + 'parent_id' => null, 'type' => $type, ]; case ApplicationType::Assistant: @@ -261,7 +261,7 @@ public function setStatusAttribute(ApplicationStatus|string $status) 'offer_accepted_at' => null, 'offer_sent_at' => null, 'table_number' => null, - 'parent' => null, + 'parent_id' => null, 'waiting_at' => null, 'type' => ApplicationType::Dealer, 'canceled_at' => now(), @@ -374,7 +374,7 @@ public static function getAllApplicationsForExport() 'users.email AS email', 'users.reg_id AS reg_id', 'type AS app_type', - 'parent', + 'parent_id', 'display_name', 'applications.website AS app_website', 'table_number', diff --git a/database/migrations/2014_10_12_100000_create_password_reset_tokens_table.php b/database/migrations/2014_10_12_100000_create_password_reset_tokens_table.php deleted file mode 100644 index 81a7229..0000000 --- a/database/migrations/2014_10_12_100000_create_password_reset_tokens_table.php +++ /dev/null @@ -1,28 +0,0 @@ -string('email')->primary(); - $table->string('token'); - $table->timestamp('created_at')->nullable(); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('password_reset_tokens'); - } -}; diff --git a/database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php b/database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php deleted file mode 100644 index e828ad8..0000000 --- a/database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php +++ /dev/null @@ -1,33 +0,0 @@ -id(); - $table->morphs('tokenable'); - $table->string('name'); - $table->string('token', 64)->unique(); - $table->text('abilities')->nullable(); - $table->timestamp('last_used_at')->nullable(); - $table->timestamp('expires_at')->nullable(); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('personal_access_tokens'); - } -}; diff --git a/database/migrations/2023_02_28_212504_create_applications_table.php b/database/migrations/2023_02_28_212504_create_applications_table.php index c9edf5a..5bea3a4 100644 --- a/database/migrations/2023_02_28_212504_create_applications_table.php +++ b/database/migrations/2023_02_28_212504_create_applications_table.php @@ -14,7 +14,7 @@ public function up() $table->foreignIdFor(\App\Models\TableType::class,'table_type_assigned')->nullable()->constrained('table_types')->cascadeOnUpdate()->cascadeOnDelete(); $table->string('type'); // Dealer, Share, Assistant - $table->foreignIdFor(\App\Models\Application::class,'parent')->nullable()->constrained('applications')->cascadeOnUpdate()->cascadeOnDelete(); + $table->foreignIdFor(\App\Models\Application::class,'parent_id')->nullable()->constrained('applications')->cascadeOnUpdate()->cascadeOnDelete(); $table->string('display_name')->nullable(); $table->string('website')->nullable(); diff --git a/resources/views/forms/application.blade.php b/resources/views/forms/application.blade.php index 90037e0..2658aee 100644 --- a/resources/views/forms/application.blade.php +++ b/resources/views/forms/application.blade.php @@ -33,7 +33,7 @@ Updates {{ \Illuminate\Support\Str::ucfirst($application->type->value) }} - @if ($application->parent) + @if ($application?->parent) of {{ $application->parent()->first()->getFullName() }} @endif to diff --git a/tests/Feature/ApplicationTest.php b/tests/Feature/ApplicationTest.php index 267ad98..97ed6be 100644 --- a/tests/Feature/ApplicationTest.php +++ b/tests/Feature/ApplicationTest.php @@ -62,7 +62,7 @@ public function test_application_creation_and_edit_normal() 'table_type_requested' => $table->id, 'table_type_assigned' => null, 'type' => ApplicationType::Dealer->value, - 'parent' => null, + 'parent_id' => null, 'display_name' => "Tin", 'website' => "https://eurofurence.org", 'table_number' => null, @@ -122,7 +122,7 @@ public function test_updating_existing_share_does_not_require_code() $childApp = Application::factory()->create([ 'user_id' => $child->id, 'type' => ApplicationType::Share->value, - 'parent' => $parentApp->id, + 'parent_id' => $parentApp->id, "canceled_at" => null, ] ); @@ -171,7 +171,7 @@ public function test_application_creation_edit_normal() 'user_id' => $user->id, 'table_type_requested' => $table->id, 'type' => ApplicationType::Dealer->value, - 'parent' => null, + 'parent_id' => null, 'display_name' => "TinUpdate", 'website' => "https://eurofurence-update.org", 'merchandise' => "I am selling dragons",