-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: message cleaner drop repository (#6052)
This PR use the new `TwentyORM` for the message-cleaner module by using the new injection system with `@InjectWorkspaceRepository`.
- Loading branch information
Showing
4 changed files
with
72 additions
and
23 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
77 changes: 60 additions & 17 deletions
77
...erver/src/modules/messaging/message-cleaner/services/messaging-message-cleaner.service.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 |
---|---|---|
@@ -1,40 +1,83 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; | ||
import { MessageThreadRepository } from 'src/modules/messaging/common/repositories/message-thread.repository'; | ||
import { MessageRepository } from 'src/modules/messaging/common/repositories/message.repository'; | ||
import { EntityManager, IsNull } from 'typeorm'; | ||
|
||
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator'; | ||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository'; | ||
import { MessageThreadWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-thread.workspace-entity'; | ||
import { MessageWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message.workspace-entity'; | ||
import { deleteUsingPagination } from 'src/modules/messaging/message-cleaner/utils/delete-using-pagination.util'; | ||
|
||
@Injectable() | ||
export class MessagingMessageCleanerService { | ||
constructor( | ||
@InjectObjectMetadataRepository(MessageWorkspaceEntity) | ||
private readonly messageRepository: MessageRepository, | ||
@InjectObjectMetadataRepository(MessageThreadWorkspaceEntity) | ||
private readonly messageThreadRepository: MessageThreadRepository, | ||
@InjectWorkspaceRepository(MessageWorkspaceEntity) | ||
private readonly messageRepository: WorkspaceRepository<MessageWorkspaceEntity>, | ||
@InjectWorkspaceRepository(MessageThreadWorkspaceEntity) | ||
private readonly messageThreadRepository: WorkspaceRepository<MessageThreadWorkspaceEntity>, | ||
) {} | ||
|
||
public async cleanWorkspaceThreads(workspaceId: string) { | ||
await deleteUsingPagination( | ||
workspaceId, | ||
500, | ||
this.messageRepository.getNonAssociatedMessageIdsPaginated.bind( | ||
this.messageRepository, | ||
), | ||
this.messageRepository.deleteByIds.bind(this.messageRepository), | ||
async ( | ||
limit: number, | ||
offset: number, | ||
workspaceId: string, | ||
transactionManager?: EntityManager, | ||
) => { | ||
const nonAssociatedMessages = await this.messageRepository.find( | ||
{ | ||
where: { | ||
messageChannelMessageAssociations: IsNull(), | ||
}, | ||
take: limit, | ||
skip: offset, | ||
}, | ||
transactionManager, | ||
); | ||
|
||
return nonAssociatedMessages.map(({ id }) => id); | ||
}, | ||
async ( | ||
ids: string[], | ||
workspaceId: string, | ||
transactionManager?: EntityManager, | ||
) => { | ||
await this.messageRepository.delete(ids, transactionManager); | ||
}, | ||
); | ||
|
||
await deleteUsingPagination( | ||
workspaceId, | ||
500, | ||
this.messageThreadRepository.getOrphanThreadIdsPaginated.bind( | ||
this.messageThreadRepository, | ||
), | ||
this.messageThreadRepository.deleteByIds.bind( | ||
this.messageThreadRepository, | ||
), | ||
async ( | ||
limit: number, | ||
offset: number, | ||
workspaceId: string, | ||
transactionManager?: EntityManager, | ||
) => { | ||
const orphanThreads = await this.messageThreadRepository.find( | ||
{ | ||
where: { | ||
messages: IsNull(), | ||
}, | ||
take: limit, | ||
skip: offset, | ||
}, | ||
transactionManager, | ||
); | ||
|
||
return orphanThreads.map(({ id }) => id); | ||
}, | ||
async ( | ||
ids: string[], | ||
workspaceId: string, | ||
transactionManager?: EntityManager, | ||
) => { | ||
await this.messageThreadRepository.delete(ids, transactionManager); | ||
}, | ||
); | ||
} | ||
} |