End year procedures implementation #111
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
use App\Actions\YearEndProcedures\RecordHistoricalSeats;
|
||||
use App\Exceptions\AuditionAdminException;
|
||||
use App\Models\Ensemble;
|
||||
use App\Models\Entry;
|
||||
use App\Models\HistoricalSeat;
|
||||
use App\Models\Seat;
|
||||
use App\Models\Student;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('only executes for an admin user', function () {
|
||||
$action = new RecordHistoricalSeats();
|
||||
expect(fn () => $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);
|
||||
});
|
||||
Loading…
Reference in New Issue