diff --git a/app/Actions/Schools/CreateSchool.php b/app/Actions/Schools/CreateSchool.php new file mode 100644 index 0000000..dcada3b --- /dev/null +++ b/app/Actions/Schools/CreateSchool.php @@ -0,0 +1,42 @@ +create($name, $address, $city, $state, $zip); + } + + public function create( + string $name, + ?string $address = null, + ?string $city = null, + ?string $state = null, + ?string $zip = null + ): School { + $newSchool = School::create([ + 'name' => $name, + 'address' => $address, + 'city' => $city, + 'state' => $state, + 'zip' => $zip, + ]); + + if (auth()->user()) { + $message = 'Created school '.$newSchool->name; + $affects = ['schools' => [$newSchool->id]]; + auditionLog($message, $affects); + } + + return $newSchool; + } +} diff --git a/tests/Feature/app/Actions/Schools/CreateSchoolTest.php b/tests/Feature/app/Actions/Schools/CreateSchoolTest.php new file mode 100644 index 0000000..a05b248 --- /dev/null +++ b/tests/Feature/app/Actions/Schools/CreateSchoolTest.php @@ -0,0 +1,49 @@ +creator = app(CreateSchool::class); +}); + +it('creates a school', function () { + $newSchool = ($this->creator)( + 'Longfellow Intermediate', + '210 East 4th Stret', + 'Chelsea', + 'OK', + '74016', + ); + $dbSchool = School::first(); + expect(School::where('name', 'Longfellow Intermediate')->exists())->toBeTrue() + ->and($dbSchool->name)->toEqual('Longfellow Intermediate') + ->and($dbSchool->address)->toEqual('210 East 4th Stret') + ->and($dbSchool->city)->toEqual('Chelsea') + ->and($dbSchool->state)->toEqual('OK') + ->and($dbSchool->zip)->toEqual('74016'); +}); + +it('logs school creation', function () { + actAsAdmin(); + $schoolAddress = fake()->streetAddress(); + $schoolCity = fake()->city(); + $schoolState = 'OK'; + $schoolZip = fake()->postcode(); + $schoolName = $schoolCity.' High School'; + $this->creator->create( + $schoolName, + $schoolAddress, + $schoolCity, + $schoolState, + $schoolZip, + ); + $logEntry = \App\Models\AuditLogEntry::first(); + expect($logEntry->message)->toEqual('Created school '.$schoolName) + ->and($logEntry->affected['schools'])->toEqual([1]) + ->and($logEntry->user)->toEqual(auth()->user()->email); + +});