From ab441dbd3f60f2deae36465653c7665296c453d7 Mon Sep 17 00:00:00 2001 From: Priyobroto Kar <104967639+PriyobrotoKar@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:21:56 +0530 Subject: [PATCH] fix(api): Add NotFound exception on passing an invalid roleId while inviting user in workspace (#408) --- .../workspace/service/workspace.service.ts | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/apps/api/src/workspace/service/workspace.service.ts b/apps/api/src/workspace/service/workspace.service.ts index 74a6489f..90535149 100644 --- a/apps/api/src/workspace/service/workspace.service.ts +++ b/apps/api/src/workspace/service/workspace.service.ts @@ -414,6 +414,17 @@ export class WorkspaceService { } }) + const invalidRoles = await this.findInvalidWorkspaceRoles( + workspace.id, + roleIds + ) + + if (invalidRoles.length > 0) { + throw new NotFoundException( + `Workspace ${workspace.name} (${workspace.id}) does not have roles ${invalidRoles.join(', ')}` + ) + } + // Create new associations const createNewAssociations = this.prisma.workspaceMemberRoleAssociation.createMany({ @@ -980,6 +991,17 @@ export class WorkspaceService { ) } + const invalidRoles = await this.findInvalidWorkspaceRoles( + workspace.id, + member.roleIds + ) + + if (invalidRoles.length > 0) { + throw new NotFoundException( + `Workspace ${workspace.name} (${workspace.id}) does not have roles ${invalidRoles.join(', ')}` + ) + } + // Create the workspace membership const createMembership = this.prisma.workspaceMember.create({ data: { @@ -1045,6 +1067,26 @@ export class WorkspaceService { } } + private async findInvalidWorkspaceRoles( + workspaceId: string, + roleIds: string[] + ) { + const roles = await this.prisma.workspaceRole.findMany({ + where: { + id: { + in: roleIds + }, + workspaceId: workspaceId + } + }) + + const roleIdSet = new Set(roles.map((role) => role.id)) + + const invalidRoles = roleIds.filter((id) => !roleIdSet.has(id)) + + return invalidRoles + } + private async memberExistsInWorkspace( workspaceId: string, userId: string