This PR is fixing some issues and adding enhancement in TwentyORM: - [x] Composite fields in nested relations are not formatted properly - [x] Passing operators like `Any` in `where` condition is breaking the query - [x] Ability to auto load workspace-entities based on a regex path I've also introduced an example of use for `CalendarEventService`: https://github.com/twentyhq/twenty/pull/5439/files#diff-3a7dffc0dea57345d10e70c648e911f98fe237248bcea124dafa9c8deb1db748R15
94 lines
2.8 KiB
TypeScript
94 lines
2.8 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
|
|
import axios from 'axios';
|
|
|
|
import { EnvironmentService } from 'src/engine/integrations/environment/environment.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 GoogleAPIRefreshAccessTokenService {
|
|
constructor(
|
|
private readonly environmentService: EnvironmentService,
|
|
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
|
|
private readonly connectedAccountRepository: ConnectedAccountRepository,
|
|
) {}
|
|
|
|
async refreshAndSaveAccessToken(
|
|
workspaceId: string,
|
|
connectedAccountId: string,
|
|
): Promise<void> {
|
|
const connectedAccount = await this.connectedAccountRepository.getById(
|
|
connectedAccountId,
|
|
workspaceId,
|
|
);
|
|
|
|
if (!connectedAccount) {
|
|
throw new Error(
|
|
`No connected account found for ${connectedAccountId} in workspace ${workspaceId}`,
|
|
);
|
|
}
|
|
|
|
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) {
|
|
throw new Error(
|
|
`No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId}`,
|
|
);
|
|
}
|
|
|
|
const accessToken = await this.refreshAccessToken(
|
|
refreshToken,
|
|
connectedAccountId,
|
|
workspaceId,
|
|
);
|
|
|
|
await this.connectedAccountRepository.updateAccessToken(
|
|
accessToken,
|
|
connectedAccountId,
|
|
workspaceId,
|
|
);
|
|
}
|
|
|
|
async refreshAccessToken(
|
|
refreshToken: string,
|
|
connectedAccountId: string,
|
|
workspaceId: string,
|
|
): Promise<string> {
|
|
try {
|
|
const response = await axios.post(
|
|
'https://oauth2.googleapis.com/token',
|
|
{
|
|
client_id: this.environmentService.get('AUTH_GOOGLE_CLIENT_ID'),
|
|
client_secret: this.environmentService.get(
|
|
'AUTH_GOOGLE_CLIENT_SECRET',
|
|
),
|
|
refresh_token: refreshToken,
|
|
grant_type: 'refresh_token',
|
|
},
|
|
{
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
},
|
|
);
|
|
|
|
return response.data.access_token;
|
|
} catch (error) {
|
|
await this.connectedAccountRepository.updateAuthFailedAt(
|
|
connectedAccountId,
|
|
workspaceId,
|
|
);
|
|
|
|
throw new Error(`Error refreshing access token: ${error.message}`);
|
|
}
|
|
}
|
|
}
|