chore: add ticket module and tickets entity
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsEnum, IsNotEmpty, IsString } from "class-validator";
|
||||
import { IsEnum, IsNotEmpty, IsString, Length } from "class-validator";
|
||||
|
||||
import { CommonMessage } from "../../../common/enums/message.enum";
|
||||
import { CommonMessage, UserMessage } from "../../../common/enums/message.enum";
|
||||
import { ValidityType } from "../enums/validity-type.enum";
|
||||
|
||||
export class CheckValidityDTO {
|
||||
@@ -11,6 +11,7 @@ export class CheckValidityDTO {
|
||||
type: ValidityType;
|
||||
|
||||
@IsNotEmpty({ message: CommonMessage.THIS_FILED_IS_REQUIRED })
|
||||
@Length(3, 50, { message: UserMessage.USERNAME_SHOULD_BE_BETWEEN_3_AND_50 })
|
||||
@IsString({ message: CommonMessage.THIS_FILED_IS_REQUIRED })
|
||||
@ApiProperty({ example: "mahyargdz" })
|
||||
value: string;
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
export class UpdateProfileDto {
|
||||
userName: string;
|
||||
import { ApiProperty, PartialType, PickType } from "@nestjs/swagger";
|
||||
import { IsNotEmpty, IsOptional, IsString, Length } from "class-validator";
|
||||
|
||||
import { UserMessage } from "../../../common/enums/message.enum";
|
||||
import { CompleteRegistrationDto } from "../../auth/DTO/complete-register.dto";
|
||||
|
||||
export class UpdateProfileDto extends PartialType(PickType(CompleteRegistrationDto, ["firstName", "lastName", "birthDate"] as const)) {
|
||||
@IsOptional()
|
||||
@IsNotEmpty({ message: UserMessage.USERNAME_NOT_EMPTY })
|
||||
@Length(3, 50, { message: UserMessage.USERNAME_SHOULD_BE_BETWEEN_3_AND_50 })
|
||||
@IsString({ message: UserMessage.USERNAME_NOT_EMPTY })
|
||||
@ApiProperty({ description: "User name", example: "mamad24" })
|
||||
userName?: string;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Column, Entity, JoinColumn, OneToOne } from "typeorm";
|
||||
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from "typeorm";
|
||||
|
||||
import { Role } from "./role.entity";
|
||||
import { BaseEntity } from "../../../common/entities/base.entity";
|
||||
import { Ticket } from "../../tickets/entities/ticket.entity";
|
||||
|
||||
@Entity()
|
||||
export class User extends BaseEntity {
|
||||
@@ -11,7 +12,7 @@ export class User extends BaseEntity {
|
||||
@Column({ type: "varchar", length: 11, unique: true, nullable: false })
|
||||
phone: string;
|
||||
|
||||
@Column({ type: "varchar", length: 150, unique: true, nullable: true })
|
||||
@Column({ type: "varchar", length: 50, unique: true, nullable: true })
|
||||
userName: string;
|
||||
|
||||
@Column({ type: "varchar", length: 150 })
|
||||
@@ -23,13 +24,16 @@ export class User extends BaseEntity {
|
||||
@Column({ type: "varchar", length: 200 })
|
||||
lastName: string;
|
||||
|
||||
@Column()
|
||||
@Column({ type: "varchar", length: 12, nullable: true })
|
||||
birthDate: string;
|
||||
|
||||
@Column({ type: "varchar", length: 100 })
|
||||
@Column({ type: "varchar", length: 100, unique: true, nullable: false })
|
||||
nationalCode: string;
|
||||
|
||||
@JoinColumn()
|
||||
@OneToOne(() => Role, { eager: true, cascade: true, onDelete: "RESTRICT", nullable: false })
|
||||
@ManyToOne(() => Role, { eager: true, onDelete: "RESTRICT", nullable: false })
|
||||
role: Role;
|
||||
|
||||
@OneToMany(() => Ticket, (ticket) => ticket.user)
|
||||
tickets: Ticket[];
|
||||
}
|
||||
|
||||
@@ -21,4 +21,10 @@ export class UserRepository extends Repository<User> {
|
||||
phone,
|
||||
});
|
||||
}
|
||||
|
||||
async findOneWithUserName(userName: string): Promise<User | null> {
|
||||
return this.findOneBy({
|
||||
userName,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Body, Controller, Get, Patch, Post } from "@nestjs/common";
|
||||
import { Body, Controller, Get, HttpCode, HttpStatus, Patch, Post } from "@nestjs/common";
|
||||
import { ApiOperation, ApiTags } from "@nestjs/swagger";
|
||||
|
||||
import { CheckValidityDTO } from "./DTO/check-validity.dto";
|
||||
import { UpdateProfileDto } from "./DTO/update-profile.dto";
|
||||
import { User } from "./entities/user.entity";
|
||||
import { UsersService } from "./providers/users.service";
|
||||
import { AuthGuards } from "../../common/decorators/auth-guard.decorator";
|
||||
@@ -19,13 +20,18 @@ export class UsersController {
|
||||
return this.usersService.getMe(user.id);
|
||||
}
|
||||
|
||||
@AuthGuards()
|
||||
@ApiOperation({ summary: "Update user profile" })
|
||||
@Patch("update-profile")
|
||||
updateProfile() {}
|
||||
updateProfile(@Body() updateProfileDto: UpdateProfileDto, @UserDec() user: User) {
|
||||
return this.usersService.updateProfile(user.id, updateProfileDto);
|
||||
}
|
||||
|
||||
@AuthGuards()
|
||||
@ApiOperation({ summary: "Check validity of user field" })
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Post("check-validity")
|
||||
checkValidity(@Body() checkValidityDto: CheckValidityDTO) {
|
||||
return this.usersService.checkValidity(checkValidityDto);
|
||||
checkValidity(@Body() checkValidityDto: CheckValidityDTO, @UserDec() user: User) {
|
||||
return this.usersService.checkValidity(checkValidityDto, user.id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user