import { ValidationPipe , ParseUUIDPipe , Controller , UseGuards , Param , Post , Body , Get , Put } from '@nestjs/common' ; import { ApiPaginatedResponse , PaginationParams , PaginationRequest , PaginationResponseDto } from '@libs/pagination' ; import { ChangePasswordRequestDto , CreateUserRequestDto , UpdateUserRequestDto , UserResponseDto } from './dtos' ; import { ApiConflictResponse , ApiBearerAuth , ApiOperation , ApiQuery , ApiTags } from '@nestjs/swagger' ; import { CurrentUser , Permissions , TOKEN_NAME } from '@auth' ; import { ApiGlobalResponse } from '@common/decorators' ; import { UsersService } from './users.service' ; import { UserEntity } from './user.entity' ; @ ApiTags ( 'Users' ) @ ApiBearerAuth ( TOKEN_NAME ) @ Controller ( { path : 'access/users' , version : '1' , } ) export class UsersController { constructor ( private usersService ) { } @ ApiOperation ( { description : 'Get a paginated user list' } ) @ ApiPaginatedResponse ( UserResponseDto ) @ ApiQuery ( { name : 'search' , type : 'string' , required : false , example : 'admin' , } ) @ Permissions ( 'admin.access.users.read' , 'admin.access.users.create' , 'admin.access.users.update' ) @ Get ( ) public getUsers ( @ PaginationParams ( ) pagination ) { return this . usersService . getUsers ( pagination ) ; } @ ApiOperation ( { description : 'Get user by id' } ) @ ApiGlobalResponse ( UserResponseDto ) @ Permissions ( 'admin.access.users.read' , 'admin.access.users.create' , 'admin.access.users.update' ) @ Get ( '/:id' ) public getUserById ( @ Param ( 'id' , ParseUUIDPipe ) id ) { return this . usersService . getUserById ( id ) ; } @ ApiOperation ( { description : 'Create new user' } ) @ ApiGlobalResponse ( UserResponseDto ) @ ApiConflictResponse ( { description : 'User already exists' } ) @ ApiGlobalResponse ( UserResponseDto ) @ Permissions ( 'admin.access.users.create' ) @ Post ( ) public createUser ( @ Body ( ValidationPipe ) UserDto ) { return this . usersService . createUser ( UserDto ) ; } @ ApiOperation ( { description : 'Update user by id' } ) @ ApiGlobalResponse ( UserResponseDto ) @ ApiConflictResponse ( { description : 'User already exists' } ) @ Permissions ( 'admin.access.users.update' ) @ Put ( '/:id' ) public updateUser ( @ Param ( 'id' , ParseUUIDPipe ) id , @ Body ( ValidationPipe ) UserDto ) { return this . usersService . updateUser ( id , UserDto ) ; } @ ApiOperation ( { description : 'Change user password' } ) @ ApiGlobalResponse ( UserResponseDto ) @ Post ( '/change/password' ) changePassword ( @ Body ( ValidationPipe ) changePassword , @ CurrentUser ( ) user ) { return this . usersService . changePassword ( changePassword , user . id ) ; } }