From 12dbbf8a25f9d6c606b1608e1d435e71a4dd7bab Mon Sep 17 00:00:00 2001 From: aniebietafia Date: Sun, 2 Feb 2025 21:51:19 +0100 Subject: [PATCH] users: implementing the delete user profile. --- .../src/users/dto/verify-email.dto.ts | 3 +- .../providers/delete-user-profile.provider.ts | 28 +++++++++++++++++-- .../providers/get-user-profile.provider.ts | 4 +-- .../src/users/users.controller.ts | 2 +- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/brints-estate-api/src/users/dto/verify-email.dto.ts b/brints-estate-api/src/users/dto/verify-email.dto.ts index d7c592b..ea8bd10 100644 --- a/brints-estate-api/src/users/dto/verify-email.dto.ts +++ b/brints-estate-api/src/users/dto/verify-email.dto.ts @@ -3,7 +3,8 @@ import { IsEmail, IsNotEmpty, IsString } from 'class-validator'; export class VerifyEmailDto { @ApiProperty({ - example: 'gsteu266dinjd8h4gd6784nuuv', + example: + 'f4a64343757b6f6f75ef240ab68ed92f48158b9ade29a2e52b7824cb4fb16d4b85a8d2a9b1ba7829', type: String, required: true, }) diff --git a/brints-estate-api/src/users/providers/delete-user-profile.provider.ts b/brints-estate-api/src/users/providers/delete-user-profile.provider.ts index cf64745..26cb0df 100644 --- a/brints-estate-api/src/users/providers/delete-user-profile.provider.ts +++ b/brints-estate-api/src/users/providers/delete-user-profile.provider.ts @@ -5,30 +5,52 @@ import { Repository } from 'typeorm'; import { IActiveUser } from 'src/auth/interfaces/active-user.interface'; import { CustomException } from 'src/exceptions/custom.exception'; import { UserRole } from 'src/enums/user-role.enum'; +import { UserAuth } from '../entities/userAuth.entity'; @Injectable() export class DeleteUserProfileProvider { constructor( @InjectRepository(User) private readonly userRepository: Repository, + + @InjectRepository(UserAuth) + private readonly userAuthRepository: Repository, ) {} public async deleteUser( activeUser: IActiveUser, userId: string, ): Promise { - const user = await this.userRepository.findOne({ where: { id: userId } }); + const user = await this.userRepository.findOne({ + where: { id: userId }, + relations: { user_auth: true }, + }); if (!user) throw new CustomException(HttpStatus.NOT_FOUND, 'User does not exist.'); - if (activeUser.role !== UserRole.SUPER_ADMIN || userId !== activeUser.sub) + const userAuth = await this.userAuthRepository.findOne({ + where: { id: user.user_auth.id }, + }); + + if (!userAuth) + throw new CustomException( + HttpStatus.NOT_FOUND, + 'Server error. The resource does not exist.', + ); + + if ( + activeUser.role !== UserRole.SUPER_ADMIN && + user.id !== activeUser.sub + ) { throw new CustomException( HttpStatus.FORBIDDEN, 'You do not have the authority to delete this account.', ); + } - await this.userRepository.delete({ id: userId }); + await this.userAuthRepository.remove(userAuth); + await this.userRepository.remove(user); return null; } diff --git a/brints-estate-api/src/users/providers/get-user-profile.provider.ts b/brints-estate-api/src/users/providers/get-user-profile.provider.ts index 375243c..1e06624 100644 --- a/brints-estate-api/src/users/providers/get-user-profile.provider.ts +++ b/brints-estate-api/src/users/providers/get-user-profile.provider.ts @@ -26,8 +26,8 @@ export class GetUserProfileProvider { throw new CustomException(HttpStatus.NOT_FOUND, 'User does not exist'); if ( - loggedInUser.role !== UserRole.SUPER_ADMIN && - userId !== loggedInUser.sub + userId !== loggedInUser.sub && + loggedInUser.role !== UserRole.SUPER_ADMIN ) throw new CustomException( HttpStatus.FORBIDDEN, diff --git a/brints-estate-api/src/users/users.controller.ts b/brints-estate-api/src/users/users.controller.ts index 71fcc79..9189307 100644 --- a/brints-estate-api/src/users/users.controller.ts +++ b/brints-estate-api/src/users/users.controller.ts @@ -199,7 +199,7 @@ export class UsersController { @UseInterceptors(ClassSerializerInterceptor) @UseFilters(HttpExceptionFilter) public async getUser( - @Param('id') userId: string, + @Param('userId') userId: string, @ActiveUser() loggedInUser: IActiveUser, ) { const payload = await this.usersService.getUserProfile(