Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 6x 6x 6x 6x 20x 20x 3x 3x 3x 1x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 4x 4x 1x 3x 3x 1x 2x 2x 1x 1x 1x 2x 2x 1x 4x 4x 2x 2x 2x | import { Injectable, ConflictException, NotFoundException, BadRequestException, } from "@nestjs/common"; import { UserRepository } from "./user.repository"; import { PasswordService } from "../auth/password.service"; import { User } from "./user.entity"; import { RegisterDto } from "../auth/dto/register.dto"; import { UpdateUserDto } from "./dto/update-user.dto"; import { ChangePasswordDto } from "./dto/change-password.dto"; import { UserProfile } from "@calendar-todo/shared-types"; @Injectable() export class UserService { constructor( private readonly userRepository: UserRepository, private readonly passwordService: PasswordService, ) {} async create(registerDto: RegisterDto): Promise<User> { const { email, password, name } = registerDto; // 이메일 중복 확인 const existingUser = await this.userRepository.findByEmail(email); if (existingUser) { throw new ConflictException("이미 가입된 이메일입니다"); } // 비밀번호 강도 검사 const passwordValidation = this.passwordService.validatePasswordStrength(password); if (!passwordValidation.isValid) { throw new BadRequestException(passwordValidation.errors); } // 비밀번호 해싱 const passwordHash = await this.passwordService.hashPassword(password); // 사용자 생성 const user = await this.userRepository.create({ email, passwordHash, name, emailVerified: false, isActive: true, }); return user; } async findById(id: string): Promise<User | null> { return this.userRepository.findById(id); } async findByEmail(email: string): Promise<User | null> { return this.userRepository.findByEmail(email); } async update(id: string, updateUserDto: UpdateUserDto): Promise<UserProfile> { const user = await this.userRepository.findById(id); if (!user) { throw new NotFoundException("User not found"); } const updatedUser = await this.userRepository.update(id, updateUserDto); Iif (!updatedUser) { throw new NotFoundException("User not found"); } return updatedUser.toProfile(); } async changePassword( id: string, changePasswordDto: ChangePasswordDto, ): Promise<void> { const user = await this.userRepository.findById(id); if (!user) { throw new NotFoundException("User not found"); } // 현재 비밀번호 확인 const isCurrentPasswordValid = await this.passwordService.comparePassword( changePasswordDto.currentPassword, user.passwordHash, ); if (!isCurrentPasswordValid) { throw new BadRequestException("Current password is incorrect"); } // 새 비밀번호 강도 검사 const passwordValidation = this.passwordService.validatePasswordStrength( changePasswordDto.newPassword, ); if (!passwordValidation.isValid) { throw new BadRequestException(passwordValidation.errors); } // 새 비밀번호 해싱 const newPasswordHash = await this.passwordService.hashPassword( changePasswordDto.newPassword, ); // 비밀번호 업데이트 await this.userRepository.update(id, { passwordHash: newPasswordHash }); } async delete(id: string): Promise<void> { const deleted = await this.userRepository.delete(id); if (!deleted) { throw new NotFoundException("User not found"); } } async validatePassword( email: string, password: string, ): Promise<User | null> { const user = await this.userRepository.findByEmail(email); if (!user || !user.isActive) { return null; } const isPasswordValid = await this.passwordService.comparePassword( password, user.passwordHash, ); return isPasswordValid ? user : null; } } |