* Merge messages and threads * rename messageChannelSync to messageChannelMessage * add merge logic * remove deprecated methods * restore enqueue GmailFullSyncJob after connectedAccount creation
103 lines
3.0 KiB
TypeScript
103 lines
3.0 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
|
|
import { FetchMessagesByBatchesService } from 'src/workspace/messaging/services/fetch-messages-by-batches.service';
|
|
import { GmailClientProvider } from 'src/workspace/messaging/providers/gmail/gmail-client.provider';
|
|
import { MessagingUtilsService } from 'src/workspace/messaging/services/messaging-utils.service';
|
|
|
|
@Injectable()
|
|
export class GmailFullSyncService {
|
|
constructor(
|
|
private readonly gmailClientProvider: GmailClientProvider,
|
|
private readonly fetchMessagesByBatchesService: FetchMessagesByBatchesService,
|
|
private readonly utils: MessagingUtilsService,
|
|
) {}
|
|
|
|
public async fetchConnectedAccountThreads(
|
|
workspaceId: string,
|
|
connectedAccountId: string,
|
|
maxResults = 500,
|
|
): Promise<void> {
|
|
const { workspaceDataSource, dataSourceMetadata, connectedAccount } =
|
|
await this.utils.getDataSourceMetadataWorkspaceMetadataAndConnectedAccount(
|
|
workspaceId,
|
|
connectedAccountId,
|
|
);
|
|
|
|
const accessToken = connectedAccount.accessToken;
|
|
const refreshToken = connectedAccount.refreshToken;
|
|
|
|
if (!refreshToken) {
|
|
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 gmailMessageChannelId = gmailMessageChannel[0].id;
|
|
|
|
const gmailClient =
|
|
await this.gmailClientProvider.getGmailClient(refreshToken);
|
|
|
|
const messages = await gmailClient.users.messages.list({
|
|
userId: 'me',
|
|
maxResults,
|
|
});
|
|
|
|
const messagesData = messages.data.messages;
|
|
|
|
const messageExternalIds = messagesData
|
|
? messagesData.map((message) => message.id || '')
|
|
: [];
|
|
|
|
if (!messagesData || messagesData?.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const messageQueries =
|
|
this.utils.createQueriesFromMessageIds(messageExternalIds);
|
|
|
|
const { messages: messagesToSave, errors } =
|
|
await this.fetchMessagesByBatchesService.fetchAllMessages(
|
|
messageQueries,
|
|
accessToken,
|
|
);
|
|
|
|
if (messagesToSave.length === 0) {
|
|
return;
|
|
}
|
|
|
|
await this.utils.saveMessages(
|
|
messagesToSave,
|
|
dataSourceMetadata,
|
|
workspaceDataSource,
|
|
connectedAccount,
|
|
gmailMessageChannelId,
|
|
);
|
|
|
|
if (errors.length) throw new Error('Error fetching messages');
|
|
|
|
const lastModifiedMessageId = messagesData[0].id;
|
|
|
|
const historyId = messagesToSave.find(
|
|
(message) => message.externalId === lastModifiedMessageId,
|
|
)?.historyId;
|
|
|
|
if (!historyId) throw new Error('No history id found');
|
|
|
|
await this.utils.saveLastSyncHistoryId(
|
|
historyId,
|
|
connectedAccount.id,
|
|
dataSourceMetadata,
|
|
workspaceDataSource,
|
|
);
|
|
}
|
|
}
|