Cleanup on doubler model

This commit is contained in:
Matt Young 2025-06-23 03:25:10 -05:00
parent f0daa05fcf
commit 88ef36d8be
1 changed files with 18 additions and 16 deletions

View File

@ -62,29 +62,31 @@ class Doubler extends Model
->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)
->whereDoesntHave('flags', function ($query) {
$query->whereIn('flag_name', ['declined', 'no-show', 'failed-prelim']);
})
->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::updateOrCreate(
[
'student_id' => $student->id,
'event_id' => $eventId,
],
[
'entries' => $entryList,
'accepted_entry' => $acceptedEntryId,
]
);
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