Skip to content

Commit

Permalink
fix participant soft deletes (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgmyr authored Jan 25, 2022
1 parent b7f205b commit 26faa96
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,15 @@ public function participants()
*/
public function users()
{
return $this->belongsToMany(Models::classname('User'), Models::table('participants'), 'thread_id', 'user_id');
return $this
->belongsToMany(
Models::classname('User'),
Models::table('participants'),
'thread_id',
'user_id'
)
->whereNull(Models::table('participants') . '.deleted_at')
->withTimestamps();
}

/**
Expand Down
15 changes: 9 additions & 6 deletions src/Traits/Messagable.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ public function participants()
*/
public function threads()
{
return $this->belongsToMany(
Models::classname(Thread::class),
Models::table('participants'),
'user_id',
'thread_id'
);
return $this
->belongsToMany(
Models::classname(Thread::class),
Models::table('participants'),
'user_id',
'thread_id'
)
->whereNull(Models::table('participants') . '.deleted_at')
->withTimestamps();
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/MessagableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,31 @@ public function it_should_get_participant_threads(): void
$firstThread = $user->threads->first();
$this->assertInstanceOf(Thread::class, $firstThread);
}

/** @test */
public function it_should_not_include_deleted_threads(): void
{
$user = $this->userFactory();

$thread1 = $this->threadFactory();
$user_thread1 = $this->participantFactory([
'user_id' => $user->id,
'thread_id' => $thread1->id,
]);


$thread2 = $this->threadFactory();
$user_thread2 = $this->participantFactory([
'user_id' => $user->id,
'thread_id' => $thread2->id,
]);

$this->assertSame(2, $user->threads()->count());
$this->assertSame(1, $thread1->users()->count());

$thread1->removeParticipant($user->id);

$this->assertSame(1, $user->threads()->count());
$this->assertSame(0, $thread1->users()->count());
}
}

0 comments on commit 26faa96

Please sign in to comment.