feat: added create user function in the user service
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
export enum UserMessages {
|
||||
USER_ALREADY_EXISTS = 'کاربر مورد نظر در حال حاضر وجود دارد. لطفا وارد حساب شوید!'
|
||||
}
|
||||
+17
-1
@@ -1,8 +1,24 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { NestFactory, Reflector } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { ClassSerializerInterceptor, ValidationPipe } from '@nestjs/common';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe(
|
||||
{
|
||||
whitelist: true,
|
||||
forbidNonWhitelisted: true,
|
||||
transform: true
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
app.useGlobalInterceptors(
|
||||
new ClassSerializerInterceptor(app.get(Reflector))
|
||||
)
|
||||
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Exclude } from 'class-transformer';
|
||||
import {
|
||||
IsEmail,
|
||||
IsNotEmpty,
|
||||
IsString,
|
||||
Matches,
|
||||
MaxLength,
|
||||
MinLength,
|
||||
} from 'class-validator';
|
||||
|
||||
export class CreateUserDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@MaxLength(20)
|
||||
firstName: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
lastName: string;
|
||||
@MaxLength(20)
|
||||
|
||||
@IsEmail()
|
||||
@MaxLength(100)
|
||||
email: string;
|
||||
|
||||
@Matches(/^09\d{9}$/, {
|
||||
message: 'Phone number must be a valid Iranian mobile number.',
|
||||
})
|
||||
phone: string;
|
||||
|
||||
@IsString()
|
||||
@MinLength(8)
|
||||
@Exclude()
|
||||
password: string;
|
||||
}
|
||||
@@ -1,9 +1,38 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { UserRepository } from '../repositories/user.repository';
|
||||
import { User } from '../entities/user.entity';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { CreateUserDto } from '../DTO/create-user.dto';
|
||||
import { UserMessages } from 'src/common/enums/messages.enum';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(
|
||||
private readonly userRepository: UserRepository
|
||||
) {}
|
||||
constructor(private readonly userRepository: UserRepository) {}
|
||||
|
||||
async signupUser(userDto: CreateUserDto): Promise<User> {
|
||||
const existingUser = await this.userRepository.findOne({
|
||||
where: [
|
||||
{
|
||||
phone: userDto.phone,
|
||||
},
|
||||
{
|
||||
email: userDto.email,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (existingUser)
|
||||
throw new BadRequestException(UserMessages.USER_ALREADY_EXISTS);
|
||||
|
||||
const hashedPassword: string = await bcrypt.hash(userDto.password, 10);
|
||||
|
||||
const user = this.userRepository.create({
|
||||
...userDto,
|
||||
password: hashedPassword,
|
||||
});
|
||||
|
||||
await this.userRepository.save(user);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user