user
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { Controller, Get, Patch, Body, Req, UseGuards, Query, ValidationPipe } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation, ApiBody } from '@nestjs/swagger';
|
||||
import { AuthGuard, type AuthRequest } from 'src/modules/auth/guards/auth.guard';
|
||||
import { UserService } from './user.service';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { FindUsersDto } from './dto/find-user.dto';
|
||||
import { AdminAuthGuard } from 'src/modules/auth/guards/auth.admin.guard';
|
||||
|
||||
@ApiTags('User')
|
||||
@Controller('user')
|
||||
export class UserController {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Get the current authenticated user profile' })
|
||||
@Get('/')
|
||||
async getUser(@Req() req: AuthRequest) {
|
||||
const userId = req.user.sub;
|
||||
const user = await this.userService.findById(+userId);
|
||||
return {
|
||||
message: `GET request: Retrieving profile for authenticated user `,
|
||||
user,
|
||||
};
|
||||
}
|
||||
|
||||
// 2. Update User Specification (PATCH /user/spec)
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Update the user specification' })
|
||||
@ApiBody({ type: UpdateUserDto })
|
||||
@Patch('/')
|
||||
async updateUserSpec(@Req() req: AuthRequest, @Body() dto: UpdateUserDto) {
|
||||
const userId = req.user.sub;
|
||||
const user = await this.userService.updateUser(+userId, dto);
|
||||
return {
|
||||
message: `PATCH request: Updating specification for user ${userId} `,
|
||||
userId,
|
||||
user,
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Users : Admin Route' })
|
||||
@Get('/admin/users')
|
||||
async findAll(
|
||||
// Use ValidationPipe to validate and transform the DTO
|
||||
@Query(new ValidationPipe({ transform: true, whitelist: true }))
|
||||
query: FindUsersDto,
|
||||
) {
|
||||
return this.userService.findAll(query);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user