Skip to content

Fixed Registration Bug: soft deleted user cannot re-register - throws unhandled error #1810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/database/migrations/1715028537217-CreateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class CreateUser1715028537217 implements MigrationInterface {
`ALTER TABLE "user" ADD CONSTRAINT "FK_dc18daa696860586ba4667a9d31" FOREIGN KEY ("statusId") REFERENCES "status"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "session" ADD CONSTRAINT "FK_3d2f174ef04fb312fdebd0ddc53" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
`ALTER TABLE "session" ADD CONSTRAINT "FK_3d2f174ef04fb312fdebd0ddc53" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class SessionEntity extends EntityRelationalHelper {

@ManyToOne(() => UserEntity, {
eager: true,
onDelete: 'CASCADE',
})
@Index()
user: UserEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ export class UsersRelationalRepository implements UserRepository {

return entity ? UserMapper.toDomain(entity) : null;
}

async findByEmailIncludingDeleted(email: User['email']): Promise<NullableType<User>> {
if (!email) return null;

const entity = await this.usersRepository.findOne({
withDeleted: true,
where: { email },
});

return entity ? UserMapper.toDomain(entity) : null;
}

async findBySocialIdAndProvider({
socialId,
Expand Down Expand Up @@ -123,4 +134,12 @@ export class UsersRelationalRepository implements UserRepository {
async remove(id: User['id']): Promise<void> {
await this.usersRepository.softDelete(id);
}

async removePermanently(id: User['id']): Promise<void> {
await this.usersRepository.delete(id);
}

async restore(id: User['id']): Promise<void> {
await this.usersRepository.restore(id);
}
}
3 changes: 3 additions & 0 deletions src/users/infrastructure/persistence/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export abstract class UserRepository {
abstract findById(id: User['id']): Promise<NullableType<User>>;
abstract findByIds(ids: User['id'][]): Promise<User[]>;
abstract findByEmail(email: User['email']): Promise<NullableType<User>>;
abstract findByEmailIncludingDeleted(email: User['email']): Promise<NullableType<User>>;
abstract findBySocialIdAndProvider({
socialId,
provider,
Expand All @@ -37,4 +38,6 @@ export abstract class UserRepository {
): Promise<User | null>;

abstract remove(id: User['id']): Promise<void>;
abstract removePermanently(id: User['id']): Promise<void>;
abstract restore(id: User['id']): Promise<void>;
}
18 changes: 11 additions & 7 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ export class UsersService {
let email: string | null = null;

if (createUserDto.email) {
const userObject = await this.usersRepository.findByEmail(
const userObject = await this.usersRepository.findByEmailIncludingDeleted(
createUserDto.email,
);
if (userObject) {
throw new UnprocessableEntityException({
status: HttpStatus.UNPROCESSABLE_ENTITY,
errors: {
email: 'emailAlreadyExists',
},
});
if (userObject.deletedAt) {
await this.usersRepository.removePermanently(userObject.id);
} else {
throw new UnprocessableEntityException({
status: HttpStatus.UNPROCESSABLE_ENTITY,
errors: {
email: 'emailAlreadyExists',
},
});
}
}
email = createUserDto.email;
}
Expand Down