Merge pull request #247 from SyncrowIOT/fix-delted-user-error

Refactor user and invited user validation in InviteUserService
This commit is contained in:
faris Aljohari
2025-02-04 07:40:31 -06:00
committed by GitHub

View File

@ -181,29 +181,12 @@ export class InviteUserService {
where: { email },
relations: ['project'],
});
if (!user?.isActive) {
throw new HttpException('This user is deleted', HttpStatus.BAD_REQUEST);
}
if (user?.project) {
throw new HttpException(
'This email already has a project',
HttpStatus.BAD_REQUEST,
);
}
this.validateUserOrInvite(user, 'User');
const invitedUser = await this.inviteUserRepository.findOne({
where: { email },
relations: ['project'],
});
if (!invitedUser?.isActive) {
throw new HttpException('This user is deleted', HttpStatus.BAD_REQUEST);
}
if (invitedUser?.project) {
throw new HttpException(
'This email already has a project',
HttpStatus.BAD_REQUEST,
);
}
this.validateUserOrInvite(invitedUser, 'Invited User');
return new SuccessResponseDto({
statusCode: HttpStatus.OK,
@ -219,6 +202,24 @@ export class InviteUserService {
);
}
}
private validateUserOrInvite(user: any, userType: string): void {
if (user) {
if (!user.isActive) {
throw new HttpException(
`${userType} is deleted`,
HttpStatus.BAD_REQUEST,
);
}
if (user.project) {
throw new HttpException(
`${userType} already has a project`,
HttpStatus.BAD_REQUEST,
);
}
}
}
async activationCode(dto: ActivateCodeDto): Promise<BaseResponseDto> {
const { activationCode, userUuid } = dto;