forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix workspace-member deletion with existing attachments/documents (tw…
…entyhq#5232) ## Context We have a non-nullable constraint on authorId in attachments and documents, until we have soft-deletion we need to handle deletion of workspace-members and their attachments/documents. This PR introduces pre-hooks to deleteOne/deleteMany This is called when a user deletes a workspace-member from the members page Next: needs to be done on user level as well. This is called when users try to delete their own accounts. I've seen other issues such as re-creating a user with a previously used email failing.
- Loading branch information
Showing
13 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/twenty-server/src/modules/activity/repositories/comment.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; | ||
|
||
@Injectable() | ||
export class CommentRepository { | ||
constructor( | ||
private readonly workspaceDataSourceService: WorkspaceDataSourceService, | ||
) {} | ||
|
||
async deleteByAuthorId(authorId: string, workspaceId: string): Promise<void> { | ||
const dataSourceSchema = | ||
this.workspaceDataSourceService.getSchemaName(workspaceId); | ||
|
||
await this.workspaceDataSourceService.executeRawQuery( | ||
`DELETE FROM ${dataSourceSchema}."comment" WHERE "authorId" = $1`, | ||
[authorId], | ||
workspaceId, | ||
); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/twenty-server/src/modules/attachment/repositories/attachment.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; | ||
|
||
@Injectable() | ||
export class AttachmentRepository { | ||
constructor( | ||
private readonly workspaceDataSourceService: WorkspaceDataSourceService, | ||
) {} | ||
|
||
async deleteByAuthorId(authorId: string, workspaceId: string): Promise<void> { | ||
const dataSourceSchema = | ||
this.workspaceDataSourceService.getSchemaName(workspaceId); | ||
|
||
await this.workspaceDataSourceService.executeRawQuery( | ||
`DELETE FROM ${dataSourceSchema}."attachment" WHERE "authorId" = $1`, | ||
[authorId], | ||
workspaceId, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...r/src/modules/workspace-member/query-hooks/workspace-member-delete-many.pre-query.hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Injectable, MethodNotAllowedException } from '@nestjs/common'; | ||
|
||
import { WorkspacePreQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-pre-query-hook/interfaces/workspace-pre-query-hook.interface'; | ||
|
||
@Injectable() | ||
export class WorkspaceMemberDeleteManyPreQueryHook | ||
implements WorkspacePreQueryHook | ||
{ | ||
constructor() {} | ||
|
||
async execute(): Promise<void> { | ||
throw new MethodNotAllowedException('Method not allowed.'); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...er/src/modules/workspace-member/query-hooks/workspace-member-delete-one.pre-query.hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { WorkspacePreQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-pre-query-hook/interfaces/workspace-pre-query-hook.interface'; | ||
import { DeleteOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface'; | ||
|
||
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; | ||
import { CommentRepository } from 'src/modules/activity/repositories/comment.repository'; | ||
import { CommentObjectMetadata } from 'src/modules/activity/standard-objects/comment.object-metadata'; | ||
import { AttachmentRepository } from 'src/modules/attachment/repositories/attachment.repository'; | ||
import { AttachmentObjectMetadata } from 'src/modules/attachment/standard-objects/attachment.object-metadata'; | ||
|
||
@Injectable() | ||
export class WorkspaceMemberDeleteOnePreQueryHook | ||
implements WorkspacePreQueryHook | ||
{ | ||
constructor( | ||
@InjectObjectMetadataRepository(AttachmentObjectMetadata) | ||
private readonly attachmentRepository: AttachmentRepository, | ||
@InjectObjectMetadataRepository(CommentObjectMetadata) | ||
private readonly commentRepository: CommentRepository, | ||
) {} | ||
|
||
// There is no need to validate the user's access to the workspace member since we don't have permission yet. | ||
async execute( | ||
userId: string, | ||
workspaceId: string, | ||
payload: DeleteOneResolverArgs, | ||
): Promise<void> { | ||
const workspaceMemberId = payload.id; | ||
|
||
await this.attachmentRepository.deleteByAuthorId( | ||
workspaceMemberId, | ||
workspaceId, | ||
); | ||
|
||
await this.commentRepository.deleteByAuthorId( | ||
workspaceMemberId, | ||
workspaceId, | ||
); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...nty-server/src/modules/workspace-member/query-hooks/workspace-member-query-hook.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; | ||
import { CommentObjectMetadata } from 'src/modules/activity/standard-objects/comment.object-metadata'; | ||
import { AttachmentObjectMetadata } from 'src/modules/attachment/standard-objects/attachment.object-metadata'; | ||
import { WorkspaceMemberDeleteManyPreQueryHook } from 'src/modules/workspace-member/query-hooks/workspace-member-delete-many.pre-query.hook'; | ||
import { WorkspaceMemberDeleteOnePreQueryHook } from 'src/modules/workspace-member/query-hooks/workspace-member-delete-one.pre-query.hook'; | ||
|
||
@Module({ | ||
imports: [ | ||
ObjectMetadataRepositoryModule.forFeature([ | ||
AttachmentObjectMetadata, | ||
CommentObjectMetadata, | ||
]), | ||
], | ||
providers: [ | ||
{ | ||
provide: WorkspaceMemberDeleteOnePreQueryHook.name, | ||
useClass: WorkspaceMemberDeleteOnePreQueryHook, | ||
}, | ||
{ | ||
provide: WorkspaceMemberDeleteManyPreQueryHook.name, | ||
useClass: WorkspaceMemberDeleteManyPreQueryHook, | ||
}, | ||
], | ||
}) | ||
export class WorkspaceMemberQueryHookModule {} |