fix bug in role module
This commit is contained in:
@@ -90,7 +90,7 @@ export class PermissionsService {
|
|||||||
if (adminRoles) {
|
if (adminRoles) {
|
||||||
listOfPermissions.push(...adminRoles.role.permissions.getItems());
|
listOfPermissions.push(...adminRoles.role.permissions.getItems());
|
||||||
}
|
}
|
||||||
return listOfPermissions.map(permission => permission);
|
return listOfPermissions
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -22,19 +22,17 @@ export class RolesService {
|
|||||||
async createRestaurantRole(dto: CreateRoleDto, restId: string) {
|
async createRestaurantRole(dto: CreateRoleDto, restId: string) {
|
||||||
const { name, permissionIds } = dto;
|
const { name, permissionIds } = dto;
|
||||||
|
|
||||||
|
const restaurant = await this.em.findOne(Restaurant, { id: restId });
|
||||||
|
|
||||||
|
if (!restaurant) {
|
||||||
|
throw new NotFoundException('Restaurant not found');
|
||||||
|
}
|
||||||
// Check if role already exists
|
// Check if role already exists
|
||||||
const existing = await this.roleRepository.findOne({ name, restaurant: restId ? { id: restId } : null });
|
const existing = await this.roleRepository.findOne({ name, restaurant });
|
||||||
if (existing) {
|
if (existing) {
|
||||||
throw new BadRequestException('Role with this name already exists for the restaurant');
|
throw new BadRequestException('Role with this name already exists for the restaurant');
|
||||||
}
|
}
|
||||||
|
|
||||||
let restaurant: Restaurant | null = null;
|
|
||||||
if (restId) {
|
|
||||||
restaurant = await this.em.findOne(Restaurant, { id: restId });
|
|
||||||
if (!restaurant) {
|
|
||||||
throw new NotFoundException('Restaurant not found');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const role = this.roleRepository.create({
|
const role = this.roleRepository.create({
|
||||||
name,
|
name,
|
||||||
@@ -67,8 +65,16 @@ export class RolesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findOne(restId: string, id: string) {
|
async findOne(restId: string, id: string) {
|
||||||
|
const role = await this.findOneOrFail(id);
|
||||||
|
if (role.restaurant && role.restaurant.id !== restId) {
|
||||||
|
throw new NotFoundException('Role not found');
|
||||||
|
}
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
async findOneOrFail(id: string) {
|
||||||
const role = await this.roleRepository.findOne(
|
const role = await this.roleRepository.findOne(
|
||||||
{ id, restaurant: { id: restId } },
|
{ id },
|
||||||
{ populate: ['permissions', 'restaurant'] },
|
{ populate: ['permissions', 'restaurant'] },
|
||||||
);
|
);
|
||||||
if (!role) {
|
if (!role) {
|
||||||
@@ -78,13 +84,7 @@ export class RolesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async update(restId: string, id: string, dto: UpdateRoleDto) {
|
async update(restId: string, id: string, dto: UpdateRoleDto) {
|
||||||
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 (dto.name) {
|
if (dto.name) {
|
||||||
role.name = dto.name;
|
role.name = dto.name;
|
||||||
|
|||||||
Reference in New Issue
Block a user