From 3aa867e812fbcf814659187e0e49c290ef795632 Mon Sep 17 00:00:00 2001 From: Artem Niehrieiev Date: Wed, 31 Jul 2024 09:46:49 +0000 Subject: [PATCH] refactor: Remove VerifyAddUserInGroupUseCase and related files --- .../src/entities/group/group.controller.ts | 65 +---------------- backend/src/entities/group/group.module.ts | 5 -- .../verify-add-user-in-group.use.case.ts | 69 ------------------- 3 files changed, 1 insertion(+), 138 deletions(-) delete mode 100644 backend/src/entities/group/use-cases/verify-add-user-in-group.use.case.ts diff --git a/backend/src/entities/group/group.controller.ts b/backend/src/entities/group/group.controller.ts index 5bf38c5c..b7836eb5 100644 --- a/backend/src/entities/group/group.controller.ts +++ b/backend/src/entities/group/group.controller.ts @@ -8,29 +8,23 @@ import { Inject, Injectable, Put, - Req, - Res, UseGuards, UseInterceptors, } from '@nestjs/common'; -import { Response, Request } from 'express'; import { UseCaseType } from '../../common/data-injection.tokens.js'; -import { SlugUuid, UserId, VerificationString } from '../../decorators/index.js'; +import { SlugUuid, UserId } from '../../decorators/index.js'; import { AmplitudeEventTypeEnum, InTransactionEnum } from '../../enums/index.js'; import { Messages } from '../../exceptions/text/messages.js'; import { GroupEditGuard, GroupReadGuard } from '../../guards/index.js'; import { Cacher } from '../../helpers/cache/cacher.js'; -import { Constants } from '../../helpers/constants/constants.js'; import { SentryInterceptor } from '../../interceptors/index.js'; import { AmplitudeService } from '../amplitude/amplitude.service.js'; import { FoundUserInGroupDs } from '../user/application/data-structures/found-user-in-group.ds.js'; -import { IToken, ITokenExp } from '../user/utils/generate-gwt-token.js'; import { AddUserInGroupDs, AddUserInGroupWithSaaSDs } from './application/data-sctructures/add-user-in-group.ds.js'; import { AddedUserInGroupDs } from './application/data-sctructures/added-user-in-group.ds.js'; import { DeletedGroupResultDs } from './application/data-sctructures/deleted-group-result.ds.js'; import { FoundGroupDataInfoDs, FoundUserGroupsDs } from './application/data-sctructures/found-user-groups.ds.js'; import { RemoveUserFromGroupResultDs } from './application/data-sctructures/remove-user-from-group-result.ds.js'; -import { VerifyAddUserInGroupDs } from './application/data-sctructures/verify-add-user-in-group.ds.js'; import { IAddUserInGroup, IDeleteGroup, @@ -38,14 +32,10 @@ import { IFindUserGroups, IRemoveUserFromGroup, IUpdateGroupTitle, - IVerifyAddUserInGroup, } from './use-cases/use-cases.interfaces.js'; import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { AddUserInGroupDto } from './dto/add-user-ingroup-dto.js'; -import { TokenExpirationResponseDto } from '../company-info/application/dto/token-expiration-response.dto.js'; import { DeleteUserFromGroupDTO } from './dto/delete-user-from-group-dto.js'; -import { VerifyUserInGroupInvitationDto } from './dto/verify-user-in-group-invitation-request-body.dto.js'; -import { getCookieDomainOptions } from '../user/utils/get-cookie-domain-options.js'; import { UpdateGroupTitleDto } from './dto/update-group-title.dto.js'; @UseInterceptors(SentryInterceptor) @@ -57,8 +47,6 @@ export class GroupController { constructor( @Inject(UseCaseType.INVITE_USER_IN_GROUP) private readonly addUserInGroupUseCase: IAddUserInGroup, - @Inject(UseCaseType.VERIFY_INVITE_USER_IN_GROUP) - private readonly verifyAddUserInGroupUseCase: IVerifyAddUserInGroup, @Inject(UseCaseType.FIND_ALL_USER_GROUPS) private readonly findAllUserGroupsUseCase: IFindUserGroups, @Inject(UseCaseType.FIND_ALL_USERS_IN_GROUP) @@ -147,57 +135,6 @@ export class GroupController { } } - @ApiOperation({ summary: 'Verify user in group invitation' }) - @ApiBody({ type: VerifyUserInGroupInvitationDto }) - @ApiResponse({ - status: 200, - description: 'Verify user in group invitation.', - type: TokenExpirationResponseDto, - }) - @Put('/group/user/verify/:verificationString') - async verifyUserInvitation( - @Body() verificationData: VerifyUserInGroupInvitationDto, - @Res({ passthrough: true }) response: Response, - @VerificationString('verificationString') verificationString: string, - @Req() req: Request, - ): Promise { - const { password, name } = verificationData; - try { - const tokenInReq = req.cookies[Constants.JWT_COOKIE_KEY_NAME]; - if (tokenInReq) { - response.clearCookie(Constants.JWT_COOKIE_KEY_NAME); - } - throw new HttpException( - { - message: Messages.TRY_VERIFY_ADD_USER_WHEN_LOGGED_IN, - }, - HttpStatus.BAD_REQUEST, - ); - } catch (e) {} - - const inputData: VerifyAddUserInGroupDs = { - verificationString: verificationString, - user_password: password, - user_name: name, - }; - const token: IToken = await this.verifyAddUserInGroupUseCase.execute(inputData, InTransactionEnum.ON); - response.cookie(Constants.JWT_COOKIE_KEY_NAME, token.token, { - httpOnly: true, - secure: true, - expires: token.exp, - ...getCookieDomainOptions(), - }); - response.cookie(Constants.ROCKETADMIN_AUTHENTICATED_COOKIE, token.exp.getTime(), { - httpOnly: false, - expires: token.exp, - ...getCookieDomainOptions(), - }); - return { - expires: token.exp, - isTemporary: token.isTemporary, - }; - } - @ApiOperation({ summary: 'Delete group' }) @ApiResponse({ status: 200, diff --git a/backend/src/entities/group/group.module.ts b/backend/src/entities/group/group.module.ts index db6304cd..a92b1ef0 100644 --- a/backend/src/entities/group/group.module.ts +++ b/backend/src/entities/group/group.module.ts @@ -16,7 +16,6 @@ import { DeleteGroupUseCase } from './use-cases/delete-group.use.case.js'; import { FindAllUserGroupsUseCase } from './use-cases/find-all-user-groups.use.case.js'; import { FindAllUsersInGroupUseCase } from './use-cases/find-all-users-in-group.use.case.js'; import { RemoveUserFromGroupUseCase } from './use-cases/remove-user-from-group.use.case.js'; -import { VerifyAddUserInGroupUseCase } from './use-cases/verify-add-user-in-group.use.case.js'; import { AddUserInGroupUseCase } from './use-cases/saas-add-user-in-group-v2.use.case.js'; import { UpdateGroupTitleUseCase } from './use-cases/update-group-title.use.case.js'; @@ -42,10 +41,6 @@ import { UpdateGroupTitleUseCase } from './use-cases/update-group-title.use.case provide: UseCaseType.INVITE_USER_IN_GROUP, useClass: AddUserInGroupUseCase, }, - { - provide: UseCaseType.VERIFY_INVITE_USER_IN_GROUP, - useClass: VerifyAddUserInGroupUseCase, - }, { provide: UseCaseType.FIND_ALL_USER_GROUPS, useClass: FindAllUserGroupsUseCase, diff --git a/backend/src/entities/group/use-cases/verify-add-user-in-group.use.case.ts b/backend/src/entities/group/use-cases/verify-add-user-in-group.use.case.ts deleted file mode 100644 index b70ffaed..00000000 --- a/backend/src/entities/group/use-cases/verify-add-user-in-group.use.case.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { HttpException, HttpStatus, Inject, Injectable, Scope } from '@nestjs/common'; -import AbstractUseCase from '../../../common/abstract-use.case.js'; -import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js'; -import { BaseType } from '../../../common/data-injection.tokens.js'; -import { Messages } from '../../../exceptions/text/messages.js'; -import { Constants } from '../../../helpers/constants/constants.js'; -import { Encryptor } from '../../../helpers/encryption/encryptor.js'; -import { ValidationHelper } from '../../../helpers/validators/validation-helper.js'; -import { generateGwtToken, IToken } from '../../user/utils/generate-gwt-token.js'; -import { VerifyAddUserInGroupDs } from '../application/data-sctructures/verify-add-user-in-group.ds.js'; -import { IVerifyAddUserInGroup } from './use-cases.interfaces.js'; -import { get2FaScope } from '../../user/utils/is-jwt-scope-need.util.js'; - -@Injectable({ scope: Scope.REQUEST }) -export class VerifyAddUserInGroupUseCase - extends AbstractUseCase - implements IVerifyAddUserInGroup -{ - constructor( - @Inject(BaseType.GLOBAL_DB_CONTEXT) - protected _dbContext: IGlobalDatabaseContext, - ) { - super(); - } - - protected async implementation(inputData: VerifyAddUserInGroupDs): Promise { - const { verificationString, user_password, user_name } = inputData; - ValidationHelper.isPasswordStrongOrThrowError(user_password); - const invitationEntity = - await this._dbContext.userInvitationRepository.findUserInvitationWithVerificationString(verificationString); - if (!invitationEntity || !invitationEntity.user) { - throw new HttpException( - { - message: Messages.VERIFICATION_LINK_INCORRECT, - }, - HttpStatus.BAD_REQUEST, - ); - } - const invitationTimeInMs = new Date(invitationEntity.createdAt).getTime(); - const oneDayAgoInMs = new Date(Constants.ONE_DAY_AGO()).getTime(); - - if (invitationTimeInMs <= oneDayAgoInMs) { - throw new HttpException( - { - messages: Messages.VERIFICATION_LINK_EXPIRED, - }, - HttpStatus.BAD_REQUEST, - ); - } - const foundCompany = await this._dbContext.companyInfoRepository.findCompanyInfoByUserId(invitationEntity.ownerId); - if (!invitationEntity.ownerId) { - const foundUser = await this._dbContext.userRepository.findOneUserById(invitationEntity.user.id); - foundUser.isActive = true; - foundUser.password = await Encryptor.hashUserPassword(user_password); - foundUser.name = user_name; - const savedUser = await this._dbContext.userRepository.saveUserEntity(foundUser); - await this._dbContext.userInvitationRepository.removeInvitationEntity(invitationEntity); - return generateGwtToken(savedUser, get2FaScope(savedUser, foundCompany)); - } - - const foundUser = await this._dbContext.userRepository.findOneUserById(invitationEntity.user.id); - foundUser.isActive = true; - foundUser.password = await Encryptor.hashUserPassword(user_password); - foundUser.name = user_name; - const savedUser = await this._dbContext.userRepository.saveUserEntity(foundUser); - await this._dbContext.userInvitationRepository.removeInvitationEntity(invitationEntity); - return generateGwtToken(savedUser, get2FaScope(savedUser, foundCompany)); - } -}