chore: add ticket module and tickets entity

This commit is contained in:
mahyargdz
2025-01-22 17:03:46 +03:30
parent c48ec70638
commit e3762c1eba
16 changed files with 166 additions and 27 deletions
+35 -3
View File
@@ -1,4 +1,6 @@
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";
@@ -6,8 +8,10 @@ import { roles } from "../../../../db/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 {
@@ -38,6 +42,31 @@ export class UsersService {
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> {
@@ -65,10 +94,13 @@ export class UsersService {
}
/************************************************************ */
async checkValidity(checkDto: CheckValidityDTO) {
const { type, value } = checkDto;
const user = await this.userRepository.findOneBy({ [type]: value });
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 };
}