This commit is contained in:
2025-11-10 22:42:32 +03:30
parent 45ca2fde06
commit bca4b75b6a
21 changed files with 54 additions and 654 deletions
@@ -1,17 +1,16 @@
import { Entity, ManyToOne, Opt, Property } from "@mikro-orm/core";
import { User } from "./user.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
import { Entity, ManyToOne, Opt, Property } from '@mikro-orm/core';
import { User } from './user.entity';
import { BaseEntity } from 'src/common/entities/base.entity';
@Entity()
export class RefreshToken extends BaseEntity {
@Property({ type: "varchar", length: 255 })
@Property({ type: 'varchar', length: 255 })
token!: string;
@ManyToOne(() => User, { deleteRule: "cascade" })
@ManyToOne(() => User, { deleteRule: 'cascade' })
user!: User;
@Property({ type: "timestamptz" })
@Property({ type: 'timestamptz' })
expiresAt!: Date;
@Property({ default: false })
+7 -21
View File
@@ -1,19 +1,6 @@
import { Entity, Enum, Property, Filter, BaseEntity } from '@mikro-orm/core';
import { Entity, Property, BaseEntity, OneToMany, Collection } from '@mikro-orm/core';
import { RefreshToken } from './refresh-token.entity';
export enum EducationLevel {
diploma = 'دیپلم',
associate = 'کاردانی',
bachelor = 'کارشناسی',
master = 'کارشناسی ارشد',
phd = 'دکتری',
}
export enum UserStatus {
finished = 'finished',
pending = 'pending',
}
@Filter({ name: 'notDeleted', cond: { deletedAt: null }, default: true })
@Entity({ tableName: 'users' })
export class User extends BaseEntity {
@Property({ nullable: true })
@@ -25,10 +12,9 @@ export class User extends BaseEntity {
@Property({ unique: true })
phone!: string;
@Enum({
items: () => UserStatus,
nullable: true,
default: UserStatus.pending,
})
status?: UserStatus;
@Property({ default: true })
isActive: boolean = true;
@OneToMany(() => RefreshToken, token => token.user)
refreshTokens = new Collection<RefreshToken>(this);
}
+11 -17
View File
@@ -1,7 +1,7 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@mikro-orm/nestjs';
import { EntityRepository, FilterQuery } from '@mikro-orm/core';
import { User, UserStatus } from './entities/user.entity';
import { User } from './entities/user.entity';
import { EntityManager } from '@mikro-orm/postgresql';
import { UpdateUserDto } from './dto/update-user.dto';
import { FindUsersDto } from './dto/find-user.dto';
@@ -28,23 +28,23 @@ export class UserService {
return user;
}
async updateUser(userId: number, dto: UpdateUserDto): Promise<User> {
const user = await this.em.findOneOrFail(User, userId, {}).catch(() => {
throw new NotFoundException(`User with ID ${userId} not found.`);
});
// async updateUser(userId: string, dto: UpdateUserDto): Promise<User> {
// const user = await this.em.findOneOrFail(User, userId, {}).catch(() => {
// throw new NotFoundException(`User with ID ${userId} not found.`);
// });
this.em.assign(user, { ...dto, status: UserStatus.finished });
// this.em.assign(user, { ...dto });
await this.em.flush();
// await this.em.flush();
return user;
}
// return user;
// }
async findByPhone(phone: string): Promise<User | null> {
return this.userRepository.findOne({ phone });
}
async findById(id: number): Promise<User | null> {
async findById(id: string): Promise<User | null> {
return this.userRepository.findOne({ id });
}
@@ -55,7 +55,7 @@ export class UserService {
}
async findAll(dto: FindUsersDto): Promise<PaginatedResult<User>> {
const { page = 1, limit = 10, search, education, orderBy = 'createdAt', order = 'desc' } = dto;
const { page = 1, limit = 10, search, orderBy = 'createdAt', order = 'desc' } = dto;
// 1. Calculate pagination
const offset = (page - 1) * limit;
@@ -63,10 +63,6 @@ export class UserService {
// 2. Build the 'where' filter query
const where: FilterQuery<User> = {};
if (education) {
where.education = education;
}
// 4. Add 'search' logic (case-insensitive)
if (search) {
const searchPattern = `%${search}%`;
@@ -75,8 +71,6 @@ export class UserService {
{ firstName: { $ilike: searchPattern } },
{ lastName: { $ilike: searchPattern } },
{ phone: { $ilike: searchPattern } },
{ nationalCode: { $ilike: searchPattern } },
{ personalCode: { $ilike: searchPattern } },
];
}