role perms

This commit is contained in:
2025-11-12 09:28:56 +03:30
parent 046286b559
commit 440c3721ce
31 changed files with 488 additions and 98 deletions
+7 -3
View File
@@ -3,11 +3,15 @@ import { AdminService } from './admin.service';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { Admin } from './entities/admin.entity';
import { JwtModule } from '@nestjs/jwt';
import { AdminRepository } from './repositories/rest.repository';
import { Role } from './entities/role.entity';
import { Permission } from './entities/permission.entity';
import { RolePermission } from './entities/rolePermission.entity';
@Module({
providers: [AdminService],
providers: [AdminService, AdminRepository],
controllers: [],
imports: [MikroOrmModule.forFeature([Admin]), JwtModule],
exports: [AdminService],
imports: [MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission]), JwtModule],
exports: [AdminService, AdminRepository],
})
export class AdminModule {}
+13 -2
View File
@@ -1,7 +1,11 @@
import { Entity, Property } from '@mikro-orm/core';
import { Entity, ManyToOne, OneToOne, Property, Unique } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
import { Role } from './role.entity';
@Entity({ tableName: 'admins' })
// Add the Unique constraint for the combination of 'phone' and 'restaurant'
@Unique({ properties: ['phone', 'restaurant'] })
export class Admin extends BaseEntity {
@Property({ nullable: true })
firstName?: string;
@@ -9,6 +13,13 @@ export class Admin extends BaseEntity {
@Property({ nullable: true })
lastName?: string;
@Property({ unique: true })
@Property()
phone!: string;
// Add the new role property
@ManyToOne(() => Role)
role!: Role;
@OneToOne(() => Restaurant, { owner: true })
restaurant!: Restaurant;
}
+13
View File
@@ -0,0 +1,13 @@
import { Entity, Property, Unique, ManyToMany, Collection } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Role } from './role.entity';
@Entity({ tableName: 'permissions' })
export class Permission extends BaseEntity {
@Property()
@Unique()
name!: string;
@ManyToMany({ entity: () => Role, mappedBy: r => r.permissions })
roles = new Collection<Role>(this);
}
+22
View File
@@ -0,0 +1,22 @@
// role.entity.ts
import { Collection, Entity, ManyToMany, OneToMany, OneToOne, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Admin } from './admin.entity';
import { Permission } from './permission.entity';
import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
import { RolePermission } from './rolePermission.entity';
@Entity()
export class Role extends BaseEntity {
@Property()
name!: string;
@ManyToMany({ entity: () => Permission, pivotEntity: () => RolePermission, inversedBy: p => p.roles })
permissions = new Collection<Permission>(this);
@OneToMany(() => Admin, admin => admin.role)
admins = new Collection<Admin>(this);
@OneToOne(() => Restaurant, { owner: true, nullable: true })
restaurant?: Restaurant;
}
@@ -0,0 +1,17 @@
// role-permission.entity.ts (pivot)
import { Entity, ManyToOne } from '@mikro-orm/core';
import { Role } from './role.entity';
import { Permission } from './permission.entity';
@Entity({ tableName: 'role_permissions' })
export class RolePermission {
// دو ManyToOne دقیقاً؛ هر کدام می‌تونن primary: true باشند برای composite PK
@ManyToOne(() => Role, { primary: true })
role!: Role;
@ManyToOne(() => Permission, { primary: true })
permission!: Permission;
// **نکته:** اینجا *فقط* همین دو خصوصیت کافی است —
// از تعریف جداگانه‌ی roleId / permissionId با @PrimaryKey خودداری کن مگر بخوای نام/نوع ستون را سفارشی کنی.
}
@@ -0,0 +1,14 @@
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
import { Admin } from '../entities/admin.entity';
import { Injectable } from '@nestjs/common';
@Injectable()
export class AdminRepository extends EntityRepository<Admin> {
constructor(readonly em: EntityManager) {
super(em, Admin);
}
async findByPhoneAndrestaurantSlug(phone: string, slug: string): Promise<Admin | null> {
return this.findOne({ phone, restaurant: { slug } }, { populate: ['restaurant', 'role', 'role.permissions'] });
}
}