From f4ffcd254a34bc56a4cb7bae5c7a926ee469c47a Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Thu, 26 Feb 2026 17:42:38 +0330 Subject: [PATCH] fix bug in role module --- .../roles/providers/permissions.service.ts | 2 +- src/modules/roles/providers/roles.service.ts | 32 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/modules/roles/providers/permissions.service.ts b/src/modules/roles/providers/permissions.service.ts index 8e96fa4..b3f9b41 100644 --- a/src/modules/roles/providers/permissions.service.ts +++ b/src/modules/roles/providers/permissions.service.ts @@ -90,7 +90,7 @@ export class PermissionsService { if (adminRoles) { listOfPermissions.push(...adminRoles.role.permissions.getItems()); } - return listOfPermissions.map(permission => permission); + return listOfPermissions } /** diff --git a/src/modules/roles/providers/roles.service.ts b/src/modules/roles/providers/roles.service.ts index 3182035..7bb03fd 100644 --- a/src/modules/roles/providers/roles.service.ts +++ b/src/modules/roles/providers/roles.service.ts @@ -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;