Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pkg/service/user_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (

// MergeUsers consolidates the retiring user into the surviving user. All
// identity-forward state moves to the survivor; historical truth stays in
// place. The retiring user is flipped to status=MERGED. All work happens in a
// single transaction.
// place. The retiring user is flipped to status=MERGED, and a PENDING survivor
// of an ACTIVE user becomes ACTIVE. All work happens in a single transaction.
//
// Moved to survivor (duplicates on the retiring user are dropped first):
// - user_identities
Expand Down Expand Up @@ -106,6 +106,13 @@ func (s *Service) MergeUsersTx(ctx context.Context, tx *sql.Tx, survivingID, ret
}

if err := func() error {
// The survivor takes the retiring user's sign-in identity, so it is ACTIVE if either was.
if retiring.Status == models.UserActive && survivor.Status != models.UserActive {
if err := s.users.UpdateStatus(ctx, tx, survivingID, models.UserActive); err != nil {
return err
}
survivor.Status = models.UserActive
}
if err := s.userIdentities.ReassignUser(ctx, tx, retiringID, survivingID); err != nil {
return fmt.Errorf("reassign user identities: %w", err)
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/service/user_merge_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,16 @@ func TestDeleteUser_RefusesAUserStillReferenced(t *testing.T) {
t.Fatalf("delete a project PI: %v, want ErrInUse", err)
}
}

func TestMergeUsers_ActivatesPendingSurvivorOfActiveUser(t *testing.T) {
database := setupTestDB(t)
svc := newTestService(database)
survivor := seedPendingUserWithEmail(t, database, "survivor@example.edu")
retiring := seedUser(t, database, "retiring@example.edu")
if _, err := svc.MergeUsers(ctx(), survivor, retiring); err != nil {
t.Fatalf("merge: %v", err)
}
if after, err := svc.GetUser(ctx(), survivor); err != nil || after.Status != models.UserActive {
t.Errorf("survivor: got %+v, %v, want ACTIVE", after, err)
}
}