-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove message-import cache when connectedAccount is removed (#6021)
- Loading branch information
1 parent
3b7901b
commit 1736aee
Showing
6 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
9 changes: 8 additions & 1 deletion
9
...y-server/src/modules/connected-account/query-hooks/connected-account-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 |
---|---|---|
@@ -1,16 +1,23 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module'; | ||
import { BlocklistCreateManyPreQueryHook } from 'src/modules/connected-account/query-hooks/blocklist/blocklist-create-many.pre-query.hook'; | ||
import { BlocklistUpdateManyPreQueryHook } from 'src/modules/connected-account/query-hooks/blocklist/blocklist-update-many.pre-query.hook'; | ||
import { BlocklistUpdateOnePreQueryHook } from 'src/modules/connected-account/query-hooks/blocklist/blocklist-update-one.pre-query.hook'; | ||
import { ConnectedAccountDeleteOnePreQueryHook } from 'src/modules/connected-account/query-hooks/connected-account/connected-account-delete-one.pre-query.hook'; | ||
import { BlocklistValidationModule } from 'src/modules/connected-account/services/blocklist/blocklist-validation.module'; | ||
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity'; | ||
|
||
@Module({ | ||
imports: [BlocklistValidationModule], | ||
imports: [ | ||
BlocklistValidationModule, | ||
TwentyORMModule.forFeature([MessageChannelWorkspaceEntity]), | ||
], | ||
providers: [ | ||
BlocklistCreateManyPreQueryHook, | ||
BlocklistUpdateManyPreQueryHook, | ||
BlocklistUpdateOnePreQueryHook, | ||
ConnectedAccountDeleteOnePreQueryHook, | ||
], | ||
}) | ||
export class ConnectedAccountQueryHookModule {} |
43 changes: 43 additions & 0 deletions
43
...cted-account/query-hooks/connected-account/connected-account-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,43 @@ | ||
import { EventEmitter2 } from '@nestjs/event-emitter'; | ||
|
||
import { WorkspaceQueryHookInstance } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/interfaces/workspace-query-hook.interface'; | ||
import { DeleteOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface'; | ||
|
||
import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator'; | ||
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator'; | ||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository'; | ||
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity'; | ||
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event'; | ||
|
||
@WorkspaceQueryHook(`connectedAccount.deleteOne`) | ||
export class ConnectedAccountDeleteOnePreQueryHook | ||
implements WorkspaceQueryHookInstance | ||
{ | ||
constructor( | ||
@InjectWorkspaceRepository(MessageChannelWorkspaceEntity) | ||
private readonly messageChannelRepository: WorkspaceRepository<MessageChannelWorkspaceEntity>, | ||
private eventEmitter: EventEmitter2, | ||
) {} | ||
|
||
async execute( | ||
_userId: string, | ||
workspaceId: string, | ||
payload: DeleteOneResolverArgs, | ||
): Promise<void> { | ||
const connectedAccountId = payload.id; | ||
|
||
const messageChannels = await this.messageChannelRepository.findBy({ | ||
connectedAccountId, | ||
}); | ||
|
||
messageChannels.forEach((messageChannel) => { | ||
this.eventEmitter.emit('messageChannel.deleted', { | ||
workspaceId, | ||
recordId: messageChannel.id, | ||
} satisfies Pick< | ||
ObjectRecordDeleteEvent<MessageChannelWorkspaceEntity>, | ||
'workspaceId' | 'recordId' | ||
>); | ||
}); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
.../twenty-server/src/modules/messaging/message-import-manager/jobs/messaging-clean-cache.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,38 @@ | ||
import { Logger } from '@nestjs/common'; | ||
|
||
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator'; | ||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; | ||
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator'; | ||
import { InjectCacheStorage } from 'src/engine/integrations/cache-storage/decorators/cache-storage.decorator'; | ||
import { CacheStorageNamespace } from 'src/engine/integrations/cache-storage/types/cache-storage-namespace.enum'; | ||
import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache-storage.service'; | ||
|
||
export type MessagingCleanCacheJobData = { | ||
workspaceId: string; | ||
messageChannelId: string; | ||
}; | ||
|
||
@Processor(MessageQueue.messagingQueue) | ||
export class MessagingCleanCacheJob { | ||
private readonly logger = new Logger(MessagingCleanCacheJob.name); | ||
|
||
constructor( | ||
@InjectCacheStorage(CacheStorageNamespace.Messaging) | ||
private readonly cacheStorage: CacheStorageService, | ||
) {} | ||
|
||
@Process(MessagingCleanCacheJob.name) | ||
async handle(data: MessagingCleanCacheJobData): Promise<void> { | ||
this.logger.log( | ||
`Deleting message channel ${data.messageChannelId} associated cache in workspace ${data.workspaceId}`, | ||
); | ||
|
||
await this.cacheStorage.del( | ||
`messages-to-import:${data.workspaceId}:gmail:${data.messageChannelId}`, | ||
); | ||
|
||
this.logger.log( | ||
`Deleted message channel ${data.messageChannelId} associated cache in workspace ${data.workspaceId}`, | ||
); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ing/message-import-manager/listeners/messaging-import-manager-message-channel.listener.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,33 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { OnEvent } from '@nestjs/event-emitter'; | ||
|
||
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event'; | ||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; | ||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; | ||
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator'; | ||
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity'; | ||
import { | ||
MessagingCleanCacheJob, | ||
MessagingCleanCacheJobData, | ||
} from 'src/modules/messaging/message-import-manager/jobs/messaging-clean-cache'; | ||
|
||
@Injectable() | ||
export class MessagingMessageImportManagerMessageChannelListener { | ||
constructor( | ||
@InjectMessageQueue(MessageQueue.messagingQueue) | ||
private readonly messageQueueService: MessageQueueService, | ||
) {} | ||
|
||
@OnEvent('messageChannel.deleted') | ||
async handleDeletedEvent( | ||
payload: ObjectRecordDeleteEvent<MessageChannelWorkspaceEntity>, | ||
) { | ||
await this.messageQueueService.add<MessagingCleanCacheJobData>( | ||
MessagingCleanCacheJob.name, | ||
{ | ||
workspaceId: payload.workspaceId, | ||
messageChannelId: payload.recordId, | ||
}, | ||
); | ||
} | ||
} |
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