From f4243875a6fa6175f5e59252a14046205b33c8a5 Mon Sep 17 00:00:00 2001 From: bhavanakarwade <137506897+bhavanakarwade@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:48:17 +0530 Subject: [PATCH] fix: send email verification issue (#823) Signed-off-by: bhavanakarwade --- apps/user/src/user.service.ts | 48 +++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/apps/user/src/user.service.ts b/apps/user/src/user.service.ts index 6f8675235..b4d0259c1 100644 --- a/apps/user/src/user.service.ts +++ b/apps/user/src/user.service.ts @@ -87,48 +87,52 @@ export class UserService { * @param userEmailVerification * @returns */ + async sendVerificationMail(userEmailVerification: ISendVerificationEmail): Promise { try { - const { email, brandLogoUrl, platformName } = userEmailVerification; - + const { email, brandLogoUrl, platformName, clientId, clientSecret } = userEmailVerification; + if ('PROD' === process.env.PLATFORM_PROFILE_MODE) { // eslint-disable-next-line prefer-destructuring const domain = email.split('@')[1]; - if (DISALLOWED_EMAIL_DOMAIN.includes(domain)) { throw new BadRequestException(ResponseMessages.user.error.InvalidEmailDomain); } } + const userDetails = await this.userRepository.checkUserExist(email); - - if (userDetails?.isEmailVerified) { - throw new ConflictException(ResponseMessages.user.error.exists); - } - - if (userDetails && !userDetails.isEmailVerified) { - throw new ConflictException(ResponseMessages.user.error.verificationAlreadySent); + + if (userDetails) { + if (userDetails.isEmailVerified) { + throw new ConflictException(ResponseMessages.user.error.exists); + } else { + throw new ConflictException(ResponseMessages.user.error.verificationAlreadySent); + } } - + const verifyCode = uuidv4(); - const uniqueUsername = await this.createUsername(email, verifyCode); - userEmailVerification.username = uniqueUsername; - const resUser = await this.userRepository.createUser(userEmailVerification, verifyCode); - - const token = await this.clientRegistrationService.getManagementToken(resUser.clientId, resUser.clientSecret); - const getClientData = await this.clientRegistrationService.getClientRedirectUrl(resUser.clientId, token); + let sendVerificationMail: boolean; + try { + const token = await this.clientRegistrationService.getManagementToken(clientId, clientSecret); + const getClientData = await this.clientRegistrationService.getClientRedirectUrl(clientId, token); const [redirectUrl] = getClientData[0]?.redirectUris || []; - + if (!redirectUrl) { throw new NotFoundException(ResponseMessages.user.error.redirectUrlNotFound); } - - await this.sendEmailForVerification(email, resUser.verificationCode, redirectUrl, resUser.clientId, brandLogoUrl, platformName); + + sendVerificationMail = await this.sendEmailForVerification(email, verifyCode, redirectUrl, clientId, brandLogoUrl, platformName); } catch (error) { throw new InternalServerErrorException(ResponseMessages.user.error.emailSend); } - - return resUser; + + if (sendVerificationMail) { + const uniqueUsername = await this.createUsername(email, verifyCode); + userEmailVerification.username = uniqueUsername; + const resUser = await this.userRepository.createUser(userEmailVerification, verifyCode); + return resUser; + } } catch (error) { this.logger.error(`In Create User : ${JSON.stringify(error)}`); throw new RpcException(error.response ? error.response : error);