Skip to content

Commit

Permalink
refactor: Update UserController to use @currentuser decorator for use…
Browse files Browse the repository at this point in the history
…r identification
  • Loading branch information
PleBea committed Jul 21, 2024
1 parent c94a170 commit d2cb404
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/modules/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { User } from '@prisma/client';

import { Body, Controller, Delete, Get, Param, Patch, Put, Req, UseGuards } from '@nestjs/common';
import { Body, Controller, Delete, Get, Param, Patch, Put, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiBody, ApiOkResponse, ApiOperation, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';

Expand All @@ -23,8 +23,8 @@ export class UserController {
@ApiOperation({ summary: 'Get user information' })
@ApiOkResponse({ description: 'User information', type: UserResponseDTO })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async getUser(@Req() req: any): Promise<UserResponseDTO> {
return await this.userService.findUserById(req.user.id);
async getUser(@CurrentUser() user: User): Promise<UserResponseDTO> {
return await this.userService.findUserById(user.id);
}

@Patch('/')
Expand Down Expand Up @@ -54,8 +54,8 @@ export class UserController {
@ApiOperation({ summary: 'Get user information by id' })
@ApiOkResponse({ description: 'User information', type: UserResponseDTO })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async getUserById(@Param('userId') userId: string): Promise<UserResponseDTO> {
return await this.userService.findUserById(userId);
async getUserById(@CurrentUser() user: User, @Param('userId') userId: string): Promise<UserResponseDTO> {
return await this.userService.findUserById(userId, user.id);
}

@Put('/fcm')
Expand Down
17 changes: 15 additions & 2 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ export class UserService {
private readonly s3: S3Service,
) {}

async findUserById(id: string) {
const user = await this.prisma.user.findUnique({ where: { id } });
async findUserById(id: string, userId?: string) {
const user = await this.prisma.user.findUnique({
where: {
id,
...(userId
? {
plan: {
users: {
some: { id: userId },
},
},
}
: {}),
},
});
if (!user) throw new HttpException('User not found', HttpStatus.NOT_FOUND);

return user;
Expand Down

0 comments on commit d2cb404

Please sign in to comment.