From d9a7e97047ac2251cd28db9146ac2bb2d3846ee6 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Sun, 22 Jun 2025 23:19:38 -0500 Subject: [PATCH] Doubler model and migration created --- app/Models/Doubler.php | 82 ++++++++++++++++++- ...025_06_20_175904_create_doublers_table.php | 13 ++- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/app/Models/Doubler.php b/app/Models/Doubler.php index c280c05..e0714bb 100644 --- a/app/Models/Doubler.php +++ b/app/Models/Doubler.php @@ -2,10 +2,88 @@ namespace App\Models; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; + +use function PHPUnit\Framework\isInstanceOf; class Doubler extends Model { - use HasFactory; + // Specify that we're not using a single primary key + protected $primaryKey = null; + + public $incrementing = false; + + protected $guarded = []; + + public function student(): BelongsTo + { + return $this->belongsTo(Student::class); + } + + public function event(): BelongsTo + { + return $this->belongsTo(Event::class); + } + + // Find a doubler based on both keys + public static function findDoubler($studentId, $eventId) + { + return static::where('student_id', $studentId) + ->where('event_id', $eventId) + ->first(); + } + + /** + * Sync doubler records for a specified event + */ + public static function syncForEvent($eventId): void + { + if (isInstanceOf(Event::class, $eventId)) { + $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') + ->get(); + + foreach ($studentsWithMultipleEntries as $student) { + // Get entries that are not declined. If only one, they're our accepted entry. + $availableEntries = $student->entries()->available()->get(); + if ($availableEntries->count() === 1) { + $acceptedEntryId = $availableEntries->first()->id; + } else { + $acceptedEntryId = null; + } + // Create or update the doubler record + static::updateOrCreate( + [ + 'student_id' => $student->id, + 'event_id' => $eventId, + ], + [ + '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/database/migrations/2025_06_20_175904_create_doublers_table.php b/database/migrations/2025_06_20_175904_create_doublers_table.php index 8e26400..8d2c16b 100644 --- a/database/migrations/2025_06_20_175904_create_doublers_table.php +++ b/database/migrations/2025_06_20_175904_create_doublers_table.php @@ -1,5 +1,7 @@ id(); + // Foreign keys that will form the composite primary key + $table->foreignIdFor(Student::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate(); + $table->foreignIdFor(Event::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate(); + + // Doubler Specific Fields + $table->integer('entry_count'); + $table->foreignIdFor(Entry::class, 'accepted_entry')->nullable()->constrained()->cascadeOnDelete()->cascadeOnUpdate(); + + // Set the composite primary key + $table->primary(['student_id', 'event_id']); $table->timestamps(); }); }