Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multi-workspace followup #4197

Merged
merged 23 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/twenty-front/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ export type User = {
createdAt: Scalars['DateTime'];
defaultAvatarUrl?: Maybe<Scalars['String']>;
defaultWorkspace: Workspace;
defaultWorkspaceId: Scalars['String'];
deletedAt?: Maybe<Scalars['DateTime']>;
disabled?: Maybe<Scalars['Boolean']>;
email: Scalars['String'];
Expand All @@ -587,7 +588,7 @@ export type User = {
supportUserHash?: Maybe<Scalars['String']>;
updatedAt: Scalars['DateTime'];
workspaceMember?: Maybe<WorkspaceMember>;
workspaces?: Maybe<Array<UserWorkspace>>;
workspaces: Array<UserWorkspace>;
};

export type UserEdge = {
Expand All @@ -611,7 +612,7 @@ export type UserWorkspace = {
updatedAt: Scalars['DateTime'];
user: User;
userId: Scalars['String'];
workspace: Workspace;
workspace?: Maybe<Workspace>;
workspaceId: Scalars['String'];
};

Expand Down Expand Up @@ -935,7 +936,7 @@ export type UploadProfilePictureMutation = { __typename?: 'Mutation', uploadProf
export type GetCurrentUserQueryVariables = Exact<{ [key: string]: never; }>;


export type GetCurrentUserQuery = { __typename?: 'Query', currentUser: { __typename?: 'User', id: string, firstName: string, lastName: string, email: string, canImpersonate: boolean, supportUserHash?: string | null, workspaceMember?: { __typename?: 'WorkspaceMember', id: string, colorScheme: string, avatarUrl?: string | null, locale: string, name: { __typename?: 'FullName', firstName: string, lastName: string } } | null, defaultWorkspace: { __typename?: 'Workspace', id: string, displayName?: string | null, logo?: string | null, domainName?: string | null, inviteHash?: string | null, allowImpersonation: boolean, subscriptionStatus: string, activationStatus: string, featureFlags?: Array<{ __typename?: 'FeatureFlag', id: string, key: string, value: boolean, workspaceId: string }> | null }, workspaces?: Array<{ __typename?: 'UserWorkspace', workspace: { __typename?: 'Workspace', id: string, displayName?: string | null, logo?: string | null, domainName?: string | null } }> | null } };
export type GetCurrentUserQuery = { __typename?: 'Query', currentUser: { __typename?: 'User', id: string, firstName: string, lastName: string, email: string, canImpersonate: boolean, supportUserHash?: string | null, workspaceMember?: { __typename?: 'WorkspaceMember', id: string, colorScheme: string, avatarUrl?: string | null, locale: string, name: { __typename?: 'FullName', firstName: string, lastName: string } } | null, defaultWorkspace: { __typename?: 'Workspace', id: string, displayName?: string | null, logo?: string | null, domainName?: string | null, inviteHash?: string | null, allowImpersonation: boolean, subscriptionStatus: string, activationStatus: string, featureFlags?: Array<{ __typename?: 'FeatureFlag', id: string, key: string, value: boolean, workspaceId: string }> | null }, workspaces: Array<{ __typename?: 'UserWorkspace', workspace?: { __typename?: 'Workspace', id: string, displayName?: string | null, logo?: string | null, domainName?: string | null } | null }> } };

export type ActivateWorkspaceMutationVariables = Exact<{
input: ActivateWorkspaceInput;
Expand Down
5 changes: 0 additions & 5 deletions packages/twenty-server/src/core/auth/auth.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ export class AuthResolver {

const result = await this.authService.verify(email);

if (result.user.id) {
result.user.workspaces =
await this.userWorkspaceService.findUserWorkspaces(result.user.id);
}

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class AuthService {
where: {
email,
},
relations: ['defaultWorkspace'],
relations: ['defaultWorkspace', 'workspaces'],
});

assert(user, "This user doesn't exist", NotFoundException);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,7 @@ export class TokenService {

assert(workspace, 'workspace doesnt exist', NotFoundException);

const userWorkspace =
await this.userWorkspaceService.checkUserWorkspaceExists(
user.id,
workspace.id,
);

assert(userWorkspace, 'cannot access workspace', ForbiddenException);
assert(workspace.users.map((u) => u.id).includes(user.id), 'user does not belong to workspace', ForbiddenException);

await this.userRepository.save({
id: user.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class UserWorkspace {
id: string;

@Field(() => User)
@ManyToOne(() => User, (user) => user.workspaceUsers, {
@ManyToOne(() => User, (user) => user.workspaces, {
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'userId' })
Expand All @@ -34,8 +34,8 @@ export class UserWorkspace {
@Column()
userId: string;

@Field(() => Workspace)
@ManyToOne(() => Workspace, (workspace) => workspace.workspaceUsers, {
@Field(() => Workspace, { nullable: true })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think one migration is missing then

@ManyToOne(() => Workspace, (workspace) => workspace.users, {
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'workspaceId' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ export class UserWorkspaceService extends TypeOrmQueryService<UserWorkspace> {
);
}

async findUserWorkspaces(userId: string): Promise<UserWorkspace[]> {
return this.userWorkspaceRepository.find({
where: {
userId,
},
relations: ['workspace'],
});
}

async checkUserWorkspaceExists(
userId: string,
workspaceId: string,
Expand Down
8 changes: 2 additions & 6 deletions packages/twenty-server/src/core/user/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ export class User {
@Field(() => WorkspaceMember, { nullable: true })
workspaceMember: WorkspaceMember;

@OneToMany(() => UserWorkspace, (userWorkspace) => userWorkspace.user, {
onDelete: 'CASCADE',
})
workspaceUsers: UserWorkspace[];

@Field(() => [UserWorkspace], { nullable: true })
@Field(() => [UserWorkspace])
@OneToMany(() => UserWorkspace, (userWorkspace) => userWorkspace.user)
workspaces: UserWorkspace[];
}
4 changes: 1 addition & 3 deletions packages/twenty-server/src/core/user/user.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ export class UserResolver {
@Query(() => User)
async currentUser(@AuthUser() { id }: User): Promise<User> {
const user = await this.userService.findById(id, {
relations: [{ name: 'defaultWorkspace', query: {} }],
relations: [{ name: 'defaultWorkspace', query: {} }, { name: 'workspaces', query: {} }],
});

assert(user, 'User not found');

user.workspaces = await this.userWorkspaceService.findUserWorkspaces(id);

return user;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {

await this.userWorkspaceRepository.delete({ workspaceId: id });

const users = await this.userRepository.find({
where: {
defaultWorkspaceId: workspace.id,
},
});

await this.userRepository.remove(users);

await this.workspaceManagerService.delete(id);
if (shouldDeleteCoreWorkspace) {
await this.workspaceRepository.delete(id);
Expand Down
5 changes: 0 additions & 5 deletions packages/twenty-server/src/core/workspace/workspace.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ export class Workspace {
@OneToMany(() => User, (user) => user.defaultWorkspace)
users: User[];

@OneToMany(() => UserWorkspace, (userWorkspace) => userWorkspace.workspace, {
onDelete: 'CASCADE',
})
workspaceUsers: UserWorkspace[];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think the OneToMany with userWorkspace entities is missing, but we might not call that field users as it is already taken for the OneToMany with user entities
Let's call it workspaceUsers

@Field()
@Column({ default: true })
allowImpersonation: boolean;
Expand Down