Files
dzone-api/src/modules/users/entities/user.entity.ts
T
2026-03-08 11:01:56 +03:30

72 lines
2.3 KiB
TypeScript

import { Collection, Entity, EntityRepositoryType, ManyToOne, OneToMany, OneToOne, Opt, Property, Unique } from "@mikro-orm/core";
import { RefreshToken } from "./refresh-token.entity";
import { Role } from "./role.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
import { UserAnnouncement } from "../../announcements/entities/user-announcement.entity";
import { Business } from "../../businesses/entities/business.entity";
import { Company } from "../../companies/entities/company.entity";
import { UserRepository } from "../repositories/user.repository";
@Entity({ repository: () => UserRepository })
@Unique({ properties: ["phone", "business", "userName", "nationalCode"] })
export class User extends BaseEntity {
@Property({ type: "varchar", length: 150 })
firstName!: string;
@Property({ type: "varchar", length: 200 })
lastName!: string;
@Property({ type: "varchar", length: 150, nullable: true })
email?: string;
@Property({ type: "varchar", length: 11, nullable: false })
phone!: string;
@Property({ type: "varchar", length: 50, nullable: true })
userName!: string;
@Property({ type: "varchar", length: 150 })
password!: string;
@Property({ type: "varchar", length: 12, nullable: true })
birthDate?: string;
@Property({ type: "varchar", length: 100, nullable: true })
nationalCode?: string;
@Property({ type: "varchar", length: 100, nullable: true })
profilePic?: string;
@Property({ type: "boolean", default: false })
emailVerified!: boolean & Opt;
@Property({ persist: false })
get fullName(): string | null {
return `${this.firstName} ${this.lastName}`;
}
//-----------------------------------
@ManyToOne(() => Role, { deleteRule: "restrict" })
role!: Role;
@ManyToOne(() => Business, { deleteRule: "restrict" })
business!: Business;
//-----------------------------------
@OneToOne(() => Company, (company) => company.user, { orphanRemoval: true, nullable: true })
company?: Company;
//-----------------------------------
@OneToMany(() => RefreshToken, (token) => token.user)
refreshTokens = new Collection<RefreshToken>(this);
@OneToMany(() => UserAnnouncement, (userAnnouncement) => userAnnouncement.user)
userAnnouncements = new Collection<UserAnnouncement>(this);
[EntityRepositoryType]?: UserRepository;
}