Skip to content

Commit

Permalink
Fix access token refresh (twentyhq#5825)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bosiraphael authored Jun 11, 2024
1 parent 3328666 commit 64b8e4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 64b8e4e

Please sign in to comment.