role perms
This commit is contained in:
@@ -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 {}
|
||||
|
||||
@@ -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
@@ -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);
|
||||
}
|
||||
Executable
+22
@@ -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'] });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user