diff --git a/app/Models/Mship/Concerns/HasWaitingLists.php b/app/Models/Mship/Concerns/HasWaitingLists.php index 3ab1be051..3ccfee0c1 100644 --- a/app/Models/Mship/Concerns/HasWaitingLists.php +++ b/app/Models/Mship/Concerns/HasWaitingLists.php @@ -20,13 +20,13 @@ public function waitingListAccounts() */ public function waitingLists() { - return $this->waitingListAccounts() + // waiting list accounts soft delete so this will exclude them + $waitingListAccounts = $this->waitingListAccounts() ->withTrashed() - ->get() - ->mapWithKeys(function (WaitingListAccount $waitingListAccount) { - return [$waitingListAccount->waitingList->id => $waitingListAccount->waitingList]; - }) - ->values(); + ->with('waitingList') + ->get(); + + return $this->extractListsFromWaitingListAccounts($waitingListAccounts); } /** @@ -41,7 +41,15 @@ public function currentWaitingLists(): Collection ->with('waitingList') ->get(); - // @fixme maybe replace with mapWithKeys + return $this->extractListsFromWaitingListAccounts($waitingListAccounts); + } + + /** + * @param Collection $waitingListAccounts + * @return Collection + */ + private function extractListsFromWaitingListAccounts(Collection $waitingListAccounts): Collection + { $waitingLists = collect(); foreach ($waitingListAccounts as $waitingListAccount) { $waitingList = $waitingListAccount->waitingList; diff --git a/tests/Unit/Account/Relationships/AccountWaitingListsTest.php b/tests/Unit/Account/Relationships/AccountWaitingListsTest.php index 58f1750eb..786165309 100644 --- a/tests/Unit/Account/Relationships/AccountWaitingListsTest.php +++ b/tests/Unit/Account/Relationships/AccountWaitingListsTest.php @@ -47,4 +47,16 @@ public function itCanGetAllCurrentWaitingLists() $this->assertCount(1, $this->user->fresh()->currentWaitingLists()); $this->assertContains($this->currentWaitingList->id, $this->user->fresh()->currentWaitingLists()->pluck('id')); } + + /** @test */ + public function itCanHandleTrashedWaitingLists() + { + $trashed = factory(WaitingList::class)->create(); + $trashed->addToWaitingList($this->user, $this->privacc); + $trashed->delete(); + $trashed->save(); + + $this->assertCount(1, $this->user->fresh()->currentWaitingLists()); + $this->assertCount(2, $this->user->fresh()->waitingLists()); + } }