Skip to content

Commit

Permalink
fix(api): Add NotFound exception on passing an invalid roleId while i…
Browse files Browse the repository at this point in the history
…nviting user in workspace (keyshade-xyz#408)
  • Loading branch information
PriyobrotoKar authored and rajdip-b committed Sep 5, 2024
1 parent 3e114d9 commit ab441db
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions apps/api/src/workspace/service/workspace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ab441db

Please sign in to comment.