feat: added create user function in the user service

This commit is contained in:
2026-07-19 03:39:26 +03:30
parent 95e3a91eea
commit 0b94a809f9
7 changed files with 271 additions and 27 deletions
+3
View File
@@ -0,0 +1,3 @@
export enum UserMessages {
USER_ALREADY_EXISTS = 'کاربر مورد نظر در حال حاضر وجود دارد. لطفا وارد حساب شوید!'
}
+17 -1
View File
@@ -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();
+35
View File
@@ -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;
}
+33 -4
View File
@@ -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;
}
}