Return user and with current workspace

* Create endpoint to return user and their workspace
* Only return auth tokens on login/signup
* Allow nullable workspace name
This commit is contained in:
Philipinho
2023-08-26 23:38:14 +01:00
parent da8ee065df
commit 3e7c2de9a4
8 changed files with 85 additions and 28 deletions
@@ -27,7 +27,7 @@ export class Workspace {
@Column({ length: 255, nullable: true })
logo: string;
@Column({ length: 255, unique: true })
@Column({ length: 255, nullable: true, unique: true })
hostname: string;
@Column({ length: 255, nullable: true })
@@ -7,4 +7,8 @@ export class WorkspaceRepository extends Repository<Workspace> {
constructor(private dataSource: DataSource) {
super(Workspace, dataSource.createEntityManager());
}
async findById(workspaceId: string) {
return this.findOneBy({ id: workspaceId });
}
}
@@ -16,15 +16,21 @@ export class WorkspaceService {
) {}
async create(
createWorkspaceDto: CreateWorkspaceDto,
userId: string,
createWorkspaceDto?: CreateWorkspaceDto,
): Promise<Workspace> {
let workspace: Workspace = plainToInstance(Workspace, createWorkspaceDto);
let workspace: Workspace;
if (createWorkspaceDto) {
workspace = plainToInstance(Workspace, createWorkspaceDto);
} else {
workspace = new Workspace();
}
workspace.inviteCode = uuid();
workspace.creatorId = userId;
if (!workspace.hostname?.trim()) {
if (workspace.name && !workspace.hostname?.trim()) {
workspace.hostname = generateHostname(createWorkspaceDto.name);
}
@@ -46,4 +52,33 @@ export class WorkspaceService {
return this.workspaceUserRepository.save(workspaceUser);
}
async findById(workspaceId: string): Promise<Workspace> {
return await this.workspaceRepository.findById(workspaceId);
}
async getUserCurrentWorkspace(
userId: string,
workspaceId?: string,
): Promise<Workspace> {
// TODO: use workspaceId and fetch workspace based on the id
// we currently assume the user belongs to one workspace
const userWorkspace = await this.workspaceUserRepository.findOne({
where: { userId: userId },
relations: ['workspace'],
});
return userWorkspace.workspace;
}
async userWorkspaces(userId: string): Promise<Workspace[]> {
const workspaces = await this.workspaceUserRepository.find({
where: { userId: userId },
relations: ['workspace'],
});
return workspaces.map(
(userWorkspace: WorkspaceUser) => userWorkspace.workspace,
);
}
}