Skip to content

Commit

Permalink
test: Refactored workspace e2e tests (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b authored May 27, 2024
1 parent 9266788 commit 5d094d5
Show file tree
Hide file tree
Showing 2 changed files with 299 additions and 182 deletions.
39 changes: 38 additions & 1 deletion apps/api/src/workspace/service/workspace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ export class WorkspaceService {
`User ${userId} is not a member of workspace ${workspace.name} (${workspace.id})`
)

const workspaceAdminRole = await this.getWorkspaceAdminRole(workspaceId)

// Check if the admin role is tried to be assigned to the user
if (roleIds.includes(workspaceAdminRole.id)) {
throw new BadRequestException(`Admin role cannot be assigned to the user`)
}

// Update the role of the user
const membership = await this.prisma.workspaceMember.findUnique({
where: {
Expand All @@ -396,6 +403,7 @@ export class WorkspaceService {
}
})

// Create new associations
const createNewAssociations =
this.prisma.workspaceMemberRoleAssociation.createMany({
data: roleIds.map((roleId) => ({
Expand Down Expand Up @@ -837,12 +845,40 @@ export class WorkspaceService {
)
}

private async getWorkspaceAdminRole(
workspaceId: Workspace['id']
): Promise<WorkspaceRole> {
const adminRole = await this.prisma.workspaceRole.findFirst({
where: {
hasAdminAuthority: true,
workspaceId
}
})

if (!adminRole) {
throw new InternalServerErrorException(
`Admin role not found for workspace ${workspaceId}`
)
}

return adminRole
}

private async addMembersToWorkspace(
workspace: Workspace,
currentUser: User,
members: WorkspaceMemberDTO[]
) {
const workspaceAdminRole = await this.getWorkspaceAdminRole(workspace.id)

for (const member of members) {
// Check if the admin role is tried to be assigned to the user
if (member.roleIds.includes(workspaceAdminRole.id)) {
throw new BadRequestException(
`Admin role cannot be assigned to the user`
)
}

const memberUser: User | null = await this.prisma.user.findUnique({
where: {
email: member.email
Expand Down Expand Up @@ -903,7 +939,8 @@ export class WorkspaceService {
const createMember = this.prisma.user.create({
data: {
id: userId,
email: member.email
email: member.email,
isOnboardingFinished: false
}
})

Expand Down
Loading

0 comments on commit 5d094d5

Please sign in to comment.