Skip to content

Commit

Permalink
Fix: create-user-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
minchodang committed Sep 7, 2024
1 parent 352b850 commit 0b557cc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 38 deletions.
5 changes: 4 additions & 1 deletion src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class AuthService {
userType: UserType,
): Promise<any> {
//NOTE: email & userType까지 동일해야 동일 유저
console.log(email, password, userType, 'validateUser');
const user = await this.authRepository.findOneByEmailAndUserType(
email,
userType,
Expand Down Expand Up @@ -97,9 +98,11 @@ export class AuthService {
userType: UserType,
): Promise<CreateTokenResponseDto> {
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);
Expand All @@ -110,7 +113,7 @@ export class AuthService {
{ email, password },
userType,
);

console.log(newUser, 'newUser');
return await this.createToken(newUser);
}

Expand Down
9 changes: 7 additions & 2 deletions src/modules/user/user.repository.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -63,7 +67,7 @@ export class UserRepository {
}
}

async create(userInfo: UserInfo): Promise<UserInfo> {
async create(userInfo: UserInfo, userType: UserType): Promise<UserInfo> {
let id = userInfo.id;
if (!id) {
id = uuidv4();
Expand All @@ -75,6 +79,7 @@ export class UserRepository {
PK: id,
SK: `UserInfo#${id}`,
...userInfo,
userType,
password: hashedPassword,
createdAt,
};
Expand Down
76 changes: 41 additions & 35 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -39,6 +41,7 @@ export class UserService {
userInfo: CreateUserInfoRequestDto,
userType: UserType,
): Promise<CreateUserInfoResponseDto> {
console.log(userInfo, userType, 'userInfo, userType');
if (userType === UserType.NORMAL) {
const emailVerification =
await this.authRepository.findEmailVerificationByEmail(userInfo.email);
Expand All @@ -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;
Expand All @@ -74,46 +77,49 @@ export class UserService {
return responseItem;
}

async hashPassword(password: string): Promise<string> {
const saltOrRounds = 10;
const hashedPassword = await bcrypt.hash(password, saltOrRounds);
return hashedPassword;
}
async hashPassword(password: string): Promise<string> {
const saltOrRounds = 10;
const hashedPassword = await bcrypt.hash(password, saltOrRounds);
return hashedPassword;
}

async update(id: string, userInfo: UpdateUserInfoRequestDto): Promise<Omit<User, 'password'>> {
const { email, password, nickname } = userInfo;
async update(
id: string,
userInfo: UpdateUserInfoRequestDto,
): Promise<Omit<User, 'password'>> {
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<User> = {};
const updateData: Partial<User> = {};

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<User, 'password'> = {
id: user.id,
email: user.email,
nickname: user.nickname,
// 다른 필드들...
};
const user = await this.usersRepository.update(id, updateData);
const responseItem: Omit<User, 'password'> = {
id: user.id,
email: user.email,
nickname: user.nickname,
// 다른 필드들...
};

return responseItem;
}
return responseItem;
}

async getUserTestList(id: string, query: any): Promise<any> {
const limit = query.limit;
Expand Down

0 comments on commit 0b557cc

Please sign in to comment.