From 6715c9346a2b1177ed0accf227f935ba24bdd497 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Fri, 4 Jul 2025 11:31:43 -0500 Subject: [PATCH] Create DoublerSync action to replace static methods on Doubler class. --- app/Actions/Tabulation/DoublerSync.php | 85 ++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 app/Actions/Tabulation/DoublerSync.php diff --git a/app/Actions/Tabulation/DoublerSync.php b/app/Actions/Tabulation/DoublerSync.php new file mode 100644 index 0000000..ee64180 --- /dev/null +++ b/app/Actions/Tabulation/DoublerSync.php @@ -0,0 +1,85 @@ +syncForEvent($event); + } else { + $this->syncAllDoublers(); + } + } + + public function syncForEvent(Event|int $eventId): void + { + 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 + Doubler::create([ + 'student_id' => $student->id, + 'event_id' => $eventId, + 'entries' => $entryList, + 'accepted_entry' => $acceptedEntryId, + ]); + + } + + // remove doubler records for students who no longer have multiple entries + Doubler::where('event_id', $eventId) + ->whereNotIn('student_id', $studentsWithMultipleEntries->pluck('id')) + ->delete(); + } + + public function syncAllDoublers(): void + { + $events = Event::all(); + foreach ($events as $event) { + $this->syncForEvent($event); + } + } +}