Skip to content

Commit

Permalink
Merge pull request #68 from Brints/listings
Browse files Browse the repository at this point in the history
users: implementing the delete user profile.
  • Loading branch information
aniebietafia authored Feb 2, 2025
2 parents e327cc8 + 12dbbf8 commit d129ead
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
3 changes: 2 additions & 1 deletion brints-estate-api/src/users/dto/verify-email.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { IsEmail, IsNotEmpty, IsString } from 'class-validator';

export class VerifyEmailDto {
@ApiProperty({
example: 'gsteu266dinjd8h4gd6784nuuv',
example:
'f4a64343757b6f6f75ef240ab68ed92f48158b9ade29a2e52b7824cb4fb16d4b85a8d2a9b1ba7829',
type: String,
required: true,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<User>,

@InjectRepository(UserAuth)
private readonly userAuthRepository: Repository<UserAuth>,
) {}

public async deleteUser(
activeUser: IActiveUser,
userId: string,
): Promise<null> {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion brints-estate-api/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit d129ead

Please sign in to comment.