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)
@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,

View File

@ -22,6 +22,10 @@ export class WorkspaceService {
private workspaceUserRepository: WorkspaceUserRepository,
) {}
async findById(workspaceId: string): Promise<Workspace> {
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<void> {
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<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();
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<Workspace> {
return await this.workspaceRepository.findById(workspaceId);
}
async getUserCurrentWorkspace(userId: string): Promise<Workspace> {
// TODO: use workspaceId and fetch workspace based on the id
// we currently assume the user belongs to one workspace