refactor: change the role to roles and many to many

This commit is contained in:
mahyargdz
2025-02-16 16:58:51 +03:30
parent 9cdd7ef28e
commit ad673613d9
32 changed files with 264 additions and 88 deletions
@@ -0,0 +1,14 @@
import { Column, Entity, ManyToMany } from "typeorm";
import { Role } from "./role.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
import { PermissionEnum } from "../enums/permission.enum";
@Entity()
export class Permission extends BaseEntity {
@Column({ type: "enum", enum: PermissionEnum, nullable: false, unique: true })
name: PermissionEnum;
@ManyToMany(() => Role, (role) => role.permissions)
roles: Role[];
}
+6 -1
View File
@@ -1,5 +1,6 @@
import { Column, Entity } from "typeorm";
import { Column, Entity, JoinTable, ManyToMany } from "typeorm";
import { Permission } from "./permission.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
import { RoleEnum } from "../enums/role.enum";
@@ -7,4 +8,8 @@ import { RoleEnum } from "../enums/role.enum";
export class Role extends BaseEntity {
@Column({ type: "enum", enum: RoleEnum, default: RoleEnum.USER, nullable: false, unique: true })
name: RoleEnum;
@ManyToMany(() => Permission, (permission) => permission.roles)
@JoinTable({ name: "role_permission_relation" })
permissions: Permission[];
}
+12 -6
View File
@@ -1,5 +1,5 @@
import { Exclude } from "class-transformer";
import { Column, Entity, ManyToMany, ManyToOne, OneToMany, OneToOne } from "typeorm";
import { Column, Entity, JoinTable, ManyToMany, OneToMany, OneToOne } from "typeorm";
import { LegalUser } from "./legal-user.entity";
import { RealUser } from "./real-user.entity";
@@ -47,8 +47,17 @@ export class User extends BaseEntity {
nationalCode: string;
//-----------------------------------------
@ManyToOne(() => Role, { eager: true, onDelete: "RESTRICT", nullable: false })
role: Role;
// @ManyToOne(() => Role, { eager: true, onDelete: "RESTRICT", nullable: false })
// role: Role;
@ManyToMany(() => Role)
@JoinTable({ name: "user_role_relation" })
roles: Role[];
@ManyToMany(() => UserGroup, (group) => group.users)
groups: UserGroup[];
//---------------------------------------
@OneToMany(() => Ticket, (ticket) => ticket.user)
tickets: Ticket[];
@@ -56,9 +65,6 @@ export class User extends BaseEntity {
@OneToMany(() => TicketMessage, (ticketMessage) => ticketMessage.author)
ticketMessage: TicketMessage[];
@ManyToMany(() => UserGroup, (group) => group.users)
groups: UserGroup[];
@OneToMany(() => Criticism, (criticism) => criticism.user)
criticisms: Criticism[];