replace calls to Doubler model static methods with calls to the DoublerSync action
This commit is contained in:
parent
879403cc33
commit
86fb7a7e62
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue