diff --git a/app/Http/Controllers/Api/ProjectInvitationController.php b/app/Http/Controllers/Api/ProjectInvitationController.php index 38344e0a3..afa1e1f3b 100644 --- a/app/Http/Controllers/Api/ProjectInvitationController.php +++ b/app/Http/Controllers/Api/ProjectInvitationController.php @@ -83,7 +83,7 @@ public function join(JoinProjectInvitation $request, $id) if ($invitation->add_to_sessions) { AnnotationSession::join('project_volume', 'annotation_sessions.volume_id', '=', 'project_volume.volume_id') ->where('project_volume.project_id', $project->id) - ->eachById(fn ($session) => $session->users()->attach($userId)); + ->eachById(fn ($session) => $session->users()->syncWithoutDetaching([$userId])); } } diff --git a/tests/php/Http/Controllers/Api/ProjectInvitationControllerTest.php b/tests/php/Http/Controllers/Api/ProjectInvitationControllerTest.php index 9ab75313c..b654083f6 100644 --- a/tests/php/Http/Controllers/Api/ProjectInvitationControllerTest.php +++ b/tests/php/Http/Controllers/Api/ProjectInvitationControllerTest.php @@ -259,6 +259,35 @@ public function testJoinAddToSessions() $this->assertNotNull($session->users()->find($this->user()->id)); } + public function testJoinAddToSessionsAlreadyExist() + { + $session = AnnotationSession::factory()->create([ + 'volume_id' => $this->volume()->id, + 'starts_at' => '2023-11-04', + 'ends_at' => '2023-11-05', + ]); + + $invitation = ProjectInvitation::factory()->create([ + 'project_id' => $this->project()->id, + 'role_id' => Role::editorId(), + 'add_to_sessions' => true, + ]); + + $session->users()->attach($this->user()); + $session->users()->attach($this->expert()); + + $this->beUser(); + $this + ->postJson("/api/v1/project-invitations/{$invitation->id}/join", [ + 'token' => $invitation->uuid, + ]) + ->assertSuccessful(); + + $this->assertNotNull($session->users()->find($this->user()->id)); + // The expert should not be detached by this. + $this->assertNotNull($session->users()->find($this->expert()->id)); + } + public function testShowQrCode() { $invitation = ProjectInvitation::factory()->create([