twitch-clone/backend/src/module/auth/account/account.service.ts

60 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}
}