The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { Injectable, Logger } from '@nestjs/common';
import { isDefined } from 'twenty-shared/utils';
import { In } from 'typeorm';
import { type MessageChannelEntity } from 'src/engine/metadata-modules/message-channel/entities/message-channel.entity';
import { WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';
import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';
import { type MessageChannelMessageAssociationMessageFolderWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel-message-association-message-folder.workspace-entity';
import { type MessageChannelMessageAssociationWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel-message-association.workspace-entity';
import { type MessageFolderEntity } from 'src/engine/metadata-modules/message-folder/entities/message-folder.entity';
import { MessagingMessageCleanerService } from 'src/modules/messaging/message-cleaner/services/messaging-message-cleaner.service';
const BATCH_SIZE = 200;
@Injectable()
export class MessagingDeleteFolderMessagesService {
private readonly logger = new Logger(
MessagingDeleteFolderMessagesService.name,
);
constructor(
private readonly messagingMessageCleanerService: MessagingMessageCleanerService,
private readonly workspaceOrmManager: WorkspaceOrmManager,
) {}
async deleteFolderMessages(
workspaceId: string,
messageChannel: MessageChannelEntity,
messageFolder: MessageFolderEntity,
): Promise<number> {
this.logger.log(
`WorkspaceId: ${workspaceId}, MessageChannelId: ${messageChannel.id}, FolderId: ${messageFolder.id} - Deleting messages from folder: ${messageFolder.name}`,
);
const authContext = buildSystemAuthContext(workspaceId);
let totalDeletedCount = 0;
await this.workspaceOrmManager.executeInWorkspaceContext(
async () => {
const messageFolderAssociationRepository =
this.workspaceOrmManager.getRepositoryWithoutPermissionChecks<MessageChannelMessageAssociationMessageFolderWorkspaceEntity>(
'messageChannelMessageAssociationMessageFolder',
);
const messageChannelMessageAssociationRepository =
this.workspaceOrmManager.getRepositoryWithoutPermissionChecks<MessageChannelMessageAssociationWorkspaceEntity>(
'messageChannelMessageAssociation',
);
let hasMoreData = true;
while (hasMoreData) {
const folderAssociations =
await messageFolderAssociationRepository.find({
where: {
messageFolderId: messageFolder.id,
},
take: BATCH_SIZE,
});
if (folderAssociations.length === 0) {
hasMoreData = false;
continue;
}
const folderAssociationIds = folderAssociations.map(
(folderAssociation) => folderAssociation.id,
);
const messageChannelMessageAssociationIds = folderAssociations.map(
(folderAssociation) =>
folderAssociation.messageChannelMessageAssociationId,
);
const associations =
await messageChannelMessageAssociationRepository.find({
where: {
id: In(messageChannelMessageAssociationIds),
messageChannelId: messageChannel.id,
},
});
const messageExternalIds = associations
.map((association) => association.messageExternalId)
.filter(isDefined);
this.logger.log(
`WorkspaceId: ${workspaceId}, MessageChannelId: ${messageChannel.id}, FolderId: ${messageFolder.id} - Deleting ${messageExternalIds.length} messages`,
);
if (messageExternalIds.length > 0) {
await this.messagingMessageCleanerService.deleteMessagesChannelMessageAssociationsAndRelatedOrphans(
{
workspaceId,
messageExternalIds,
messageChannelId: messageChannel.id,
},
);
totalDeletedCount += messageExternalIds.length;
}
await messageFolderAssociationRepository.delete({
id: In(folderAssociationIds),
});
}
},
authContext,
{ lite: true },
);
this.logger.log(
`WorkspaceId: ${workspaceId}, MessageChannelId: ${messageChannel.id}, FolderId: ${messageFolder.id} - Completed deleting ${totalDeletedCount} messages from folder: ${messageFolder.name}`,
);
return totalDeletedCount;
}
}