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[];
@@ -0,0 +1,19 @@
export enum PermissionEnum {
SERVICES = "services",
CUSTOMERS = "customers",
AGENTS = "agents",
DEVELOPERS = "developers",
INVOICES = "invoices",
TRANSACTIONS = "transactions",
DISCOUNTS = "discounts",
ADMINS = "admins",
TICKETS = "tickets",
CRITICISMS = "criticisms",
CONTACTS_US = "contacts_us",
ADVERTISEMENTS = "advertisements",
ANNOUNCEMENTS = "announcements",
BLOGS = "blogs",
LEARNINGS = "learnings",
LOGS = "logs",
SETTINGS = "settings",
}
+2 -2
View File
@@ -1,6 +1,6 @@
export enum RoleEnum {
SUPER_ADMIN = "super_admin",
ADMIN = "admin",
USER = "user",
VISITOR = "visitor",
DEVELOPER = "developer",
AGENT = "agent",
}
+3 -3
View File
@@ -107,13 +107,13 @@ export class UsersService {
/************************************************************ */
async createUser(registerDto: CompleteRegistrationDto, hashedPassword: string, queryRunner: QueryRunner): Promise<User> {
const role = await queryRunner.manager.findOne(Role, { where: { name: RoleEnum.USER } });
const role = await queryRunner.manager.findOneBy(Role, { name: RoleEnum.USER });
if (!role) throw new BadRequestException(UserMessage.ROLE_NOT_FOUND);
const user = queryRunner.manager.create(User, {
...registerDto,
password: hashedPassword,
role,
roles: [role],
});
await queryRunner.manager.save(user);
@@ -264,7 +264,7 @@ export class UsersService {
const customer = await this.userRepository.findOne({
where: {
id: userId,
role: {
roles: {
name: RoleEnum.USER,
},
},
@@ -11,20 +11,41 @@ export class UserRepository extends Repository<User> {
}
async findOneWithEmail(email: string): Promise<User | null> {
return this.findOneBy({
email,
return this.findOne({
where: {
email,
},
relations: {
roles: {
permissions: true,
},
},
});
}
async findOneWithPhone(phone: string): Promise<User | null> {
return this.findOneBy({
phone,
return this.findOne({
where: {
phone,
},
relations: {
roles: {
permissions: true,
},
},
});
}
async findOneWithUserName(userName: string): Promise<User | null> {
return this.findOneBy({
userName,
return this.findOne({
where: {
userName,
},
relations: {
roles: {
permissions: true,
},
},
});
}
}
+2 -1
View File
@@ -2,6 +2,7 @@ import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { LegalUser } from "./entities/legal-user.entity";
import { Permission } from "./entities/permission.entity";
import { Role } from "./entities/role.entity";
import { UserGroup } from "./entities/user-group.entity";
import { User } from "./entities/user.entity";
@@ -19,7 +20,7 @@ import { RealUser } from "./entities/real-user.entity";
import { RealUserRepository } from "./repositories/real-user.repository";
@Module({
imports: [TypeOrmModule.forFeature([User, Role, UserGroup, UserSetting, RealUser, LegalUser]), WalletsModule],
imports: [TypeOrmModule.forFeature([User, Role, UserGroup, UserSetting, RealUser, LegalUser, Permission]), WalletsModule],
providers: [
UsersService,
UserRepository,