feat: multi-workspace followup (#4197)

* Seed UserWorkspace for existing demo/dev users

* add workspaces field to currentUser

* new token generation endpoint for switching workspace

* lint fix

* include dependency

* requested fixes

* resolver test pass

* changing defaultWorkspace and workspaceMember when switching workspaces

* tests fix

* requested changes

* delete user/workspace edge case handled

* after merge

* requested changes

* :wq!

* workspace manytoone relation

* lint fix / import fix

* gql codegen

* Fix migrations and generateJWT

* migration fix

* relations fix

---------

Co-authored-by: martmull <martmull@hotmail.fr>
This commit is contained in:
Aditya Pimpalkar
2024-03-04 15:14:04 +00:00
committed by GitHub
parent 4a0f2e8c24
commit 63d403454c
25 changed files with 363 additions and 22 deletions

View File

@ -23,8 +23,11 @@ import { assert } from 'src/utils/assert';
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
import { User } from 'src/core/user/user.entity';
import { WorkspaceMember } from 'src/core/user/dtos/workspace-member.dto';
import { UserWorkspaceService } from 'src/core/user-workspace/user-workspace.service';
import { UserService } from './services/user.service';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
const getHMACKey = (email?: string, key?: string | null) => {
if (!email || !key) return null;
@ -38,15 +41,21 @@ const getHMACKey = (email?: string, key?: string | null) => {
@Resolver(() => User)
export class UserResolver {
constructor(
@InjectRepository(User, 'core')
private readonly userRepository: Repository<User>,
private readonly userService: UserService,
private readonly userWorkspaceService: UserWorkspaceService,
private readonly environmentService: EnvironmentService,
private readonly fileUploadService: FileUploadService,
) {}
@Query(() => User)
async currentUser(@AuthUser() { id }: User) {
const user = await this.userService.findById(id, {
relations: [{ name: 'defaultWorkspace', query: {} }],
async currentUser(@AuthUser() { id }: User): Promise<User> {
const user = await this.userRepository.findOne({
where: {
id,
},
relations: ['defaultWorkspace', 'workspaces', 'workspaces.workspace'],
});
assert(user, 'User not found');