feat: User services

This commit is contained in:
sauravdhakal12
2026-02-20 15:53:45 +05:45
parent 9561693cb4
commit f6bce78aee
39 changed files with 6509 additions and 66 deletions

1
src/user/dtos/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from './user.dto';

21
src/user/dtos/user.dto.ts Normal file
View 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;
}
}

View File

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

View File

@@ -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 },
});
}
}