From 0b557ccf79a24c6b0701e6631dc3cd09c23d956d Mon Sep 17 00:00:00 2001 From: minchodang Date: Sun, 8 Sep 2024 00:53:24 +0900 Subject: [PATCH] Fix: create-user-logic --- src/modules/auth/auth.service.ts | 5 +- src/modules/user/user.repository.ts | 9 +++- src/modules/user/user.service.ts | 76 ++++++++++++++++------------- 3 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 71dd18c..f5b6dff 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -33,6 +33,7 @@ export class AuthService { userType: UserType, ): Promise { //NOTE: email & userType까지 동일해야 동일 유저 + console.log(email, password, userType, 'validateUser'); const user = await this.authRepository.findOneByEmailAndUserType( email, userType, @@ -97,9 +98,11 @@ export class AuthService { userType: UserType, ): Promise { const { email, password } = socialLoginDto; + console.log(email, password, userType, 'socialLoginDto'); const user = await this.validateUser(email, password, userType); + console.log(user, 'user'); if (user) { //1.기존 가입 유저라면, token create return await this.createToken(user); @@ -110,7 +113,7 @@ export class AuthService { { email, password }, userType, ); - + console.log(newUser, 'newUser'); return await this.createToken(newUser); } diff --git a/src/modules/user/user.repository.ts b/src/modules/user/user.repository.ts index 30746fe..6f48281 100644 --- a/src/modules/user/user.repository.ts +++ b/src/modules/user/user.repository.ts @@ -1,4 +1,8 @@ -import { GetCommandOutput, QueryCommandOutput, UpdateCommandOutput } from '@aws-sdk/lib-dynamodb'; +import { + GetCommandOutput, + QueryCommandOutput, + UpdateCommandOutput, +} from '@aws-sdk/lib-dynamodb'; import { Inject, Injectable } from '@nestjs/common'; import * as bcrypt from 'bcrypt'; import { DatabaseError } from 'src/core/errors/database-error'; @@ -63,7 +67,7 @@ export class UserRepository { } } - async create(userInfo: UserInfo): Promise { + async create(userInfo: UserInfo, userType: UserType): Promise { let id = userInfo.id; if (!id) { id = uuidv4(); @@ -75,6 +79,7 @@ export class UserRepository { PK: id, SK: `UserInfo#${id}`, ...userInfo, + userType, password: hashedPassword, createdAt, }; diff --git a/src/modules/user/user.service.ts b/src/modules/user/user.service.ts index e044697..1ecc433 100644 --- a/src/modules/user/user.service.ts +++ b/src/modules/user/user.service.ts @@ -1,6 +1,8 @@ import { BadRequestException, - ConflictException, Injectable, NotFoundException + ConflictException, + Injectable, + NotFoundException, } from '@nestjs/common'; import * as bcrypt from 'bcrypt'; import { generateRandomNickName } from 'src/core/utils/generate-random-nickname.util'; @@ -39,6 +41,7 @@ export class UserService { userInfo: CreateUserInfoRequestDto, userType: UserType, ): Promise { + console.log(userInfo, userType, 'userInfo, userType'); if (userType === UserType.NORMAL) { const emailVerification = await this.authRepository.findEmailVerificationByEmail(userInfo.email); @@ -61,11 +64,11 @@ export class UserService { userInfo['nickname'] = nickName; } - const item = await this.usersRepository.create(userInfo); + const item = await this.usersRepository.create(userInfo, userType); const responseItem = { id: item.PK, - userType: userType, // Add this line + userType, ...item, }; delete responseItem.PK; @@ -74,46 +77,49 @@ export class UserService { return responseItem; } -async hashPassword(password: string): Promise { - const saltOrRounds = 10; - const hashedPassword = await bcrypt.hash(password, saltOrRounds); - return hashedPassword; -} + async hashPassword(password: string): Promise { + const saltOrRounds = 10; + const hashedPassword = await bcrypt.hash(password, saltOrRounds); + return hashedPassword; + } -async update(id: string, userInfo: UpdateUserInfoRequestDto): Promise> { - const { email, password, nickname } = userInfo; + async update( + id: string, + userInfo: UpdateUserInfoRequestDto, + ): Promise> { + const { email, password, nickname } = userInfo; - if (!email && !password && !nickname) { - throw new BadRequestException( - 'At least one of email, password, or nickname is required.', - ); - } + if (!email && !password && !nickname) { + throw new BadRequestException( + 'At least one of email, password, or nickname is required.', + ); + } - const updateData: Partial = {}; + const updateData: Partial = {}; - if (email) { - updateData.email = email; - } + if (email) { + updateData.email = email; + } - if (password) { - const hashedPassword = await this.hashPassword(password); - updateData.password = hashedPassword; - } + if (password) { + const hashedPassword = await this.hashPassword(password); + updateData.password = hashedPassword; + } - if (nickname) { - updateData.nickname = nickname; - } + if (nickname) { + updateData.nickname = nickname; + } - const user = await this.usersRepository.update(id, updateData); - const responseItem: Omit = { - id: user.id, - email: user.email, - nickname: user.nickname, - // 다른 필드들... - }; + const user = await this.usersRepository.update(id, updateData); + const responseItem: Omit = { + id: user.id, + email: user.email, + nickname: user.nickname, + // 다른 필드들... + }; - return responseItem; -} + return responseItem; + } async getUserTestList(id: string, query: any): Promise { const limit = query.limit;