Skip to content

Commit

Permalink
Prevent category deactivation if used by at least one participant (#49)
Browse files Browse the repository at this point in the history
* Prevent category deactivation if used by at least one participant

* Improve participant registration test
  • Loading branch information
avvertix authored Feb 10, 2024
1 parent b6222e6 commit 742cd21
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
11 changes: 11 additions & 0 deletions app/Http/Controllers/ChampionshipCategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use App\Models\Category;
use App\Models\Championship;
use App\Models\ChampionshipTire;
use App\Models\Participant;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;

class ChampionshipCategoryController extends Controller
{
Expand Down Expand Up @@ -133,6 +135,15 @@ public function update(Request $request, Category $category)
})]
]);


if(!($request->boolean('enabled') ?? false)){
$hasParticipants = Participant::where('championship_id', $championship->getKey())->where('category_id', $category->getKey())->exists();

if($hasParticipants){
throw ValidationException::withMessages(['enabled' => __('The category cannot be deactivated because one or more competitors are registered in it.')]);
}
}

$category->update([
'name' => $validated['name'],
'short_name' => $validated['short_name'] ?? null,
Expand Down
3 changes: 2 additions & 1 deletion lang/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,6 @@
"Registration": "Iscrizione",
"Tune the registration opening period.": "Aggiusta il periodo di apertura delle iscrizioni",
"Open the registration at": "Apri le iscrizioni alle (giorno e ora)",
"Close the registration at": "Chiudi le iscrizioni alle (giorno e ora)"
"Close the registration at": "Chiudi le iscrizioni alle (giorno e ora)",
"The category cannot be deactivated because one or more competitors are registered in it.": "La categoria non può essere disattivata poiché utilizzata da uno o più participanti."
}
33 changes: 33 additions & 0 deletions tests/Feature/ChampionshipCategoryControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Category;
use App\Models\Championship;
use App\Models\ChampionshipTire;
use App\Models\Participant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
Expand Down Expand Up @@ -406,6 +407,38 @@ public function test_category_can_be_disabled_by_omitting_enabled(): void
$this->assertFalse($updatedCategory->enabled);
}

public function test_category_cannot_be_disabled_when_assigned_to_a_participant(): void
{
$user = User::factory()->organizer()->create();

$category = Category::factory()
->create([
'enabled' => true,
]);

$existingParticipant = Participant::factory()
->recycle($category->championship)
->category($category)
->create();

$response = $this
->actingAs($user)
->from(route('categories.edit', $category))
->put(route('categories.update', $category), [
'name' => 'Category name',
]);

$response->assertRedirectToRoute('categories.edit', $category);

$response->assertSessionHasErrors(['enabled' => 'The category cannot be deactivated because one or more competitors are registered in it.']);

$updatedCategory = $category->fresh();

$this->assertInstanceOf(Category::class, $updatedCategory);

$this->assertTrue($updatedCategory->enabled);
}

public function test_category_details_shown(): void
{
$user = User::factory()->organizer()->create();
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/SelfRegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public function test_last_participant_can_self_register()

$this->travelBack();

$participant = Participant::latest()->first();
$participant = $race->participants()->where('bib', 100)->first();

$this->assertInstanceOf(Participant::class, $participant);
$this->assertEquals(100, $participant->bib);
Expand Down

0 comments on commit 742cd21

Please sign in to comment.