fix:bug --> get admin permission
This commit is contained in:
@@ -40,7 +40,7 @@ export class AdminRepository extends EntityRepository<Admin> {
|
||||
// Ensure all roles are populated
|
||||
await adminRole.admin.roles.loadItems();
|
||||
for (const role of adminRole.admin.roles.getItems()) {
|
||||
if (role.role && !role.role.permissions.isInitialized()) {
|
||||
if (role.role && role.role.permissions && !role.role.permissions.isInitialized()) {
|
||||
await role.role.permissions.loadItems();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import { PlanEnum } from '../interface/plan.interface';
|
||||
import { Admin } from '../../admin/entities/admin.entity';
|
||||
import { AdminRole } from '../../admin/entities/adminRole.entity';
|
||||
import { Role } from '../../roles/entities/role.entity';
|
||||
import { normalizePhone } from 'src/modules/utils/phone.util';
|
||||
|
||||
@Injectable()
|
||||
export class RestaurantsService {
|
||||
@@ -52,22 +53,21 @@ export class RestaurantsService {
|
||||
throw new BadRequestException(`Role not found for plan: ${dto.plan}`);
|
||||
}
|
||||
|
||||
// Create admin (using a default phone number for now - this could be made configurable)
|
||||
const adminPhone = `admin_${dto.slug}@system.local`; // Temporary phone format for system-generated admins
|
||||
let admin = await em.findOne(Admin, { phone: adminPhone });
|
||||
const normalizedPhone = normalizePhone(dto.phone);
|
||||
let admin = await em.findOne(Admin, { phone: normalizedPhone });
|
||||
if (!admin) {
|
||||
admin = em.create(Admin, {
|
||||
phone: adminPhone,
|
||||
firstName: 'مدیر',
|
||||
lastName: dto.name,
|
||||
phone: normalizedPhone,
|
||||
firstName: 'نام مدیر',
|
||||
lastName: 'نام خانوادگی مدیر',
|
||||
});
|
||||
}
|
||||
|
||||
// Create admin role relationship
|
||||
const adminRole = em.create(AdminRole, {
|
||||
admin: admin,
|
||||
role: role,
|
||||
restaurant: restaurant,
|
||||
admin,
|
||||
role,
|
||||
restaurant,
|
||||
});
|
||||
|
||||
// Persist all entities
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { EntityRepository } from '@mikro-orm/core';
|
||||
import { EntityManager, EntityRepository } from '@mikro-orm/core';
|
||||
import { Permission } from '../entities/permission.entity';
|
||||
import { CacheService } from 'src/modules/utils/cache.service';
|
||||
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
|
||||
import { AdminRole } from 'src/modules/admin/entities/adminRole.entity';
|
||||
|
||||
@Injectable()
|
||||
export class PermissionsService {
|
||||
@@ -14,7 +15,8 @@ export class PermissionsService {
|
||||
private readonly permissionRepository: EntityRepository<Permission>,
|
||||
private readonly cacheService: CacheService,
|
||||
private readonly adminRepository: AdminRepository,
|
||||
) {}
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
async findAll() {
|
||||
const permissions = await this.permissionRepository.findAll();
|
||||
@@ -76,33 +78,19 @@ export class PermissionsService {
|
||||
}
|
||||
|
||||
async getAdminFullPermissions(adminId: string, restId: string): Promise<Permission[]> {
|
||||
const admin = await this.adminRepository.findOne(
|
||||
{ id: adminId, roles: { restaurant: { id: restId } } },
|
||||
{ populate: ['roles', 'roles.role', 'roles.role.permissions'] },
|
||||
);
|
||||
|
||||
if (!admin || !admin.roles) {
|
||||
return [];
|
||||
const listOfPermissions = []
|
||||
const adminRoles = await this.em.findOne(AdminRole, {
|
||||
admin: {
|
||||
id: adminId,
|
||||
},
|
||||
restaurant: {
|
||||
id: restId,
|
||||
},
|
||||
}, { populate: ['role', 'role.permissions'] });
|
||||
if (adminRoles) {
|
||||
listOfPermissions.push(...adminRoles.role.permissions.getItems());
|
||||
}
|
||||
|
||||
// Ensure roles collection is loaded
|
||||
await admin.roles.loadItems();
|
||||
|
||||
// Extract permission names as array of strings
|
||||
const permissions = await Promise.all(
|
||||
admin.roles
|
||||
.getItems()
|
||||
.filter(r => r.role) // Filter out any null/undefined roles
|
||||
.map(async r => {
|
||||
// Ensure permissions collection is initialized
|
||||
if (!r.role.permissions.isInitialized()) {
|
||||
await r.role.permissions.loadItems();
|
||||
}
|
||||
return r.role.permissions.getItems();
|
||||
}),
|
||||
);
|
||||
|
||||
return permissions.flat();
|
||||
return listOfPermissions.map(permission => permission);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user