6256 refactor messaging module to remove all provider specific code and put it inside the drivers folders (#6721)
Closes #6256 Closes #6257 + Create custom exceptions --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -0,0 +1,14 @@
|
||||
import { CustomException } from 'src/utils/custom-exception';
|
||||
|
||||
export class RefreshAccessTokenException extends CustomException {
|
||||
code: RefreshAccessTokenExceptionCode;
|
||||
constructor(message: string, code: RefreshAccessTokenExceptionCode) {
|
||||
super(message, code);
|
||||
}
|
||||
}
|
||||
|
||||
export enum RefreshAccessTokenExceptionCode {
|
||||
REFRESH_TOKEN_NOT_FOUND = 'REFRESH_TOKEN_NOT_FOUND',
|
||||
REFRESH_ACCESS_TOKEN_FAILED = 'REFRESH_ACCESS_TOKEN_FAILED',
|
||||
PROVIDER_NOT_SUPPORTED = 'PROVIDER_NOT_SUPPORTED',
|
||||
}
|
||||
@ -2,6 +2,10 @@ import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
||||
import { GoogleAPIRefreshAccessTokenService } from 'src/modules/connected-account/refresh-access-token-manager/drivers/google/services/google-api-refresh-access-token.service';
|
||||
import {
|
||||
RefreshAccessTokenException,
|
||||
RefreshAccessTokenExceptionCode,
|
||||
} from 'src/modules/connected-account/refresh-access-token-manager/exceptions/refresh-access-token.exception';
|
||||
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
|
||||
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||
|
||||
@ -20,20 +24,25 @@ export class RefreshAccessTokenService {
|
||||
const refreshToken = connectedAccount.refreshToken;
|
||||
|
||||
if (!refreshToken) {
|
||||
throw new Error(
|
||||
throw new RefreshAccessTokenException(
|
||||
`No refresh token found for connected account ${connectedAccount.id} in workspace ${workspaceId}`,
|
||||
RefreshAccessTokenExceptionCode.REFRESH_TOKEN_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
const accessToken = await this.refreshAccessToken(
|
||||
connectedAccount,
|
||||
refreshToken,
|
||||
);
|
||||
|
||||
await this.connectedAccountRepository.updateAccessToken(
|
||||
accessToken,
|
||||
connectedAccount.id,
|
||||
workspaceId,
|
||||
);
|
||||
let accessToken: string;
|
||||
|
||||
try {
|
||||
accessToken = await this.refreshAccessToken(
|
||||
connectedAccount,
|
||||
refreshToken,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new RefreshAccessTokenException(
|
||||
`Error refreshing access token for connected account ${connectedAccount.id} in workspace ${workspaceId}: ${error.message}`,
|
||||
RefreshAccessTokenExceptionCode.REFRESH_ACCESS_TOKEN_FAILED,
|
||||
);
|
||||
}
|
||||
|
||||
await this.connectedAccountRepository.updateAccessToken(
|
||||
accessToken,
|
||||
@ -54,8 +63,9 @@ export class RefreshAccessTokenService {
|
||||
refreshToken,
|
||||
);
|
||||
default:
|
||||
throw new Error(
|
||||
`Provider ${connectedAccount.provider} is not supported.`,
|
||||
throw new RefreshAccessTokenException(
|
||||
`Provider ${connectedAccount.provider} is not supported`,
|
||||
RefreshAccessTokenExceptionCode.PROVIDER_NOT_SUPPORTED,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user