check for existing workspace user

This commit is contained in:
Philipinho
2023-09-05 17:50:43 +01:00
parent 60c6452f9b
commit add9303249
2 changed files with 21 additions and 12 deletions

View File

@ -51,8 +51,7 @@ export class WorkspaceController {
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Post('delete') @Post('delete')
async deleteWorkspace(@Body() deleteWorkspaceDto: DeleteWorkspaceDto, async deleteWorkspace(@Body() deleteWorkspaceDto: DeleteWorkspaceDto) {
) {
return this.workspaceService.delete(deleteWorkspaceDto); return this.workspaceService.delete(deleteWorkspaceDto);
} }
@ -68,7 +67,7 @@ export class WorkspaceController {
} }
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Post('member') @Post('members/add')
async addWorkspaceMember( async addWorkspaceMember(
@Req() req: FastifyRequest, @Req() req: FastifyRequest,
@Body() addWorkspaceUserDto: AddWorkspaceUserDto, @Body() addWorkspaceUserDto: AddWorkspaceUserDto,
@ -86,7 +85,7 @@ export class WorkspaceController {
} }
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Delete('member') @Delete('members/delete')
async removeWorkspaceMember( async removeWorkspaceMember(
@Req() req: FastifyRequest, @Req() req: FastifyRequest,
@Body() removeWorkspaceUserDto: RemoveWorkspaceUserDto, @Body() removeWorkspaceUserDto: RemoveWorkspaceUserDto,
@ -103,7 +102,7 @@ export class WorkspaceController {
} }
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Post('member/role') @Post('members/role')
async updateWorkspaceMemberRole( async updateWorkspaceMemberRole(
@Req() req: FastifyRequest, @Req() req: FastifyRequest,
@Body() workspaceUserRoleDto: UpdateWorkspaceUserRoleDto, @Body() workspaceUserRoleDto: UpdateWorkspaceUserRoleDto,

View File

@ -22,6 +22,10 @@ export class WorkspaceService {
private workspaceUserRepository: WorkspaceUserRepository, private workspaceUserRepository: WorkspaceUserRepository,
) {} ) {}
async findById(workspaceId: string): Promise<Workspace> {
return await this.workspaceRepository.findById(workspaceId);
}
async create( async create(
userId: string, userId: string,
createWorkspaceDto?: CreateWorkspaceDto, createWorkspaceDto?: CreateWorkspaceDto,
@ -63,7 +67,7 @@ export class WorkspaceService {
return this.workspaceRepository.save(workspace); return this.workspaceRepository.save(workspace);
} }
async delete(deleteWorkspaceDto: DeleteWorkspaceDto) { async delete(deleteWorkspaceDto: DeleteWorkspaceDto): Promise<void> {
const workspace = await this.workspaceRepository.findById( const workspace = await this.workspaceRepository.findById(
deleteWorkspaceDto.workspaceId, deleteWorkspaceDto.workspaceId,
); );
@ -71,7 +75,9 @@ export class WorkspaceService {
throw new NotFoundException('Workspace not found'); throw new NotFoundException('Workspace not found');
} }
return 0; //TODO
// remove all existing users from workspace
// delete workspace
} }
async addUserToWorkspace( async addUserToWorkspace(
@ -79,6 +85,14 @@ export class WorkspaceService {
workspaceId: string, workspaceId: string,
role: string, role: string,
): Promise<WorkspaceUser> { ): Promise<WorkspaceUser> {
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(); const workspaceUser = new WorkspaceUser();
workspaceUser.userId = userId; workspaceUser.userId = userId;
workspaceUser.workspaceId = workspaceId; workspaceUser.workspaceId = workspaceId;
@ -104,7 +118,7 @@ export class WorkspaceService {
} }
workspaceUser.role = workspaceUserRoleDto.role; 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); return this.workspaceUserRepository.save(workspaceUser);
} }
@ -127,10 +141,6 @@ export class WorkspaceService {
}); });
} }
async findById(workspaceId: string): Promise<Workspace> {
return await this.workspaceRepository.findById(workspaceId);
}
async getUserCurrentWorkspace(userId: string): Promise<Workspace> { async getUserCurrentWorkspace(userId: string): Promise<Workspace> {
// TODO: use workspaceId and fetch workspace based on the id // TODO: use workspaceId and fetch workspace based on the id
// we currently assume the user belongs to one workspace // we currently assume the user belongs to one workspace