|
| 1 | +import { HttpException, HttpStatus, Logger } from '@nestjs/common'; |
| 2 | +import { CreateUserDto } from '../dto/user-create.dto'; |
| 3 | +import { UserLoginResponseDto } from '../dto/user-login-response.dto'; |
| 4 | +import { PrismaService } from '../../prisma/prisma.service'; |
| 5 | +import { User } from '@prisma/client'; |
| 6 | +import { UpdateUserDto } from '../dto/user-update.dto'; |
| 7 | +import { AuthService } from '../../auth/auth.service'; |
| 8 | +import { UserLoginRequestDto } from '../dto/user-login-request.dto'; |
| 9 | +import { Users } from '../users.interface'; |
| 10 | + |
| 11 | +export class DbUsersService implements Users { |
| 12 | + private readonly logger: Logger = new Logger(DbUsersService.name); |
| 13 | + |
| 14 | + constructor( |
| 15 | + private readonly prismaService: PrismaService, |
| 16 | + private readonly authService: AuthService |
| 17 | + ) {} |
| 18 | + |
| 19 | + async create(createUserDto: CreateUserDto): Promise<UserLoginResponseDto> { |
| 20 | + const user = { |
| 21 | + email: createUserDto.email.trim().toLowerCase(), |
| 22 | + firstName: createUserDto.firstName, |
| 23 | + lastName: createUserDto.lastName, |
| 24 | + apiKey: this.authService.generateApiKey(), |
| 25 | + password: await this.authService.encryptPassword(createUserDto.password), |
| 26 | + }; |
| 27 | + |
| 28 | + const userData = await this.prismaService.user.create({ |
| 29 | + data: user, |
| 30 | + }); |
| 31 | + |
| 32 | + return new UserLoginResponseDto(userData, null); |
| 33 | + } |
| 34 | + |
| 35 | + async update(id: string, userDto: UpdateUserDto): Promise<UserLoginResponseDto> { |
| 36 | + const user = await this.prismaService.user.update({ |
| 37 | + where: { id }, |
| 38 | + data: { |
| 39 | + email: userDto.email, |
| 40 | + firstName: userDto.firstName, |
| 41 | + lastName: userDto.lastName, |
| 42 | + }, |
| 43 | + }); |
| 44 | + const token = this.authService.signToken(user); |
| 45 | + return new UserLoginResponseDto(user, token); |
| 46 | + } |
| 47 | + |
| 48 | + async changePassword(user: User, newPassword: string): Promise<boolean> { |
| 49 | + await this.prismaService.user.update({ |
| 50 | + where: { id: user.id }, |
| 51 | + data: { |
| 52 | + password: await this.authService.encryptPassword(newPassword), |
| 53 | + }, |
| 54 | + }); |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + async login(userLoginRequestDto: UserLoginRequestDto) { |
| 59 | + const user = await this.prismaService.user.findUnique({ |
| 60 | + where: { email: userLoginRequestDto.email }, |
| 61 | + }); |
| 62 | + if (!user) { |
| 63 | + throw new HttpException('Invalid email or password.', HttpStatus.BAD_REQUEST); |
| 64 | + } |
| 65 | + |
| 66 | + const isMatch = await this.authService.compare(userLoginRequestDto.password, user.password); |
| 67 | + |
| 68 | + if (!isMatch) { |
| 69 | + throw new HttpException('Invalid email or password.', HttpStatus.BAD_REQUEST); |
| 70 | + } |
| 71 | + |
| 72 | + const token = this.authService.signToken(user); |
| 73 | + return new UserLoginResponseDto(user, token); |
| 74 | + } |
| 75 | +} |
0 commit comments