2022-12-05 10:02:19 +01:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
2024-11-14 17:39:06 +01:00
|
|
|
HttpCode,
|
2022-12-05 10:02:19 +01:00
|
|
|
Param,
|
|
|
|
|
Patch,
|
|
|
|
|
Post,
|
2023-03-04 23:40:02 +01:00
|
|
|
Res,
|
2022-12-05 10:02:19 +01:00
|
|
|
UseGuards,
|
|
|
|
|
} from "@nestjs/common";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { User } from "@prisma/client";
|
2023-03-04 23:40:02 +01:00
|
|
|
import { Response } from "express";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { GetUser } from "src/auth/decorator/getUser.decorator";
|
2022-12-05 10:02:19 +01:00
|
|
|
import { AdministratorGuard } from "src/auth/guard/isAdmin.guard";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { JwtGuard } from "src/auth/guard/jwt.guard";
|
2024-11-14 17:39:06 +01:00
|
|
|
import { ConfigService } from "../config/config.service";
|
2022-12-05 15:53:24 +01:00
|
|
|
import { CreateUserDTO } from "./dto/createUser.dto";
|
|
|
|
|
import { UpdateOwnUserDTO } from "./dto/updateOwnUser.dto";
|
2022-12-05 10:02:19 +01:00
|
|
|
import { UpdateUserDto } from "./dto/updateUser.dto";
|
2022-10-10 17:58:42 +02:00
|
|
|
import { UserDTO } from "./dto/user.dto";
|
2022-12-05 10:02:19 +01:00
|
|
|
import { UserSevice } from "./user.service";
|
2022-10-09 22:30:32 +02:00
|
|
|
|
|
|
|
|
@Controller("users")
|
|
|
|
|
export class UserController {
|
2024-09-24 12:21:41 +02:00
|
|
|
constructor(
|
|
|
|
|
private userService: UserSevice,
|
2024-11-14 17:39:06 +01:00
|
|
|
private config: ConfigService
|
2024-09-24 12:21:41 +02:00
|
|
|
) {}
|
2022-12-05 10:02:19 +01:00
|
|
|
|
|
|
|
|
// Own user operations
|
2022-10-09 22:30:32 +02:00
|
|
|
@Get("me")
|
|
|
|
|
@UseGuards(JwtGuard)
|
2024-03-25 19:12:27 +01:00
|
|
|
async getCurrentUser(@GetUser() user?: User) {
|
|
|
|
|
if (!user) return null;
|
2023-10-22 22:09:53 +08:00
|
|
|
const userDTO = new UserDTO().from(user);
|
|
|
|
|
userDTO.hasPassword = !!user.password;
|
|
|
|
|
return userDTO;
|
2022-10-09 22:30:32 +02:00
|
|
|
}
|
2022-12-05 10:02:19 +01:00
|
|
|
|
|
|
|
|
@Patch("me")
|
|
|
|
|
@UseGuards(JwtGuard)
|
2022-12-05 15:53:24 +01:00
|
|
|
async updateCurrentUser(
|
|
|
|
|
@GetUser() user: User,
|
2024-11-14 17:39:06 +01:00
|
|
|
@Body() data: UpdateOwnUserDTO
|
2022-12-05 15:53:24 +01:00
|
|
|
) {
|
2022-12-05 10:02:19 +01:00
|
|
|
return new UserDTO().from(await this.userService.update(user.id, data));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("me")
|
2024-11-14 17:39:06 +01:00
|
|
|
@HttpCode(204)
|
2022-12-05 10:02:19 +01:00
|
|
|
@UseGuards(JwtGuard)
|
2023-03-04 23:40:02 +01:00
|
|
|
async deleteCurrentUser(
|
|
|
|
|
@GetUser() user: User,
|
2024-11-14 17:39:06 +01:00
|
|
|
@Res({ passthrough: true }) response: Response
|
2023-03-04 23:40:02 +01:00
|
|
|
) {
|
2024-11-14 17:39:06 +01:00
|
|
|
await this.userService.delete(user.id);
|
|
|
|
|
|
2024-11-14 17:31:17 +01:00
|
|
|
const isSecure = this.config.get("general.secureCookies");
|
2024-09-24 12:21:41 +02:00
|
|
|
|
|
|
|
|
response.cookie("access_token", "accessToken", {
|
|
|
|
|
maxAge: -1,
|
|
|
|
|
secure: isSecure,
|
|
|
|
|
});
|
2023-03-04 23:40:02 +01:00
|
|
|
response.cookie("refresh_token", "", {
|
|
|
|
|
path: "/api/auth/token",
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
maxAge: -1,
|
2024-09-24 12:21:41 +02:00
|
|
|
secure: isSecure,
|
2023-03-04 23:40:02 +01:00
|
|
|
});
|
2022-12-05 10:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Global user operations
|
|
|
|
|
@Get()
|
|
|
|
|
@UseGuards(JwtGuard, AdministratorGuard)
|
|
|
|
|
async list() {
|
|
|
|
|
return new UserDTO().fromList(await this.userService.list());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
|
|
|
|
@UseGuards(JwtGuard, AdministratorGuard)
|
2022-12-05 15:53:24 +01:00
|
|
|
async create(@Body() user: CreateUserDTO) {
|
2022-12-05 10:02:19 +01:00
|
|
|
return new UserDTO().from(await this.userService.create(user));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch(":id")
|
|
|
|
|
@UseGuards(JwtGuard, AdministratorGuard)
|
|
|
|
|
async update(@Param("id") id: string, @Body() user: UpdateUserDto) {
|
|
|
|
|
return new UserDTO().from(await this.userService.update(id, user));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete(":id")
|
|
|
|
|
@UseGuards(JwtGuard, AdministratorGuard)
|
2022-12-05 15:53:24 +01:00
|
|
|
async delete(@Param("id") id: string) {
|
2022-12-05 10:02:19 +01:00
|
|
|
return new UserDTO().from(await this.userService.delete(id));
|
|
|
|
|
}
|
2022-10-09 22:30:32 +02:00
|
|
|
}
|