57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { Controller, Get, Req, UseGuards, Patch, Body, ValidationPipe } from '@nestjs/common';
|
|
import { ApiTags, ApiBearerAuth, ApiOperation } 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';
|
|
|
|
@ApiTags('User')
|
|
@Controller('user')
|
|
export class UserController {
|
|
constructor(private readonly userService: UserService) {}
|
|
|
|
@UseGuards(AuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiOperation({ summary: 'Get the current authenticated user profile' })
|
|
@Get('/me')
|
|
async getUser(@Req() req: AuthRequest) {
|
|
const userId = req.userId;
|
|
const restId = req.restId;
|
|
const user = await this.userService.findById(userId);
|
|
return {
|
|
message: `GET request: Retrieving profile for authenticated user `,
|
|
user,
|
|
userId,
|
|
restId,
|
|
};
|
|
}
|
|
// 2. Update User Specification (PATCH /user)
|
|
@UseGuards(AuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiOperation({ summary: 'Update the authenticated user profile' })
|
|
@Patch('/')
|
|
async updateUser(
|
|
@Req() req: AuthRequest,
|
|
@Body(new ValidationPipe({ transform: true, whitelist: true })) dto: UpdateUserDto,
|
|
) {
|
|
const userId = req.userId;
|
|
const user = await this.userService.updateUser(userId, dto);
|
|
return {
|
|
message: `PATCH request: Updating profile 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);
|
|
// }
|
|
}
|