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:
@ -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 { 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 { 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 { 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()
|
@Injectable()
|
||||||
export class MessagingGmailFetchMessagesByBatchesService {
|
export class MessagingGmailFetchMessagesByBatchesService {
|
||||||
@ -19,15 +22,30 @@ export class MessagingGmailFetchMessagesByBatchesService {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly fetchByBatchesService: MessagingFetchByBatchesService,
|
private readonly fetchByBatchesService: MessagingFetchByBatchesService,
|
||||||
|
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
|
||||||
|
private readonly connectedAccountRepository: ConnectedAccountRepository,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async fetchAllMessages(
|
async fetchAllMessages(
|
||||||
queries: MessageQuery[],
|
queries: MessageQuery[],
|
||||||
accessToken: string,
|
|
||||||
workspaceId: string,
|
|
||||||
connectedAccountId: string,
|
connectedAccountId: string,
|
||||||
|
workspaceId: string,
|
||||||
): Promise<GmailMessage[]> {
|
): Promise<GmailMessage[]> {
|
||||||
let startTime = Date.now();
|
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(
|
const batchResponses = await this.fetchByBatchesService.fetchAllByBatches(
|
||||||
queries,
|
queries,
|
||||||
accessToken,
|
accessToken,
|
||||||
|
|||||||
@ -101,9 +101,8 @@ export class MessagingGmailMessagesImportService {
|
|||||||
const allMessages =
|
const allMessages =
|
||||||
await this.fetchMessagesByBatchesService.fetchAllMessages(
|
await this.fetchMessagesByBatchesService.fetchAllMessages(
|
||||||
messageQueries,
|
messageQueries,
|
||||||
connectedAccount.accessToken,
|
|
||||||
workspaceId,
|
|
||||||
connectedAccount.id,
|
connectedAccount.id,
|
||||||
|
workspaceId,
|
||||||
);
|
);
|
||||||
|
|
||||||
const blocklist = await this.blocklistRepository.getByWorkspaceMemberId(
|
const blocklist = await this.blocklistRepository.getByWorkspaceMemberId(
|
||||||
|
|||||||
Reference in New Issue
Block a user