| | import { Injectable, InternalServerErrorException } from "@nestjs/common"; |
| | import { Prisma, User } from "@prisma/client"; |
| | import { UserWithSecrets } from "@reactive-resume/dto"; |
| | import { ErrorMessage } from "@reactive-resume/utils"; |
| | import { PrismaService } from "nestjs-prisma"; |
| |
|
| | import { StorageService } from "../storage/storage.service"; |
| |
|
| | @Injectable() |
| | export class UserService { |
| | constructor( |
| | private readonly prisma: PrismaService, |
| | private readonly storageService: StorageService, |
| | ) {} |
| |
|
| | async findOneById(id: string): Promise<UserWithSecrets> { |
| | const user = await this.prisma.user.findUniqueOrThrow({ |
| | where: { id }, |
| | include: { secrets: true }, |
| | }); |
| |
|
| | if (!user.secrets) { |
| | throw new InternalServerErrorException(ErrorMessage.SecretsNotFound); |
| | } |
| |
|
| | return user; |
| | } |
| |
|
| | async findOneByIdentifier(identifier: string): Promise<UserWithSecrets | null> { |
| | const user = await (async (identifier: string) => { |
| | |
| | const user = await this.prisma.user.findUnique({ |
| | where: { email: identifier }, |
| | include: { secrets: true }, |
| | }); |
| |
|
| | |
| | if (user) return user; |
| |
|
| | |
| | |
| | return this.prisma.user.findUnique({ |
| | where: { username: identifier }, |
| | include: { secrets: true }, |
| | }); |
| | })(identifier); |
| |
|
| | return user; |
| | } |
| |
|
| | async findOneByIdentifierOrThrow(identifier: string): Promise<UserWithSecrets> { |
| | const user = await (async (identifier: string) => { |
| | |
| | const user = await this.prisma.user.findUnique({ |
| | where: { email: identifier }, |
| | include: { secrets: true }, |
| | }); |
| |
|
| | |
| | if (user) return user; |
| |
|
| | |
| | |
| | return this.prisma.user.findUniqueOrThrow({ |
| | where: { username: identifier }, |
| | include: { secrets: true }, |
| | }); |
| | })(identifier); |
| |
|
| | return user; |
| | } |
| |
|
| | create(data: Prisma.UserCreateInput): Promise<UserWithSecrets> { |
| | return this.prisma.user.create({ data, include: { secrets: true } }); |
| | } |
| |
|
| | updateByEmail(email: string, data: Prisma.UserUpdateArgs["data"]): Promise<User> { |
| | return this.prisma.user.update({ where: { email }, data }); |
| | } |
| |
|
| | async updateByResetToken( |
| | resetToken: string, |
| | data: Prisma.SecretsUpdateArgs["data"], |
| | ): Promise<void> { |
| | await this.prisma.secrets.update({ where: { resetToken }, data }); |
| | } |
| |
|
| | async deleteOneById(id: string): Promise<void> { |
| | await Promise.all([ |
| | this.storageService.deleteFolder(id), |
| | this.prisma.user.delete({ where: { id } }), |
| | ]); |
| | } |
| | } |
| |
|