Skip to content

Commit

Permalink
fix(db): parent self-reference on applications
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenrikur committed Feb 1, 2024
1 parent ffc82c2 commit 3fe86d5
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 80 deletions.
8 changes: 4 additions & 4 deletions app/Filament/Resources/ApplicationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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')
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/Applications/ApplicationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -236,15 +236,15 @@ 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());
}

$application->update([
'canceled_at' => now(),
'parent' => null,
'parent_id' => null,
'type' => 'dealer'
]);
$user->notify(new CanceledBySelfNotification());
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Applications/InviteesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions app/Models/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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',
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion resources/views/forms/application.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
Updates
<span
class="badge bg-primary">{{ \Illuminate\Support\Str::ucfirst($application->type->value) }}</span>
@if ($application->parent)
@if ($application?->parent)
of <span class="badge bg-secondary">{{ $application->parent()->first()->getFullName() }}</span>
@endif
to
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
]
);
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 3fe86d5

Please sign in to comment.