60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import { User } from '@/prisma/generated'
|
||
import { PrismaService } from '@/src/core/prisma/prisma.service'
|
||
import { CreateUserInput } from '@/src/module/auth/account/inputs/create-user.input'
|
||
import { VerificationService } from '@/src/module/auth/verification/verification.service'
|
||
import { ConflictException, Injectable } from '@nestjs/common'
|
||
import { hash } from 'argon2'
|
||
|
||
@Injectable()
|
||
export class AccountService {
|
||
constructor(
|
||
private readonly prismaService: PrismaService,
|
||
private readonly verificationService: VerificationService,
|
||
) {
|
||
}
|
||
|
||
public async me(id: User['id']) {
|
||
return this.prismaService.user.findUnique({
|
||
where: {
|
||
id,
|
||
},
|
||
})
|
||
}
|
||
|
||
public async create(input: CreateUserInput) {
|
||
const { email, name, password } = input
|
||
const isUserNameExists = await this.prismaService.user.findUnique({
|
||
where: {
|
||
name,
|
||
},
|
||
})
|
||
|
||
if (isUserNameExists) {
|
||
throw new ConflictException('Пользователь с таким именем уже существует')
|
||
}
|
||
|
||
const isUserEmailExists = await this.prismaService.user.findUnique({
|
||
where: {
|
||
email,
|
||
},
|
||
})
|
||
|
||
if (isUserEmailExists) {
|
||
throw new ConflictException('Пользователь с такоей почтой уже существует')
|
||
}
|
||
|
||
const user = await this.prismaService.user.create({
|
||
data: {
|
||
name,
|
||
email,
|
||
password: await hash(password),
|
||
displayName: name,
|
||
},
|
||
})
|
||
|
||
await this.verificationService.sendVerificationToken(user)
|
||
|
||
return user
|
||
}
|
||
}
|