delete role bug
This commit is contained in:
@@ -522,6 +522,7 @@ export const enum DnsRecordMessage {
|
||||
|
||||
export const enum RoleMessage {
|
||||
NOT_FOUND = 'نقش یافت نشد',
|
||||
ROLE_HAS_ADMINS = 'نقش دارای ادمین است و قابل حذف نیست',
|
||||
}
|
||||
|
||||
export const enum SignatureMessage {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user