Create AssignUserToSchool action and test.

This commit is contained in:
Matt Young 2025-07-01 18:40:44 -05:00
parent 691d1d5f7c
commit 5ebef46be7
3 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,46 @@
<?php
namespace App\Actions\Schools;
use App\Exceptions\AuditionAdminException;
use App\Models\School;
use App\Models\User;
class AssignUserToSchool
{
public function __invoke(User $user, School $school): void
{
$this->assign($user, $school);
}
public function assign(User $user, School $school, bool $addDomainToSchool = true): void
{
if (! User::where('id', $user->id)->exists()) {
throw new AuditionAdminException('User does not exist');
}
if (! School::where('id', $school->id)->exists()) {
throw new AuditionAdminException('School does not exist');
}
// Save the old school for logging
$oldSchool = $user->school;
$affected = [];
// Set the new school
$user->school_id = $school->id;
$user->save();
if ($oldSchool) {
$message = 'Moved '.$user->full_name().' from school '.$oldSchool->name.' to school '.$school->name;
$affected = ['users' => [$user->id], 'schools' => [$oldSchool->id, $school->id]];
} else {
$message = 'Assigned '.$user->full_name().' to school '.$school->name;
$affected = ['users' => [$user->id], 'schools' => [$school->id]];
}
auditionLog($message, $affected);
$domainRecorder = app(AddSchoolEmailDomain::class);
if ($addDomainToSchool) {
$domainRecorder($school, $user->emailDomain());
}
}
}

View File

@ -24,6 +24,7 @@ use App\Models\EntryFlag;
use App\Models\Room;
use App\Models\RoomUser;
use App\Models\School;
use App\Models\SchoolEmailDomain;
use App\Models\ScoreSheet;
use App\Models\ScoringGuide;
use App\Models\SeatingLimit;
@ -36,6 +37,7 @@ use App\Observers\EntryFlagObserver;
use App\Observers\EntryObserver;
use App\Observers\RoomObserver;
use App\Observers\RoomUserObserver;
use App\Observers\SchoolEmailDomainObserver;
use App\Observers\SchoolObserver;
use App\Observers\ScoreSheetObserver;
use App\Observers\ScoringGuideObserver;
@ -91,6 +93,7 @@ class AppServiceProvider extends ServiceProvider
Room::observe(RoomObserver::class);
RoomUser::observe(RoomUserObserver::class);
School::observe(SchoolObserver::class);
SchoolEmailDomain::observe(SchoolEmailDomainObserver::class);
ScoreSheet::observe(ScoreSheetObserver::class);
ScoringGuide::observe(ScoringGuideObserver::class);
Student::observe(StudentObserver::class);

View File

@ -0,0 +1,58 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
use App\Actions\Schools\AssignUserToSchool;
use App\Exceptions\AuditionAdminException;
use App\Models\AuditLogEntry;
use App\Models\School;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->assigner = app(AssignUserToSchool::class);
$this->user = User::factory()->create();
$this->school = School::factory()->create();
});
it('throws an exception if the user does not exist', function () {
$newUser = User::factory()->make();
$this->assigner->assign($newUser, $this->school);
})->throws(AuditionAdminException::class, 'User does not exist');
it('throws an exception if the school does not exist', function () {
$newSchool = School::factory()->make();
$this->assigner->assign($this->user, $newSchool);
})->throws(AuditionAdminException::class, 'School does not exist');
it('assigns a user to a school', function () {
($this->assigner)($this->user, $this->school);
expect($this->user->school_id)->toEqual($this->school->id);
});
it('logs the assignment of the user to the school', function () {
($this->assigner)($this->user, $this->school);
$logEntry = AuditLogEntry::first();
expect($logEntry->message)->toEqual('Assigned '.$this->user->full_name().' to school '.$this->school->name);
});
it('changes a users school assignment', function () {
$this->user->school_id = $this->school->id;
$this->user->save();
$newSchool = School::factory()->create();
($this->assigner)($this->user, $newSchool);
$this->user->refresh();
expect($this->user->school_id)->toEqual($newSchool->id);
});
it('logs a change in school assignment', function () {
$this->user->school_id = $this->school->id;
$this->user->save();
$newSchool = School::factory()->create();
($this->assigner)($this->user, $newSchool);
$this->user->refresh();
$logEntry = AuditLogEntry::first();
expect($logEntry->message)->toEqual('Moved '.$this->user->full_name().' from school '.$this->school->name.' to school '.$newSchool->name);
});