nest-project/src/users/users.service.ts

38 lines
997 B
TypeScript

import { Injectable, NotFoundException } from '@nestjs/common'
import { PrismaService } from 'nestjs-prisma'
import { CreateUserDto } from './dto/create-user.dto'
import { PasswordService } from './password.service'
import { Prisma } from '@prisma/client'
import { UserEntity } from './entities/user.entity'
@Injectable()
export class UsersService {
constructor(
private prisma: PrismaService,
private passwordService: PasswordService,
) {}
async findUser(where: Prisma.UserWhereUniqueInput): Promise<UserEntity> {
const user = await this.prisma.user.findUnique({
where,
})
if (!user) {
throw new NotFoundException(`No user found`)
}
return user
}
async createUser(payload: CreateUserDto) {
const hashedPassword = await this.passwordService.hashPassword(
payload.password,
)
const user = await this.prisma.user.create({
data: {
...payload,
password: hashedPassword,
},
})
return user
}
}