fix bug in role module
This commit is contained in:
@@ -90,7 +90,7 @@ export class PermissionsService {
|
||||
if (adminRoles) {
|
||||
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) {
|
||||
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
|
||||
const existing = await this.roleRepository.findOne({ name, restaurant: restId ? { id: restId } : null });
|
||||
const existing = await this.roleRepository.findOne({ name, restaurant });
|
||||
if (existing) {
|
||||
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({
|
||||
name,
|
||||
@@ -67,8 +65,16 @@ export class RolesService {
|
||||
}
|
||||
|
||||
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(
|
||||
{ id, restaurant: { id: restId } },
|
||||
{ id },
|
||||
{ populate: ['permissions', 'restaurant'] },
|
||||
);
|
||||
if (!role) {
|
||||
@@ -78,13 +84,7 @@ export class RolesService {
|
||||
}
|
||||
|
||||
async update(restId: string, id: string, dto: UpdateRoleDto) {
|
||||
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 (dto.name) {
|
||||
role.name = dto.name;
|
||||
|
||||
Reference in New Issue
Block a user