[calendar/messaging] fix connected account auth failed should skip sync (#4920)

- AuthFailedAt is set when a refreshToken is not valid and an
accessToken can't be generated, meaning it will need a manual action
from the user to provide a new refresh token.
- Calendar/messaging jobs should not be executed if authFailedAt is not
null.
This commit is contained in:
Weiko
2024-04-11 17:57:48 +02:00
committed by GitHub
parent 8853408264
commit fc56775c2a
12 changed files with 342 additions and 208 deletions

View File

@ -3,7 +3,6 @@ import { Injectable } from '@nestjs/common';
import axios from 'axios';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
import { ExceptionHandlerService } from 'src/engine/integrations/exception-handler/exception-handler.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 { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata';
@ -14,7 +13,6 @@ export class GoogleAPIRefreshAccessTokenService {
private readonly environmentService: EnvironmentService,
@InjectObjectMetadataRepository(ConnectedAccountObjectMetadata)
private readonly connectedAccountRepository: ConnectedAccountRepository,
private readonly exceptionHandlerService: ExceptionHandlerService,
) {}
async refreshAndSaveAccessToken(
@ -32,6 +30,12 @@ export class GoogleAPIRefreshAccessTokenService {
);
}
if (connectedAccount.authFailedAt) {
throw new Error(
`Skipping refresh of access token for connected account ${connectedAccountId} in workspace ${workspaceId} because auth already failed, a new refresh token is needed`,
);
}
const refreshToken = connectedAccount.refreshToken;
if (!refreshToken) {
@ -82,11 +86,7 @@ export class GoogleAPIRefreshAccessTokenService {
connectedAccountId,
workspaceId,
);
this.exceptionHandlerService.captureExceptions([error], {
user: {
workspaceId,
},
});
throw new Error(`Error refreshing access token: ${error.message}`);
}
}