Skip to content

Commit 2c60d04

Browse files
committed
override current_team_id in makeMemberToPlaceholder to avoid fk constraint error on user delete, fixes #989
1 parent 2c222f3 commit 2c60d04

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

app/Service/MemberService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public function makeMemberToPlaceholder(Member $member, bool $makeSureUserHasAtL
196196

197197
$placeholderUser = $user->replicate();
198198
$placeholderUser->is_placeholder = true;
199+
$placeholderUser->current_team_id = $member->organization_id;
199200
$placeholderUser->save();
200201

201202
$member->user()->associate($placeholderUser);

tests/Unit/Service/DeletionServiceTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,45 @@ public function test_delete_user_deletes_owned_organizations_that_have_only_one_
382382
1
383383
);
384384
}
385+
386+
public function test_delete_user_with_current_organization_set_to_owned_org_that_will_be_deleted_does_not_cause_foreign_key_violation(): void
387+
{
388+
// Arrange
389+
// User A creates an organization and invites User B
390+
$userA = User::factory()->create();
391+
$userB = User::factory()->create();
392+
$organizationOfA = Organization::factory()->withOwner($userA)->create();
393+
$organizationOfB = Organization::factory()->withOwner($userB)->create();
394+
Member::factory()->forUser($userA)->forOrganization($organizationOfA)->role(Role::Owner)->create();
395+
Member::factory()->forUser($userB)->forOrganization($organizationOfB)->role(Role::Owner)->create();
396+
$memberBInOrgA = Member::factory()->forUser($userB)->forOrganization($organizationOfA)->role(Role::Employee)->create();
397+
TimeEntry::factory()->forOrganization($organizationOfA)->forMember($memberBInOrgA)->createMany(2);
398+
399+
// User B's current_organization_id points to their own org (the one that will be deleted)
400+
$userB->update(['current_team_id' => $organizationOfB->getKey()]);
401+
402+
// Act
403+
$this->deletionService->deleteUser($userB);
404+
405+
// Assert
406+
$this->assertDatabaseMissing(User::class, [
407+
'id' => $userB->getKey(),
408+
]);
409+
$this->assertDatabaseMissing(Organization::class, [
410+
'id' => $organizationOfB->getKey(),
411+
]);
412+
$this->assertDatabaseHas(Organization::class, [
413+
'id' => $organizationOfA->getKey(),
414+
]);
415+
// The placeholder user should exist with current_team_id set to the org where they are a placeholder
416+
$placeholderUser = User::query()->where('is_placeholder', true)->first();
417+
$this->assertNotNull($placeholderUser);
418+
$this->assertSame($organizationOfA->getKey(), $placeholderUser->current_team_id);
419+
$this->assertDatabaseHas(Member::class, [
420+
'id' => $memberBInOrgA->getKey(),
421+
'user_id' => $placeholderUser->getKey(),
422+
'organization_id' => $organizationOfA->getKey(),
423+
'role' => Role::Placeholder->value,
424+
]);
425+
}
385426
}

0 commit comments

Comments
 (0)