From 5ebef46be7e6df4a527b7039e7171902aedf9780 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Tue, 1 Jul 2025 18:40:44 -0500 Subject: [PATCH] Create AssignUserToSchool action and test. --- app/Actions/Schools/AssignUserToSchool.php | 46 +++++++++++++++ app/Providers/AppServiceProvider.php | 3 + .../Schools/AssignUserToSchoolTest.php | 58 +++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 app/Actions/Schools/AssignUserToSchool.php create mode 100644 tests/Feature/app/Actions/Schools/AssignUserToSchoolTest.php diff --git a/app/Actions/Schools/AssignUserToSchool.php b/app/Actions/Schools/AssignUserToSchool.php new file mode 100644 index 0000000..71f14af --- /dev/null +++ b/app/Actions/Schools/AssignUserToSchool.php @@ -0,0 +1,46 @@ +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()); + } + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 09e5005..1fe6aaa 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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); diff --git a/tests/Feature/app/Actions/Schools/AssignUserToSchoolTest.php b/tests/Feature/app/Actions/Schools/AssignUserToSchoolTest.php new file mode 100644 index 0000000..a11d347 --- /dev/null +++ b/tests/Feature/app/Actions/Schools/AssignUserToSchoolTest.php @@ -0,0 +1,58 @@ +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); +});