chore: add user get profile and check validity route

This commit is contained in:
mahyargdz
2025-01-22 09:50:03 +03:30
parent 7431dad85f
commit 72b6cb35a0
39 changed files with 874 additions and 127 deletions
@@ -0,0 +1,17 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsEnum, IsNotEmpty, IsString } from "class-validator";
import { CommonMessage } from "../../../common/enums/message.enum";
import { ValidityType } from "../enums/validity-type.enum";
export class CheckValidityDTO {
@IsNotEmpty({ message: CommonMessage.VALIDITY_TYPE_REQUIRED })
@IsEnum(ValidityType)
@ApiProperty({ enum: ValidityType, example: ValidityType.USERNAME })
type: ValidityType;
@IsNotEmpty({ message: CommonMessage.THIS_FILED_IS_REQUIRED })
@IsString({ message: CommonMessage.THIS_FILED_IS_REQUIRED })
@ApiProperty({ example: "mahyargdz" })
value: string;
}
@@ -0,0 +1,3 @@
export class UpdateProfileDto {
userName: string;
}
+1 -1
View File
@@ -5,6 +5,6 @@ import { RoleEnum } from "../enums/role.enum";
@Entity()
export class Role extends BaseEntity {
@Column({ type: "enum", enum: RoleEnum, default: RoleEnum.USER, nullable: false })
@Column({ type: "enum", enum: RoleEnum, default: RoleEnum.USER, nullable: false, unique: true })
name: RoleEnum;
}
+6 -3
View File
@@ -5,11 +5,14 @@ import { BaseEntity } from "../../../common/entities/base.entity";
@Entity()
export class User extends BaseEntity {
@Column({ type: "varchar", length: 150, unique: true, nullable: true })
@Column({ type: "varchar", length: 150, nullable: true })
email: string;
@Column({ type: "varchar", length: 11, unique: true, nullable: true })
phoneNumber: string;
@Column({ type: "varchar", length: 11, unique: true, nullable: false })
phone: string;
@Column({ type: "varchar", length: 150, unique: true, nullable: true })
userName: string;
@Column({ type: "varchar", length: 150 })
password: string;
@@ -0,0 +1,3 @@
export enum ValidityType {
USERNAME = "userName",
}
@@ -11,8 +11,14 @@ export class UserRepository extends Repository<User> {
}
async findOneWithEmail(email: string): Promise<User | null> {
return await this.findOneBy({
return this.findOneBy({
email,
});
}
async findOneWithPhone(phone: string): Promise<User | null> {
return this.findOneBy({
phone,
});
}
}
+56 -9
View File
@@ -3,35 +3,82 @@ import { BadRequestException, Injectable, Logger } from "@nestjs/common";
import { RoleRepository } from "./roles.repository";
import { UserRepository } from "./users.repository";
import { roles } from "../../../../db/seeders/role.seeder";
import { UserMessage } from "../../../common/enums/message.enum";
import { CommonMessage, UserMessage } from "../../../common/enums/message.enum";
import { CompleteRegistrationDto } from "../../auth/DTO/complete-register.dto";
import { CheckValidityDTO } from "../DTO/check-validity.dto";
import { User } from "../entities/user.entity";
import { RoleEnum } from "../enums/role.enum";
@Injectable()
export class UsersService {
private readonly userSelect = {
id: true,
phone: true,
email: true,
userName: true,
firstName: true,
lastName: true,
nationalCode: true,
birthDate: true,
role: {
id: true,
name: true,
},
};
private logger = new Logger(UsersService.name);
constructor(
private userRepository: UserRepository,
private roleRepository: RoleRepository,
) {}
/************************************************************ */
async getMe(userId: string) {
const user = await this.userRepository.findOne({ where: { id: userId }, select: this.userSelect });
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return { user };
}
/************************************************************ */
async findOneWithEmail(email: string): Promise<User> {
const user = await this.userRepository.findOneWithEmail(email);
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return user;
}
/************************************************************ */
async findOneWithPhone(phone: string): Promise<User | null> {
const user = await this.userRepository.findOneWithPhone(phone);
return user;
}
/************************************************************ */
async createUser(registerDto: CompleteRegistrationDto, hashedPassword: string): Promise<User> {
// const existEmail = await this.userRepository.findOneWithEmail(registerDto.email);
// if (existEmail) throw new BadRequestException(UserMessage.EMAIL_EXIST);
const role = await this.roleRepository.findOneBy({ name: RoleEnum.USER });
if (!role) throw new BadRequestException(UserMessage.ROLE_NOT_FOUND);
const user = this.userRepository.create({ ...registerDto, password: hashedPassword, role });
return await this.userRepository.save(user);
}
/************************************************************ */
async checkValidity(checkDto: CheckValidityDTO) {
const { type, value } = checkDto;
const user = await this.userRepository.findOneBy({ [type]: value });
if (user) throw new BadRequestException(UserMessage.USER_EXISTS);
return { message: CommonMessage.VALID_FOR_CHOOSE };
}
///****************************************************** */
async roleSeeder() {
const countRole = await this.roleRepository.count();
if (countRole > 0) return;
const createdRoles = await this.roleRepository.insert(roles);
this.logger.log({ createdRoles });
return createdRoles;
}
async createUser(registerDto: CompleteRegistrationDto, hashedPassword: string): Promise<User> {
const existEmail = await this.userRepository.findOneWithEmail(registerDto.email);
if (existEmail) throw new BadRequestException(UserMessage.EMAIL_EXIST);
const user = this.userRepository.create({ ...registerDto, password: hashedPassword });
return await this.userRepository.save(user);
}
}
+28 -3
View File
@@ -1,6 +1,31 @@
import { Controller } from "@nestjs/common";
import { ApiTags } from "@nestjs/swagger";
import { Body, Controller, Get, Patch, Post } from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { CheckValidityDTO } from "./DTO/check-validity.dto";
import { User } from "./entities/user.entity";
import { UsersService } from "./providers/users.service";
import { AuthGuards } from "../../common/decorators/auth-guard.decorator";
import { UserDec } from "../../common/decorators/user.decorator";
@Controller("users")
@ApiTags("users")
export class UsersController {}
export class UsersController {
constructor(private usersService: UsersService) {}
@AuthGuards()
@ApiOperation({ summary: "Get user profile" })
@Get("me")
getMe(@UserDec() user: User) {
return this.usersService.getMe(user.id);
}
@Patch("update-profile")
updateProfile() {}
@AuthGuards()
@ApiOperation({ summary: "Check validity of user field" })
@Post("check-validity")
checkValidity(@Body() checkValidityDto: CheckValidityDTO) {
return this.usersService.checkValidity(checkValidityDto);
}
}
+3 -3
View File
@@ -3,10 +3,10 @@ import { TypeOrmModule } from "@nestjs/typeorm";
import { Role } from "./entities/role.entity";
import { User } from "./entities/user.entity";
import { RoleRepository } from "./providers/roles.repository";
import { UserRepository } from "./providers/users.repository";
import { UsersService } from "./providers/users.service";
import { UsersController } from "./users.controller";
import { RoleRepository } from "./providers/roles.repository";
@Module({
imports: [TypeOrmModule.forFeature([User, Role])],
@@ -15,8 +15,8 @@ import { RoleRepository } from "./providers/roles.repository";
exports: [UsersService, TypeOrmModule],
})
export class UsersModule implements OnApplicationBootstrap {
constructor(private userService: UsersService) {}
// constructor(private userService: UsersService) {}
async onApplicationBootstrap() {
await this.userService.roleSeeder();
// await this.userService.roleSeeder();
}
}