2022-06-11 23:12:06 +02:00
|
|
|
import {
|
|
|
|
Controller,
|
|
|
|
Get,
|
|
|
|
Post,
|
2022-11-07 22:53:47 +01:00
|
|
|
Delete,
|
2022-06-11 23:12:06 +02:00
|
|
|
Body,
|
|
|
|
Param,
|
|
|
|
ValidationPipe,
|
|
|
|
Put,
|
|
|
|
Query,
|
|
|
|
UseInterceptors,
|
|
|
|
UploadedFile,
|
|
|
|
Response,
|
2022-07-11 04:41:45 +02:00
|
|
|
ParseBoolPipe,
|
2022-12-23 21:08:50 +01:00
|
|
|
StreamableFile,
|
2022-12-27 17:13:44 +01:00
|
|
|
Header,
|
2022-06-11 23:12:06 +02:00
|
|
|
} from '@nestjs/common';
|
2023-01-12 03:34:36 +01:00
|
|
|
import { UserService } from '@app/domain';
|
|
|
|
import { Authenticated } from '../decorators/authenticated.decorator';
|
|
|
|
import { AuthUserDto, GetAuthUser } from '../decorators/auth-user.decorator';
|
|
|
|
import { CreateUserDto } from '@app/domain';
|
|
|
|
import { UpdateUserDto } from '@app/domain';
|
2022-05-28 05:15:35 +02:00
|
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
2023-01-12 03:34:36 +01:00
|
|
|
import { profileImageUploadOption } from '../config/profile-image-upload.config';
|
2022-09-22 22:58:17 +02:00
|
|
|
import { Response as Res } from 'express';
|
2023-02-24 17:01:10 +01:00
|
|
|
import { ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger';
|
2023-01-12 03:34:36 +01:00
|
|
|
import { UserResponseDto } from '@app/domain';
|
|
|
|
import { UserCountResponseDto } from '@app/domain';
|
|
|
|
import { CreateProfileImageDto } from '@app/domain';
|
|
|
|
import { CreateProfileImageResponseDto } from '@app/domain';
|
|
|
|
import { UserCountDto } from '@app/domain';
|
2022-02-03 17:06:44 +01:00
|
|
|
|
2022-07-09 04:26:50 +02:00
|
|
|
@ApiTags('User')
|
2022-02-03 17:06:44 +01:00
|
|
|
@Controller('user')
|
|
|
|
export class UserController {
|
2022-06-11 23:12:06 +02:00
|
|
|
constructor(private readonly userService: UserService) {}
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2022-10-28 20:57:52 +02:00
|
|
|
@Authenticated()
|
2022-04-24 04:08:45 +02:00
|
|
|
@Get()
|
2022-07-11 04:41:45 +02:00
|
|
|
async getAllUsers(
|
|
|
|
@GetAuthUser() authUser: AuthUserDto,
|
|
|
|
@Query('isAll', ParseBoolPipe) isAll: boolean,
|
|
|
|
): Promise<UserResponseDto[]> {
|
2022-05-21 09:23:55 +02:00
|
|
|
return await this.userService.getAllUsers(authUser, isAll);
|
|
|
|
}
|
|
|
|
|
2022-07-17 22:09:26 +02:00
|
|
|
@Get('/info/:userId')
|
2022-07-17 06:52:00 +02:00
|
|
|
async getUserById(@Param('userId') userId: string): Promise<UserResponseDto> {
|
|
|
|
return await this.userService.getUserById(userId);
|
|
|
|
}
|
|
|
|
|
2022-10-28 20:57:52 +02:00
|
|
|
@Authenticated()
|
2022-06-27 22:13:07 +02:00
|
|
|
@Get('me')
|
2022-07-09 04:26:50 +02:00
|
|
|
async getMyUserInfo(@GetAuthUser() authUser: AuthUserDto): Promise<UserResponseDto> {
|
2022-06-27 22:13:07 +02:00
|
|
|
return await this.userService.getUserInfo(authUser);
|
|
|
|
}
|
|
|
|
|
2022-10-28 20:57:52 +02:00
|
|
|
@Authenticated({ admin: true })
|
2022-05-21 09:23:55 +02:00
|
|
|
@Post()
|
2022-08-16 02:11:08 +02:00
|
|
|
async createUser(
|
|
|
|
@Body(new ValidationPipe({ transform: true })) createUserDto: CreateUserDto,
|
|
|
|
): Promise<UserResponseDto> {
|
2022-05-21 09:23:55 +02:00
|
|
|
return await this.userService.createUser(createUserDto);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Get('/count')
|
2022-12-09 21:53:11 +01:00
|
|
|
async getUserCount(@Query(new ValidationPipe({ transform: true })) dto: UserCountDto): Promise<UserCountResponseDto> {
|
|
|
|
return await this.userService.getUserCount(dto);
|
2022-05-21 09:23:55 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 22:53:47 +01:00
|
|
|
@Authenticated({ admin: true })
|
|
|
|
@Delete('/:userId')
|
|
|
|
async deleteUser(@GetAuthUser() authUser: AuthUserDto, @Param('userId') userId: string): Promise<UserResponseDto> {
|
|
|
|
return await this.userService.deleteUser(authUser, userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Authenticated({ admin: true })
|
|
|
|
@Post('/:userId/restore')
|
|
|
|
async restoreUser(@GetAuthUser() authUser: AuthUserDto, @Param('userId') userId: string): Promise<UserResponseDto> {
|
|
|
|
return await this.userService.restoreUser(authUser, userId);
|
|
|
|
}
|
|
|
|
|
2022-10-28 20:57:52 +02:00
|
|
|
@Authenticated()
|
2022-05-21 09:23:55 +02:00
|
|
|
@Put()
|
2022-09-18 16:27:06 +02:00
|
|
|
async updateUser(
|
|
|
|
@GetAuthUser() authUser: AuthUserDto,
|
|
|
|
@Body(ValidationPipe) updateUserDto: UpdateUserDto,
|
|
|
|
): Promise<UserResponseDto> {
|
|
|
|
return await this.userService.updateUser(authUser, updateUserDto);
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
2022-05-28 05:15:35 +02:00
|
|
|
|
|
|
|
@UseInterceptors(FileInterceptor('file', profileImageUploadOption))
|
2022-10-28 20:57:52 +02:00
|
|
|
@Authenticated()
|
2022-07-09 04:26:50 +02:00
|
|
|
@ApiConsumes('multipart/form-data')
|
|
|
|
@ApiBody({
|
2022-07-13 14:23:48 +02:00
|
|
|
description: 'A new avatar for the user',
|
2022-07-09 04:26:50 +02:00
|
|
|
type: CreateProfileImageDto,
|
|
|
|
})
|
2022-05-28 05:15:35 +02:00
|
|
|
@Post('/profile-image')
|
2022-07-09 04:26:50 +02:00
|
|
|
async createProfileImage(
|
|
|
|
@GetAuthUser() authUser: AuthUserDto,
|
|
|
|
@UploadedFile() fileInfo: Express.Multer.File,
|
|
|
|
): Promise<CreateProfileImageResponseDto> {
|
2022-05-28 05:15:35 +02:00
|
|
|
return await this.userService.createProfileImage(authUser, fileInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Get('/profile-image/:userId')
|
2023-02-20 06:29:06 +01:00
|
|
|
@Header('Cache-Control', 'max-age=600')
|
2022-07-11 04:41:45 +02:00
|
|
|
async getProfileImage(@Param('userId') userId: string, @Response({ passthrough: true }) res: Res): Promise<any> {
|
2022-12-23 21:08:50 +01:00
|
|
|
const readableStream = await this.userService.getUserProfileImage(userId);
|
|
|
|
res.set({
|
|
|
|
'Content-Type': 'image/jpeg',
|
|
|
|
});
|
|
|
|
return new StreamableFile(readableStream);
|
2022-05-28 05:15:35 +02:00
|
|
|
}
|
2022-02-03 17:06:44 +01:00
|
|
|
}
|