From 5ccac95ee152815a06241e10942faa88890ad891 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 4 Feb 2025 07:38:00 -0600 Subject: [PATCH] Refactor user and invited user validation in InviteUserService --- .../services/invite-user.service.ts | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/invite-user/services/invite-user.service.ts b/src/invite-user/services/invite-user.service.ts index 6b90321..e466248 100644 --- a/src/invite-user/services/invite-user.service.ts +++ b/src/invite-user/services/invite-user.service.ts @@ -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 { const { activationCode, userUuid } = dto;