From add930324966810339aeb412850c56933f6c91c5 Mon Sep 17 00:00:00 2001 From: Philipinho <16838612+Philipinho@users.noreply.github.com> Date: Tue, 5 Sep 2023 17:50:43 +0100 Subject: [PATCH] check for existing workspace user --- .../controllers/workspace.controller.ts | 9 ++++--- .../workspace/services/workspace.service.ts | 24 +++++++++++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/server/src/core/workspace/controllers/workspace.controller.ts b/server/src/core/workspace/controllers/workspace.controller.ts index b53655dc..35a48e57 100644 --- a/server/src/core/workspace/controllers/workspace.controller.ts +++ b/server/src/core/workspace/controllers/workspace.controller.ts @@ -51,8 +51,7 @@ export class WorkspaceController { @HttpCode(HttpStatus.OK) @Post('delete') - async deleteWorkspace(@Body() deleteWorkspaceDto: DeleteWorkspaceDto, - ) { + async deleteWorkspace(@Body() deleteWorkspaceDto: DeleteWorkspaceDto) { return this.workspaceService.delete(deleteWorkspaceDto); } @@ -68,7 +67,7 @@ export class WorkspaceController { } @HttpCode(HttpStatus.OK) - @Post('member') + @Post('members/add') async addWorkspaceMember( @Req() req: FastifyRequest, @Body() addWorkspaceUserDto: AddWorkspaceUserDto, @@ -86,7 +85,7 @@ export class WorkspaceController { } @HttpCode(HttpStatus.OK) - @Delete('member') + @Delete('members/delete') async removeWorkspaceMember( @Req() req: FastifyRequest, @Body() removeWorkspaceUserDto: RemoveWorkspaceUserDto, @@ -103,7 +102,7 @@ export class WorkspaceController { } @HttpCode(HttpStatus.OK) - @Post('member/role') + @Post('members/role') async updateWorkspaceMemberRole( @Req() req: FastifyRequest, @Body() workspaceUserRoleDto: UpdateWorkspaceUserRoleDto, diff --git a/server/src/core/workspace/services/workspace.service.ts b/server/src/core/workspace/services/workspace.service.ts index ce9a9950..606e1af1 100644 --- a/server/src/core/workspace/services/workspace.service.ts +++ b/server/src/core/workspace/services/workspace.service.ts @@ -22,6 +22,10 @@ export class WorkspaceService { private workspaceUserRepository: WorkspaceUserRepository, ) {} + async findById(workspaceId: string): Promise { + return await this.workspaceRepository.findById(workspaceId); + } + async create( userId: string, createWorkspaceDto?: CreateWorkspaceDto, @@ -63,7 +67,7 @@ export class WorkspaceService { return this.workspaceRepository.save(workspace); } - async delete(deleteWorkspaceDto: DeleteWorkspaceDto) { + async delete(deleteWorkspaceDto: DeleteWorkspaceDto): Promise { const workspace = await this.workspaceRepository.findById( deleteWorkspaceDto.workspaceId, ); @@ -71,7 +75,9 @@ export class WorkspaceService { throw new NotFoundException('Workspace not found'); } - return 0; + //TODO + // remove all existing users from workspace + // delete workspace } async addUserToWorkspace( @@ -79,6 +85,14 @@ export class WorkspaceService { workspaceId: string, role: string, ): Promise { + const existingWorkspaceUser = await this.workspaceUserRepository.findOne({ + where: { userId: userId, workspaceId: workspaceId }, + }); + + if (existingWorkspaceUser) { + throw new BadRequestException('User already added to this workspace'); + } + const workspaceUser = new WorkspaceUser(); workspaceUser.userId = userId; workspaceUser.workspaceId = workspaceId; @@ -104,7 +118,7 @@ export class WorkspaceService { } workspaceUser.role = workspaceUserRoleDto.role; - // if there is only one workspace owner, prevent the role change + // TODO: if there is only one workspace owner, prevent the role change return this.workspaceUserRepository.save(workspaceUser); } @@ -127,10 +141,6 @@ export class WorkspaceService { }); } - async findById(workspaceId: string): Promise { - return await this.workspaceRepository.findById(workspaceId); - } - async getUserCurrentWorkspace(userId: string): Promise { // TODO: use workspaceId and fetch workspace based on the id // we currently assume the user belongs to one workspace