feat: spaces - WIP

This commit is contained in:
Philipinho
2024-02-28 02:39:46 +00:00
parent 1d620eba49
commit 40251aef7d
32 changed files with 512 additions and 119 deletions

View File

@ -11,7 +11,7 @@ import {
} from '@nestjs/common';
import { WorkspaceService } from '../services/workspace.service';
import { FastifyRequest } from 'fastify';
import { JwtGuard } from '../../auth/guards/JwtGuard';
import { JwtGuard } from '../../auth/guards/jwt.guard';
import { UpdateWorkspaceDto } from '../dto/update-workspace.dto';
import { CreateWorkspaceDto } from '../dto/create-workspace.dto';
import { DeleteWorkspaceDto } from '../dto/delete-workspace.dto';
@ -24,6 +24,17 @@ import { AddWorkspaceUserDto } from '../dto/add-workspace-user.dto';
export class WorkspaceController {
constructor(private readonly workspaceService: WorkspaceService) {}
@HttpCode(HttpStatus.OK)
@Post('test')
async test(
@Req() req: FastifyRequest,
//@Body() createWorkspaceDto: CreateWorkspaceDto,
) {
//const jwtPayload = req['user'];
// const userId = jwtPayload.sub;
// return this.workspaceService.createOrJoinWorkspace();
}
@HttpCode(HttpStatus.OK)
@Post('create')
async createWorkspace(

View File

@ -14,12 +14,14 @@ import { generateHostname } from '../workspace.util';
import { UpdateWorkspaceDto } from '../dto/update-workspace.dto';
import { DeleteWorkspaceDto } from '../dto/delete-workspace.dto';
import { UpdateWorkspaceUserRoleDto } from '../dto/update-workspace-user-role.dto';
import { SpaceService } from '../../space/space.service';
@Injectable()
export class WorkspaceService {
constructor(
private workspaceRepository: WorkspaceRepository,
private workspaceUserRepository: WorkspaceUserRepository,
private spaceService: SpaceService,
) {}
async findById(workspaceId: string): Promise<Workspace> {
@ -30,6 +32,45 @@ export class WorkspaceService {
return this.workspaceRepository.save(workspace);
}
async createOrJoinWorkspace(userId) {
// context:
// only create workspace if it is not a signup to an existing workspace
// OS version is limited to one workspace.
// if there is no existing workspace, create a new workspace
// and make first user owner/admin
// check if workspace already exists in the db
// if not create default, if yes add the user to existing workspace if signup is open.
const workspaceCount = await this.workspaceRepository.count();
if (workspaceCount === 0) {
// create first workspace
// add user to workspace as admin
const newWorkspace = await this.create(userId);
await this.addUserToWorkspace(userId, newWorkspace.id, 'owner');
// maybe create default space and add user to it too.
const newSpace = await this.spaceService.create(userId, newWorkspace.id);
await this.spaceService.addUserToSpace(userId, newSpace.id, 'owner');
} else {
//TODO: accept role as param
// if no role is passed use default new member role found in workspace settings
// fetch the oldest workspace and add user to it
const firstWorkspace = await this.workspaceRepository.find({
order: {
createdAt: 'ASC',
},
take: 1,
});
await this.addUserToWorkspace(userId, firstWorkspace[0].id, 'member');
// get workspace
// if there is a default space, we should add new users to it.
}
}
async create(
userId: string,
createWorkspaceDto?: CreateWorkspaceDto,
@ -50,7 +91,6 @@ export class WorkspaceService {
}
workspace = await this.workspaceRepository.save(workspace);
await this.addUserToWorkspace(userId, workspace.id, 'owner');
return workspace;
}
@ -151,6 +191,10 @@ export class WorkspaceService {
relations: ['workspace'],
});
if (!userWorkspace) {
throw new NotFoundException('No workspace found for this user');
}
return userWorkspace.workspace;
}

View File

@ -8,11 +8,13 @@ import { WorkspaceUser } from './entities/workspace-user.entity';
import { WorkspaceInvitation } from './entities/workspace-invitation.entity';
import { WorkspaceUserRepository } from './repositories/workspace-user.repository';
import { AuthModule } from '../auth/auth.module';
import { SpaceModule } from '../space/space.module';
@Module({
imports: [
TypeOrmModule.forFeature([Workspace, WorkspaceUser, WorkspaceInvitation]),
AuthModule,
SpaceModule,
],
controllers: [WorkspaceController],
providers: [WorkspaceService, WorkspaceRepository, WorkspaceUserRepository],