delete role bug

This commit is contained in:
2026-02-26 18:08:48 +03:30
parent b4de4a82e0
commit 7c076e32f7
2 changed files with 6 additions and 9 deletions
+1
View File
@@ -522,6 +522,7 @@ export const enum DnsRecordMessage {
export const enum RoleMessage {
NOT_FOUND = 'نقش یافت نشد',
ROLE_HAS_ADMINS = 'نقش دارای ادمین است و قابل حذف نیست',
}
export const enum SignatureMessage {
+5 -9
View File
@@ -8,6 +8,7 @@ import { EntityManager } from '@mikro-orm/postgresql';
import { CreateRoleDto } from '../dto/create-role.dto';
import { UpdateRoleDto } from '../dto/update-role.dto';
import { RolePermission } from '../entities/rolePermission.entity';
import { RoleMessage } from 'src/common/enums/message.enum';
@Injectable()
export class RolesService {
@@ -75,7 +76,7 @@ export class RolesService {
async findOneOrFail(id: string) {
const role = await this.roleRepository.findOne(
{ id },
{ populate: ['permissions', 'restaurant'] },
{ populate: ['permissions', 'restaurant','admins'] },
);
if (!role) {
throw new NotFoundException('Role not found');
@@ -117,15 +118,10 @@ export class RolesService {
}
async remove(restId: string, id: string) {
const role = await this.roleRepository.findOne(
{ id, restaurant: { id: restId } },
{ populate: ['permissions', 'restaurant'] },
);
if (!role) {
throw new NotFoundException('Role not found');
}
const role = await this.findOne(restId, id);
if (!role.admins.isEmpty()) {
throw new BadRequestException('Role has admins');
throw new BadRequestException(RoleMessage.ROLE_HAS_ADMINS);
}
// Hard delete pivot table entries (role_permissions) before soft deleting the role