isSystem: false

This commit is contained in:
2025-11-24 20:39:23 +03:30
parent 26b5d2b561
commit bd8c64f7c4
4 changed files with 11 additions and 8 deletions
+3 -3
View File
@@ -51,7 +51,7 @@ export class AdminService {
}); });
await this.em.persistAndFlush(admin); await this.em.persistAndFlush(admin);
} }
const role = await this.em.findOne(Role, { id: roleId }); const role = await this.em.findOne(Role, { id: roleId, isSystem: false });
if (!role) throw new NotFoundException('Role not found'); if (!role) throw new NotFoundException('Role not found');
const restaurant = await this.em.findOne(Restaurant, { id: restId }); const restaurant = await this.em.findOne(Restaurant, { id: restId });
@@ -131,8 +131,8 @@ export class AdminService {
} }
// Update role if roleId is provided // Update role if roleId is provided
if (roleId ) { if (roleId) {
const role = await this.em.findOne(Role, { id: roleId }); const role = await this.em.findOne(Role, { id: roleId, isSystem: false });
if (!role) { if (!role) {
throw new NotFoundException('Role not found'); throw new NotFoundException('Role not found');
} }
+3 -1
View File
@@ -1,4 +1,3 @@
// role.entity.ts
import { Collection, Entity, ManyToMany, OneToMany, ManyToOne, Property } from '@mikro-orm/core'; import { Collection, Entity, ManyToMany, OneToMany, ManyToOne, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { Permission } from './permission.entity'; import { Permission } from './permission.entity';
@@ -11,6 +10,9 @@ export class Role extends BaseEntity {
@Property() @Property()
name!: string; name!: string;
@Property({ default: false })
isSystem: boolean = false;
@ManyToMany({ entity: () => Permission, pivotEntity: () => RolePermission, inversedBy: p => p.roles }) @ManyToMany({ entity: () => Permission, pivotEntity: () => RolePermission, inversedBy: p => p.roles })
permissions = new Collection<Permission>(this); permissions = new Collection<Permission>(this);
+2 -1
View File
@@ -39,6 +39,7 @@ export class RolesService {
const role = this.roleRepository.create({ const role = this.roleRepository.create({
name, name,
restaurant, restaurant,
isSystem: false,
}); });
// Add permissions if provided // Add permissions if provided
@@ -60,7 +61,7 @@ export class RolesService {
} }
async findAllGeneralAndRestaurantRoles(restId: string) { async findAllGeneralAndRestaurantRoles(restId: string) {
const where: FilterQuery<Role> = { $or: [{ restaurant: restId }, { restaurant: null }] }; const where: FilterQuery<Role> = { $or: [{ restaurant: restId }, { restaurant: null }], isSystem: false };
const roles = await this.roleRepository.find(where, { const roles = await this.roleRepository.find(where, {
orderBy: { createdAt: 'desc' }, orderBy: { createdAt: 'desc' },
+3 -3
View File
@@ -47,7 +47,7 @@ export class DatabaseSeeder extends Seeder {
// 2. Create Roles (English) // 2. Create Roles (English)
let restaurantRole = await em.findOne(Role, { name: 'restaurant' }); let restaurantRole = await em.findOne(Role, { name: 'restaurant' });
if (!restaurantRole) { if (!restaurantRole) {
const role = em.create(Role, { name: 'restaurant' }); const role = em.create(Role, { name: 'restaurant', isSystem: false });
// Add all general permissions to restaurant role (excluding special permissions) // Add all general permissions to restaurant role (excluding special permissions)
const generalPermissions = createdPermissions.filter(p => p.group === 'general'); const generalPermissions = createdPermissions.filter(p => p.group === 'general');
generalPermissions.forEach(p => role.permissions.add(p)); generalPermissions.forEach(p => role.permissions.add(p));
@@ -57,7 +57,7 @@ export class DatabaseSeeder extends Seeder {
const accountantRole = await em.findOne(Role, { name: 'accountant' }); const accountantRole = await em.findOne(Role, { name: 'accountant' });
if (!accountantRole) { if (!accountantRole) {
const role = em.create(Role, { name: 'accountant' }); const role = em.create(Role, { name: 'accountant', isSystem: false });
// Add financial permissions to accountant // Add financial permissions to accountant
const financialPerms = createdPermissions.filter( const financialPerms = createdPermissions.filter(
p => p =>
@@ -71,7 +71,7 @@ export class DatabaseSeeder extends Seeder {
const superAdminRole = await em.findOne(Role, { name: 'superAdmin' }); const superAdminRole = await em.findOne(Role, { name: 'superAdmin' });
if (!superAdminRole) { if (!superAdminRole) {
const role = em.create(Role, { name: 'superAdmin' }); const role = em.create(Role, { name: 'superAdmin', isSystem: true });
// Add restaurant management permissions to superAdmin // Add restaurant management permissions to superAdmin
const restaurantPerms = createdPermissions.filter( const restaurantPerms = createdPermissions.filter(
p => p =>