From 86fb7a7e623da9f40542740b003fcf56226e5a8e Mon Sep 17 00:00:00 2001 From: Matt Young Date: Fri, 4 Jul 2025 12:27:41 -0500 Subject: [PATCH] replace calls to Doubler model static methods with calls to the DoublerSync action --- app/Console/Commands/SyncDoublers.php | 7 +-- app/Models/Doubler.php | 62 ------------------------ app/Observers/EntryFlagObserver.php | 4 +- app/Observers/EntryObserver.php | 12 +++-- tests/Feature/app/Models/DoublerTest.php | 7 --- 5 files changed, 14 insertions(+), 78 deletions(-) diff --git a/app/Console/Commands/SyncDoublers.php b/app/Console/Commands/SyncDoublers.php index 22acab3..ff8826e 100644 --- a/app/Console/Commands/SyncDoublers.php +++ b/app/Console/Commands/SyncDoublers.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Models\Doubler; +use App\Actions\Tabulation\DoublerSync; use App\Models\Event; use Illuminate\Console\Command; @@ -30,12 +30,13 @@ class SyncDoublers extends Command */ public function handle() { + $syncer = app(DoublerSync::class); if ($eventId = $this->argument('event')) { $event = Event::findOrFail($eventId); - Doubler::syncForEvent($event); + $syncer($event); $this->info("Synced doublers for event {$event->name}"); } else { - Doubler::syncDoublers(); + $syncer(); $this->info('Synced doublers for all events'); } } diff --git a/app/Models/Doubler.php b/app/Models/Doubler.php index 3a4504c..20c71a6 100644 --- a/app/Models/Doubler.php +++ b/app/Models/Doubler.php @@ -40,66 +40,4 @@ class Doubler extends Model ->where('event_id', $eventId) ->first(); } - - /** - * Sync doubler records for a specified event - */ - public static function syncForEvent($eventId): void - { - // TODO: Move static functions to an action to facilitate testing - - if ($eventId instanceof Event) { - $eventId = $eventId->id; - } - - // Get students with multiple entries in this event's auditions - $studentsWithMultipleEntries = Student::query() - ->select('students.id') - ->join('entries', 'students.id', '=', 'entries.student_id') - ->join('auditions', 'entries.audition_id', '=', 'auditions.id') - ->where('auditions.event_id', $eventId) - ->groupBy('students.id') - ->havingRaw('COUNT(entries.id) > 1') - ->with('entries') - ->get(); - Doubler::where('event_id', $eventId)->delete(); - foreach ($studentsWithMultipleEntries as $student) { - // Get entries that are not declined. If only one, they're our accepted entry. - $entryList = collect(); // List of entry ids for th is student in this event - $undecidedEntries = collect(); // List of entry ids that are not declined, no-show, or failed prelim - $entryList = $student->entriesForEvent($eventId)->pluck('id'); - $undecidedEntries = $student->entriesForEvent($eventId)->filter(function ($entry) { - return ! $entry->hasFlag('declined') - && ! $entry->hasFlag('no_show') - && ! $entry->hasFlag('failed_prelim'); - })->pluck('id'); - if ($undecidedEntries->count() < 2) { - $acceptedEntryId = $undecidedEntries->first(); - } else { - $acceptedEntryId = null; - } - - // Create or update the doubler record - static::create([ - 'student_id' => $student->id, - 'event_id' => $eventId, - 'entries' => $entryList, - 'accepted_entry' => $acceptedEntryId, - ]); - - } - - // remove doubler records for students who no longer have multiple entries - static::where('event_id', $eventId) - ->whereNotIn('student_id', $studentsWithMultipleEntries->pluck('id')) - ->delete(); - } - - public static function syncDoublers(): void - { - $events = Event::all(); - foreach ($events as $event) { - static::syncForEvent($event); - } - } } diff --git a/app/Observers/EntryFlagObserver.php b/app/Observers/EntryFlagObserver.php index 1756c50..4306343 100644 --- a/app/Observers/EntryFlagObserver.php +++ b/app/Observers/EntryFlagObserver.php @@ -4,7 +4,6 @@ namespace App\Observers; use App\Actions\Tabulation\DoublerSync; use App\Exceptions\AuditionAdminException; -use App\Models\Doubler; use App\Models\EntryFlag; use Illuminate\Support\Facades\Cache; @@ -46,7 +45,8 @@ class EntryFlagObserver public function deleted(EntryFlag $entryFlag): void { - Doubler::syncDoublers(); + $syncer = app(DoublerSync::class); + $syncer(); Cache::forget('rank_advancement_'.$entryFlag->entry->audition_id); Cache::forget('rank_seating_'.$entryFlag->entry->audition_id); diff --git a/app/Observers/EntryObserver.php b/app/Observers/EntryObserver.php index e89e4ed..4d6614f 100644 --- a/app/Observers/EntryObserver.php +++ b/app/Observers/EntryObserver.php @@ -2,6 +2,7 @@ namespace App\Observers; +use App\Actions\Tabulation\DoublerSync; use App\Models\Audition; use App\Models\Doubler; use App\Models\Entry; @@ -22,7 +23,8 @@ class EntryObserver } // Update doublers for the event - Doubler::syncForEvent($entry->audition->event_id); + $syncer = app(DoublerSync::class); + $syncer($entry->audition->event_id); } @@ -31,10 +33,11 @@ class EntryObserver */ public function updated(Entry $entry): void { + $syncer = app(DoublerSync::class); // Update doubler table when an entry is updated - Doubler::syncForEvent($entry->audition->event_id); + $syncer($entry->audition->event_id); if ($entry->wasChanged('audition_id')) { - Doubler::syncDoublers(); + $syncer(); } } @@ -43,9 +46,10 @@ class EntryObserver */ public function deleted(Entry $entry): void { + $syncer = app(DoublerSync::class); Doubler::where('student_id', $entry->student_id)->delete(); $audition = Audition::where('id', $entry->audition_id)->first(); - Doubler::syncForEvent($audition->event_id); + $syncer($audition->event_id); } /** diff --git a/tests/Feature/app/Models/DoublerTest.php b/tests/Feature/app/Models/DoublerTest.php index e152620..6bf6449 100644 --- a/tests/Feature/app/Models/DoublerTest.php +++ b/tests/Feature/app/Models/DoublerTest.php @@ -44,10 +44,3 @@ it('can return its entries', function () { it('can find a doubler', function () { expect(Doubler::findDoubler($this->student->id, $this->event->id))->toBeInstanceOf(Doubler::class); }); - -it('can load doublers for a given event', function () { - Doubler::truncate(); - expect(Doubler::count())->toBe(0); - Doubler::syncForEvent($this->event->id); - expect(Doubler::count())->toBe(1); -});