Fix access token refresh (#5825)

In `messaging-gmail-messages-import.service`, we were refreshing the
access token before each query but we were passing the old access token
to `fetchAllMessages`.
I modified the function to query the updated connectedAccount with the
new access token.
This will solve the 401 errors we were getting in production.
This commit is contained in:
bosiraphael
2024-06-11 18:52:38 +02:00
committed by GitHub
parent 3328666308
commit 64b8e4ec4d
2 changed files with 21 additions and 4 deletions

View File

@ -10,6 +10,9 @@ import { GmailMessage } from 'src/modules/messaging/message-import-manager/drive
import { MessageQuery } from 'src/modules/messaging/message-import-manager/types/message-or-thread-query';
import { formatAddressObjectAsParticipants } from 'src/modules/messaging/message-import-manager/utils/format-address-object-as-participants.util';
import { MessagingFetchByBatchesService } from 'src/modules/messaging/common/services/messaging-fetch-by-batch.service';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
@Injectable()
export class MessagingGmailFetchMessagesByBatchesService {
@ -19,15 +22,30 @@ export class MessagingGmailFetchMessagesByBatchesService {
constructor(
private readonly fetchByBatchesService: MessagingFetchByBatchesService,
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
private readonly connectedAccountRepository: ConnectedAccountRepository,
) {}
async fetchAllMessages(
queries: MessageQuery[],
accessToken: string,
workspaceId: string,
connectedAccountId: string,
workspaceId: string,
): Promise<GmailMessage[]> {
let startTime = Date.now();
const connectedAccount = await this.connectedAccountRepository.getById(
connectedAccountId,
workspaceId,
);
if (!connectedAccount) {
throw new Error(
`Connected account ${connectedAccountId} not found in workspace ${workspaceId}`,
);
}
const accessToken = connectedAccount.accessToken;
const batchResponses = await this.fetchByBatchesService.fetchAllByBatches(
queries,
accessToken,

View File

@ -101,9 +101,8 @@ export class MessagingGmailMessagesImportService {
const allMessages =
await this.fetchMessagesByBatchesService.fetchAllMessages(
messageQueries,
connectedAccount.accessToken,
workspaceId,
connectedAccount.id,
workspaceId,
);
const blocklist = await this.blocklistRepository.getByWorkspaceMemberId(