diff --git a/app/Enums/EntryFlags.php b/app/Enums/EntryFlags.php new file mode 100644 index 0000000..7cb14d4 --- /dev/null +++ b/app/Enums/EntryFlags.php @@ -0,0 +1,12 @@ +flag_name->value; } - // return true if any flag in $this->flags has a flag_name of declined without making another db query if flags are loaded return in_array($flag, $flags); } diff --git a/app/Models/Entry.php b/app/Models/Entry.php index 0a4c608..d49fa3d 100644 --- a/app/Models/Entry.php +++ b/app/Models/Entry.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Enums\EntryFlags; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -66,8 +67,12 @@ class Entry extends Model public function hasFlag($flag): bool { - // return true if any flag in $this->flags has a flag_name of declined without making another db query if flags are loaded - return $this->flags->contains('flag_name', $flag); + $flags = []; + foreach ($this->flags as $checkFlag) { + $flags[] = $checkFlag->flag_name->value; + } + + return in_array($flag, $flags); } @@ -76,7 +81,13 @@ class Entry extends Model if ($this->hasFlag($flag)) { return; } - $this->flags()->create(['flag_name' => $flag]); + $enum = match ($flag) { + 'will_advance' => EntryFlags::WILL_ADVANCE, + 'declined' => EntryFlags::DECLINED, + 'no_show' => EntryFlags::NO_SHOW, + 'failed_prelim' => EntryFlags::FAILED_PRELIM, + }; + $this->flags()->create(['flag_name' => $enum]); $this->load('flags'); } @@ -118,8 +129,8 @@ class Entry extends Model { $query->whereDoesntHave('flags', function (Builder $query) { $query->where('flag_name', 'declined') - ->orWhere('flag_name', 'no-show') - ->orWhere('flag_name', 'failed-prelim'); + ->orWhere('flag_name', 'no_show') + ->orWhere('flag_name', 'failed_prelim'); }); } } diff --git a/app/Models/EntryFlag.php b/app/Models/EntryFlag.php index 8bd484b..5b3cf64 100644 --- a/app/Models/EntryFlag.php +++ b/app/Models/EntryFlag.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Enums\EntryFlags; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -9,8 +10,10 @@ class EntryFlag extends Model { protected $guarded = []; - // Possible flags include - // - declined: used if a doubler declines a seat in this audition. Checked by DoublerService + protected $casts = [ + 'flag_name' => EntryFlags::class, + ]; + public function entry(): BelongsTo { return $this->belongsTo(Entry::class); diff --git a/tests/Feature/Models/EntryTest.php b/tests/Feature/Models/EntryTest.php index 233e6af..d2ccaba 100644 --- a/tests/Feature/Models/EntryTest.php +++ b/tests/Feature/Models/EntryTest.php @@ -1,5 +1,6 @@ create(); - EntryFlag::create(['entry_id' => $entry->id, 'flag_name' => 'declined']); - EntryFlag::create(['entry_id' => $entry->id, 'flag_name' => 'no-show']); + EntryFlag::create(['entry_id' => $entry->id, 'flag_name' => EntryFlags::DECLINED]); + EntryFlag::create(['entry_id' => $entry->id, 'flag_name' => EntryFlags::NO_SHOW]); // Act & Assert expect($entry->flags->count())->toBe(2) - ->and($entry->flags->first()->flag_name)->toBe('declined') + ->and($entry->flags->first()->flag_name->value)->toBe('declined') ->and($entry->hasFlag('declined'))->toBeTrue() ->and($entry->hasFlag('random'))->toBeFalse(); }); @@ -90,15 +91,15 @@ it('can set and remove a flag', function () { // Arrange $entry = Entry::factory()->create(); // Act - $entry->addFlag('Test Flag'); - $entry->addFlag('Second Flag'); + $entry->addFlag('declined'); + $entry->addFlag('will_advance'); // Assert - expect($entry->hasFlag('Test Flag'))->toBeTrue() - ->and($entry->hasFlag('Second Flag'))->toBeTrue() + expect($entry->hasFlag('declined'))->toBeTrue() + ->and($entry->hasFlag('will_advance'))->toBeTrue() ->and($entry->hasFlag('random'))->toBeFalse(); - $entry->removeFlag('Test Flag'); - expect($entry->hasFlag('Test Flag'))->toBeFalse() - ->and($entry->hasFlag('Second Flag'))->toBeTrue(); + $entry->removeFlag('declined'); + expect($entry->hasFlag('declined'))->toBeFalse() + ->and($entry->hasFlag('will_advance'))->toBeTrue(); }); it('always has a score sheet count available', function () { @@ -176,8 +177,8 @@ it('has a forAdvancement scope that only returns those entries entered for advan it('has an available scope that returns only auditions without a flag that would prevent seating', function () { Entry::factory()->count(10)->create(); Entry::factory()->create()->addFlag('declined'); - Entry::factory()->create()->addFlag('no-show'); - Entry::factory()->create()->addFlag('failed-prelim'); + Entry::factory()->create()->addFlag('no_show'); + Entry::factory()->create()->addFlag('failed_prelim'); // Act & Assert expect(Entry::all()->count())->toBe(13) ->and(Entry::available()->count())->toBe(10) diff --git a/tests/Feature/Pages/JudgingEntryScoreSheetTest.php b/tests/Feature/Pages/JudgingEntryScoreSheetTest.php index bc73c59..e8518b4 100644 --- a/tests/Feature/Pages/JudgingEntryScoreSheetTest.php +++ b/tests/Feature/Pages/JudgingEntryScoreSheetTest.php @@ -96,6 +96,7 @@ it('allows an assigned judge to enter scores', function () { $score[$subscore->id] = mt_rand(0, 100); } // Act & Assert + /** @noinspection PhpUnhandledExceptionInspection */ $this->post(route('judging.saveScoreSheet', $this->entries->first()), ['advancement-vote' => 'yes', 'score' => $score]) ->assertSessionHasNoErrors()