feat: User services
This commit is contained in:
1
src/user/dtos/index.ts
Normal file
1
src/user/dtos/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './user.dto';
|
||||
21
src/user/dtos/user.dto.ts
Normal file
21
src/user/dtos/user.dto.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { User } from 'prisma/generated/prisma/client';
|
||||
|
||||
export class UserDTO {
|
||||
readonly id: string;
|
||||
readonly email: string;
|
||||
readonly firstName: string;
|
||||
readonly middleName: string | null;
|
||||
readonly lastName: string;
|
||||
readonly role: string;
|
||||
readonly profilePicture: string | null;
|
||||
|
||||
constructor(user: User) {
|
||||
this.id = user.id;
|
||||
this.email = user.email;
|
||||
this.firstName = user.firstName;
|
||||
this.lastName = user.lastName;
|
||||
this.middleName = user.middleName;
|
||||
this.role = user.role;
|
||||
this.profilePicture = user.profilePicture;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { PrismaModule } from 'src/prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
providers: [UserService]
|
||||
providers: [UserService],
|
||||
imports: [PrismaModule],
|
||||
})
|
||||
export class UserModule {}
|
||||
|
||||
@@ -1,4 +1,37 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConflictException, Injectable } from '@nestjs/common';
|
||||
import { Prisma } from 'prisma/generated/prisma/client';
|
||||
import { RegisterUserRequestDTO } from 'src/auth/dto';
|
||||
import { PrismaService } from 'src/prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {}
|
||||
export class UserService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async createUserWithPassword(dto: RegisterUserRequestDTO) {
|
||||
try {
|
||||
return await this.prisma.user.create({
|
||||
data: dto,
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
if (err.code === 'P2002') {
|
||||
throw new ConflictException('User already exists');
|
||||
}
|
||||
} else throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async findUserForAuth(email: string) {
|
||||
return await this.prisma.user.findUnique({
|
||||
where: { email },
|
||||
omit: { password: false }, // Password is omitted by default, we are just reverting it
|
||||
});
|
||||
}
|
||||
|
||||
async updateRefreshToken(id: string, refreshToken: string) {
|
||||
return await this.prisma.user.update({
|
||||
where: { id },
|
||||
data: { refreshToken },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user