feat: added delete user route

This commit is contained in:
Hackntosh 2024-02-03 16:28:20 +00:00
parent 2f4dc87af5
commit 2f33a05eab
3 changed files with 21 additions and 12 deletions

View file

View file

@ -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)
}
}

View file

@ -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')
}
}
}