Skip to content

Commit

Permalink
feat : 닉네임 유효성 검사 파이프로 뺌
Browse files Browse the repository at this point in the history
* 유효성 검사는 파이프 로직에서 담당하는게 맞아, Dto를 생성한 후
  • Loading branch information
sk000801 committed Jan 8, 2024
1 parent 08854bf commit 03e58a4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 42 deletions.
2 changes: 0 additions & 2 deletions server/src/config/errorCode.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export enum ERROR_CODE {
'FAIL_GREEN_EYE_IMAGE_RECOGNITION' = 4012,
'BAD_IMAGE' = 4013,
'NOT_ADDED_MUSIC' = 4014,
'NOT_IN_RANGE_OF_NICKNAME_LENGTH' = 4015,
'INVALID_NICKNAME_PATTERN' = 4016,
'WRONG_TOKEN' = 4100,
'EXPIRED_TOKEN' = 4101,
}
14 changes: 8 additions & 6 deletions server/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
HttpCode,
Param,
UseGuards,
UsePipes,
Patch,
Body,
Query,
Logger,
Put,
ValidationPipe,
} from '@nestjs/common';
import { UserService } from './user.service';
import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum';
Expand All @@ -18,6 +20,7 @@ import { Music } from 'src/entity/music.entity';
import { CatchyException } from 'src/config/catchyException';
import { ERROR_CODE } from 'src/config/errorCode.enum';
import { User } from 'src/entity/user.entity';
import { UserUpdateDto } from 'src/dto/userUpdate.dto';

@Controller('users')
export class UserController {
Expand All @@ -26,21 +29,20 @@ export class UserController {

@Patch()
@UseGuards(AuthGuard())
@UsePipes(ValidationPipe)
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
async updateUserImage(
@Req() req,
@Body('image_url') image_url: string,
@Body('nickname') nickname: string | null,
@Body() userUpdateDto: UserUpdateDto,
): Promise<{ user_id: string }> {
this.logger.log(
`PATCH /users - nickname=${req.user.nickname}->${nickname}, image_url=${image_url}`,
`PATCH /users - nickname=${req.user.nickname}->${userUpdateDto.nickname}, image_url=${userUpdateDto.image_url}`,
);
const user_id = req.user.user_id;
return {
user_id: await this.userService.updateUserInformation(
user_id,
image_url,
nickname,
userUpdateDto,
),
};
}
Expand All @@ -51,7 +53,7 @@ export class UserController {
@Param('name') name: string,
): Promise<{ nickname: string }> {
this.logger.log(`GET /users/duplicate/${name}`);
if (await this.userService.isDuplicatedUserEmail(name)) {
if (await this.userService.isDuplicatedUserNickname(name)) {
throw new CatchyException(
'DUPLICATED_NICKNAME',
HTTP_STATUS_CODE.DUPLICATED_NICKNAME,
Expand Down
42 changes: 8 additions & 34 deletions server/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { CatchyException } from 'src/config/catchyException';
import { ERROR_CODE } from 'src/config/errorCode.enum';
import { Recent_Played } from 'src/entity/recent_played.entity';
import { UserUpdateDto } from './../dto/userUpdate.dto';

@Injectable()
export class UserService {
Expand All @@ -17,35 +18,7 @@ export class UserService {
private recentPlayedRepository: Repository<Recent_Played>,
) {}

validateNickname(inputNickname: string): void {
if (!inputNickname) {
return;
}

if (inputNickname.length < 2 || inputNickname.length > 10) {
this.logger.error(
`user.service - validateNickname : NOT_IN_RANGE_OF_NICKNAME_LENGTH`,
);
throw new CatchyException(
'NOT_IN_RANGE_OF_NICKNAME_LENGTH',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.NOT_IN_RANGE_OF_NICKNAME_LENGTH,
);
}

if (!inputNickname.match(/^[가-힣a-zA-Z0-9_.]+$/)) {
this.logger.error(
`user.service - validateNickname : INVALID_NICKNAME_PATTERN`,
);
throw new CatchyException(
'INVALID_NICKNAME_PATTERN',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.INVALID_NICKNAME_PATTERN,
);
}
}

async isDuplicatedUserEmail(userNickname: string): Promise<boolean> {
async isDuplicatedUserNickname(userNickname: string): Promise<boolean> {
try {
const user = await this.userRepository.findOneBy({
nickname: userNickname,
Expand Down Expand Up @@ -86,8 +59,7 @@ export class UserService {

async updateUserInformation(
user_id: string,
image_url: string,
nickname: string | null,
userUpdateDto: UserUpdateDto,
): Promise<string> {
try {
const targetUser: User = await this.userRepository.findOne({
Expand All @@ -103,18 +75,20 @@ export class UserService {
);
}

if (await this.isDuplicatedUserEmail(nickname)) {
const nickname = userUpdateDto.nickname;
const image_url = userUpdateDto.image_url;

if (await this.isDuplicatedUserNickname(nickname)) {
throw new CatchyException(
'DUPLICATED_NICKNAME',
HTTP_STATUS_CODE.DUPLICATED_NICKNAME,
ERROR_CODE.DUPLICATED_NICKNAME,
);
}

this.validateNickname(nickname);

targetUser.photo = image_url;
targetUser.nickname = nickname ? nickname : targetUser.nickname;

const savedUser: User = await this.userRepository.save(targetUser);
return savedUser.user_id;
} catch (err) {
Expand Down

0 comments on commit 03e58a4

Please sign in to comment.