chore: add user get profile and check validity route
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user