116 lines
4.3 KiB
TypeScript
116 lines
4.3 KiB
TypeScript
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
|
|
import slugify from "slugify";
|
|
import { Not } from "typeorm";
|
|
|
|
import { RoleRepository } from "./roles.repository";
|
|
import { UserRepository } from "./users.repository";
|
|
import { roles } from "../../../../database/seeders/role.seeder";
|
|
import { CommonMessage, UserMessage } from "../../../common/enums/message.enum";
|
|
import { CompleteRegistrationDto } from "../../auth/DTO/complete-register.dto";
|
|
import { CheckValidityDTO } from "../DTO/check-validity.dto";
|
|
import { UpdateProfileDto } from "../DTO/update-profile.dto";
|
|
import { User } from "../entities/user.entity";
|
|
import { RoleEnum } from "../enums/role.enum";
|
|
import { ValidityType } from "../enums/validity-type.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 updateProfile(userId: string, updateProfileDto: UpdateProfileDto) {
|
|
const { firstName, lastName, birthDate } = updateProfileDto;
|
|
|
|
if (updateProfileDto.userName) {
|
|
updateProfileDto.userName = slugify(updateProfileDto.userName, { lower: true, trim: true });
|
|
const existUserName = await this.userRepository.findOneBy({ userName: updateProfileDto.userName, id: Not(userId) });
|
|
if (existUserName) throw new BadRequestException(UserMessage.USERNAME_EXIST);
|
|
}
|
|
const user = await this.userRepository.findOneBy({ id: userId });
|
|
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
|
|
|
const { userName } = updateProfileDto;
|
|
if (userName) user.userName = userName;
|
|
if (firstName) user.firstName = firstName;
|
|
if (lastName) user.lastName = lastName;
|
|
if (birthDate) user.birthDate = birthDate;
|
|
|
|
await this.userRepository.save(user);
|
|
return {
|
|
message: CommonMessage.UPDATE_SUCCESS,
|
|
};
|
|
}
|
|
/************************************************************ */
|
|
|
|
async findOneWithEmail(email: string): Promise<User | null> {
|
|
const user = await this.userRepository.findOneWithEmail(email);
|
|
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, userId: string) {
|
|
const { type } = checkDto;
|
|
if (type === ValidityType.USERNAME) checkDto.value = slugify(checkDto.value, { lower: true, trim: true });
|
|
|
|
const user = await this.userRepository.findOneBy({ [type]: checkDto.value, id: Not(userId) });
|
|
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;
|
|
}
|
|
}
|