delete role bug
This commit is contained in:
@@ -522,6 +522,7 @@ export const enum DnsRecordMessage {
|
|||||||
|
|
||||||
export const enum RoleMessage {
|
export const enum RoleMessage {
|
||||||
NOT_FOUND = 'نقش یافت نشد',
|
NOT_FOUND = 'نقش یافت نشد',
|
||||||
|
ROLE_HAS_ADMINS = 'نقش دارای ادمین است و قابل حذف نیست',
|
||||||
}
|
}
|
||||||
|
|
||||||
export const enum SignatureMessage {
|
export const enum SignatureMessage {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { EntityManager } from '@mikro-orm/postgresql';
|
|||||||
import { CreateRoleDto } from '../dto/create-role.dto';
|
import { CreateRoleDto } from '../dto/create-role.dto';
|
||||||
import { UpdateRoleDto } from '../dto/update-role.dto';
|
import { UpdateRoleDto } from '../dto/update-role.dto';
|
||||||
import { RolePermission } from '../entities/rolePermission.entity';
|
import { RolePermission } from '../entities/rolePermission.entity';
|
||||||
|
import { RoleMessage } from 'src/common/enums/message.enum';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RolesService {
|
export class RolesService {
|
||||||
@@ -75,7 +76,7 @@ export class RolesService {
|
|||||||
async findOneOrFail(id: string) {
|
async findOneOrFail(id: string) {
|
||||||
const role = await this.roleRepository.findOne(
|
const role = await this.roleRepository.findOne(
|
||||||
{ id },
|
{ id },
|
||||||
{ populate: ['permissions', 'restaurant'] },
|
{ populate: ['permissions', 'restaurant','admins'] },
|
||||||
);
|
);
|
||||||
if (!role) {
|
if (!role) {
|
||||||
throw new NotFoundException('Role not found');
|
throw new NotFoundException('Role not found');
|
||||||
@@ -117,15 +118,10 @@ export class RolesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async remove(restId: string, id: string) {
|
async remove(restId: string, id: string) {
|
||||||
const role = await this.roleRepository.findOne(
|
const role = await this.findOne(restId, id);
|
||||||
{ id, restaurant: { id: restId } },
|
|
||||||
{ populate: ['permissions', 'restaurant'] },
|
|
||||||
);
|
|
||||||
if (!role) {
|
|
||||||
throw new NotFoundException('Role not found');
|
|
||||||
}
|
|
||||||
if (!role.admins.isEmpty()) {
|
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
|
// Hard delete pivot table entries (role_permissions) before soft deleting the role
|
||||||
|
|||||||
Reference in New Issue
Block a user