diff --git a/app/Actions/YearEndProcedures/RecordHistoricalSeats.php b/app/Actions/YearEndProcedures/RecordHistoricalSeats.php index fee9460..08701f1 100644 --- a/app/Actions/YearEndProcedures/RecordHistoricalSeats.php +++ b/app/Actions/YearEndProcedures/RecordHistoricalSeats.php @@ -2,6 +2,7 @@ namespace App\Actions\YearEndProcedures; +use App\Exceptions\AuditionAdminException; use App\Models\HistoricalSeat; use App\Models\Seat; use Carbon\Carbon; @@ -17,18 +18,28 @@ class RecordHistoricalSeats $this->saveSeats(); } - public function saveSeats(): void + /** + * @throws AuditionAdminException + */ + public function saveSeats() { - $seats = Seat::all(); - foreach ($seats as $seat) { - $student_id = $seat->student->id; - $year = Carbon::now()->year; - $seat_description = $seat->ensemble->name.' - '.$seat->audition->name.' - '.$seat->seat; - HistoricalSeat::create([ - 'student_id' => $student_id, - 'year' => $year, - 'seat_description' => $seat_description, - ]); + if (! auth()->user() or ! auth()->user()->is_admin) { + throw new AuditionAdminException('Only administrators may perform this action'); } + $seats = Seat::all(); + if ($seats->count() > 0) { + foreach ($seats as $seat) { + $student_id = $seat->student->id; + $year = Carbon::now()->year; + $seat_description = $seat->ensemble->name.' - '.$seat->audition->name.' - '.$seat->seat; + HistoricalSeat::create([ + 'student_id' => $student_id, + 'year' => $year, + 'seat_description' => $seat_description, + ]); + } + } + + return true; } } diff --git a/tests/Feature/Actions/RecordHistoricalSeatsTest.php b/tests/Feature/Actions/RecordHistoricalSeatsTest.php new file mode 100644 index 0000000..f1ab21c --- /dev/null +++ b/tests/Feature/Actions/RecordHistoricalSeatsTest.php @@ -0,0 +1,56 @@ + $action())->toThrow( + AuditionAdminException::class, + 'Only administrators may perform this action' + ); + + actAsNormal(); + expect(fn () => $action())->toThrow( + AuditionAdminException::class, + 'Only administrators may perform this action' + ); + + actAsAdmin(); + expect($action->saveSeats())->toBeTrue(); + +}); + +it('saves a seated student to the historical table', function () { + actAsAdmin(); + $entry = Entry::factory()->create(); + Entry::factory(5)->create(); + $action = new RecordHistoricalSeats(); + $ensemble = Ensemble::create([ + 'event_id' => $entry->audition->event_id, + 'name' => 'Test Ensemble', + 'code' => 'te', + 'rank' => 1, + ]); + $seat = Seat::create([ + 'ensemble_id' => $ensemble->id, + 'audition_id' => $entry->audition_id, + 'seat' => '1', + 'entry_id' => $entry->id, + ]); + $action->saveSeats(); + $historical_seats = HistoricalSeat::all(); + $test_seat = $historical_seats->first(); + expect($test_seat->student_id)->toBe($entry->student_id) + ->and($historical_seats)->toHaveCount(1) + ->and($test_seat->seat_description)->toBe($ensemble->name.' - '.$entry->audition->name.' - '.$seat->seat) + ->and(Student::count())->toBe(6); +});