Delete message when no more association (#3701)

* Delete message when no more association

* remove unused injections

* rename methods

* fix after review
This commit is contained in:
Weiko
2024-01-30 17:58:36 +01:00
committed by GitHub
parent 8b9d62e425
commit 64b2ef3dc2
23 changed files with 501 additions and 201 deletions

View File

@ -9,6 +9,10 @@ import {
GmailFullSyncJobData,
GmailFullSyncJob,
} from 'src/workspace/messaging/jobs/gmail-full-sync.job';
import { ConnectedAccountService } from 'src/workspace/messaging/connected-account/connected-account.service';
import { MessageChannelService } from 'src/workspace/messaging/message-channel/message-channel.service';
import { MessageChannelMessageAssociationService } from 'src/workspace/messaging/message-channel-message-association/message-channel-message-association.service';
import { WorkspaceDataSourceService } from 'src/workspace/workspace-datasource/workspace-datasource.service';
@Injectable()
export class GmailFullSyncService {
@ -18,6 +22,10 @@ export class GmailFullSyncService {
private readonly utils: MessagingUtilsService,
@Inject(MessageQueue.messagingQueue)
private readonly messageQueueService: MessageQueueService,
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
private readonly connectedAccountService: ConnectedAccountService,
private readonly messageChannelService: MessageChannelService,
private readonly messageChannelMessageAssociationService: MessageChannelMessageAssociationService,
) {}
public async fetchConnectedAccountThreads(
@ -25,13 +33,14 @@ export class GmailFullSyncService {
connectedAccountId: string,
nextPageToken?: string,
): Promise<void> {
const { workspaceDataSource, dataSourceMetadata } =
await this.utils.getDataSourceMetadataWorkspaceMetadata(workspaceId);
const { dataSource: workspaceDataSource, dataSourceMetadata } =
await this.workspaceDataSourceService.connectedToWorkspaceDataSourceAndReturnMetadata(
workspaceId,
);
const connectedAccount = await this.utils.getConnectedAcountByIdOrFail(
const connectedAccount = await this.connectedAccountService.getByIdOrFail(
connectedAccountId,
dataSourceMetadata,
workspaceDataSource,
workspaceId,
);
const accessToken = connectedAccount.accessToken;
@ -41,18 +50,13 @@ export class GmailFullSyncService {
throw new Error('No refresh token found');
}
const gmailMessageChannel = await workspaceDataSource?.query(
`SELECT * FROM ${dataSourceMetadata.schema}."messageChannel" WHERE "connectedAccountId" = $1 AND "type" = 'email' LIMIT 1`,
[connectedAccountId],
);
if (!gmailMessageChannel.length) {
throw new Error(
`No gmail message channel found for connected account ${connectedAccountId}`,
const gmailMessageChannel =
await this.messageChannelService.getFirstByConnectedAccountIdOrFail(
workspaceId,
connectedAccountId,
);
}
const gmailMessageChannelId = gmailMessageChannel[0].id;
const gmailMessageChannelId = gmailMessageChannel.id;
const gmailClient =
await this.gmailClientProvider.getGmailClient(refreshToken);
@ -74,11 +78,10 @@ export class GmailFullSyncService {
}
const existingMessageChannelMessageAssociations =
await this.utils.getMessageChannelMessageAssociations(
await this.messageChannelMessageAssociationService.getByMessageExternalIdsAndMessageChannelId(
messageExternalIds,
gmailMessageChannelId,
dataSourceMetadata,
workspaceDataSource,
workspaceId,
);
const existingMessageChannelMessageAssociationsExternalIds =
@ -113,6 +116,7 @@ export class GmailFullSyncService {
workspaceDataSource,
connectedAccount,
gmailMessageChannelId,
workspaceId,
);
if (errors.length) throw new Error('Error fetching messages');
@ -125,11 +129,10 @@ export class GmailFullSyncService {
if (!historyId) throw new Error('No history id found');
await this.utils.saveLastSyncHistoryId(
await this.connectedAccountService.saveLastSyncHistoryId(
historyId,
connectedAccount.id,
dataSourceMetadata,
workspaceDataSource,
workspaceId,
);
if (messages.data.nextPageToken) {