From 2f33a05eabae19faf1ed55c5234ab46388e1855d Mon Sep 17 00:00:00 2001 From: CookieDasora Date: Sat, 3 Feb 2024 16:28:20 +0000 Subject: [PATCH] feat: added delete user route --- src/users/dto/delete-user.dto.ts | 0 src/users/users.controller.ts | 13 +++++++------ src/users/users.service.ts | 20 ++++++++++++++------ 3 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 src/users/dto/delete-user.dto.ts diff --git a/src/users/dto/delete-user.dto.ts b/src/users/dto/delete-user.dto.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index eabc00c..97702b8 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -31,7 +31,6 @@ import { UpdateEmailDTO } from "./dto/update-email.dto"; import { UpdateNameDTO } from "./dto/update-name.dto"; import { UpdatePasswordDTO } from "./dto/update-password.dto"; import UploadImageSchema from "./schemas/upload-image.schema"; -import { User } from "./types/user.type"; import { UserService } from "./users.service"; @ApiTags("Users") @@ -78,14 +77,14 @@ export class UserController { }) @ApiBearerAuth("JWT") updateName(@Body() { displayName, username }: UpdateNameDTO, @Request() req) { - return this.userService.updateName(req.user as User, username, displayName); + return this.userService.updateName(req.user.id, username, displayName); } @Patch("/email") @ApiOperation({ summary: "Updates the email of a logged user" }) @ApiBearerAuth("JWT") updateEmail(@Body() body: UpdateEmailDTO, @Request() req) { - return this.userService.updateEmail(req.user as User, body.email); + return this.userService.updateEmail(req.user.id, body.email); } @Patch("/password") @@ -96,7 +95,7 @@ export class UserController { @Request() req, ) { return this.userService.updatePassword( - req.user as User, + req.user.id, old_password, new_password, ); @@ -118,12 +117,14 @@ export class UserController { image: File, @Request() req, ) { - return this.userService.uploadImage(req.user, image); + return this.userService.uploadImage(req.user.id, image); } // DELETE @Delete() @ApiOperation({ summary: "Deletes the account of a logged user" }) @ApiBearerAuth("JWT") - remove() {} + delete(@Request() req) { + return this.userService.delete(req.user.id) + } } diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 0656c4c..8ae90c1 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -169,12 +169,10 @@ export class UserService { } async updatePassword( - loggedUser: User, + id: string, old_password: string, new_password: string, ): Promise<{ message: string }> { - const id = loggedUser.id; - const user = await this.prisma.user.findFirst({ where: { id }, }); @@ -200,15 +198,15 @@ export class UserService { return { message: "Password updated successfully" }; } - async uploadImage(authenticatedUser: User, image: File) { + async uploadImage(id: string, image: File) { const url = await this.s3.uploadImageToMinio( - authenticatedUser.id, + id, image.buffer, ); return await this.prisma.user.update({ where: { - id: authenticatedUser.id, + id, }, data: { profileImage: url, @@ -218,4 +216,14 @@ export class UserService { }, }); } + + async delete(id: string) { + // TODO: Add validation for safety (like e-mail confirmation or password) + try { + await this.prisma.user.deleteMany({where: {id}});; + return { message: "User deleted"} + } catch (e) { + throw new BadRequestException('Error while trying to delete user') + } + } }