5748 Create contacts for emails sent and received by email aliases (#5855)

Closes #5748
- Create feature flag
- Add scope `https://www.googleapis.com/auth/profile.emails.read` when
connecting an account
- Get email aliases with google people API, store them in
connectedAccount and refresh them before each message-import
- Update the contact creation logic accordingly
- Refactor

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
bosiraphael
2024-07-01 14:21:34 +02:00
committed by GitHub
parent a15884ea0a
commit 8c33d91734
52 changed files with 1143 additions and 754 deletions

View File

@ -2,6 +2,9 @@ import { Logger, Scope } from '@nestjs/common';
import { GoogleAPIRefreshAccessTokenService } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.service';
import { GoogleCalendarSyncService } from 'src/modules/calendar/services/google-calendar-sync/google-calendar-sync.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';
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
@ -21,6 +24,8 @@ export class GoogleCalendarSyncJob {
constructor(
private readonly googleAPIsRefreshAccessTokenService: GoogleAPIRefreshAccessTokenService,
private readonly googleCalendarSyncService: GoogleCalendarSyncService,
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
private readonly connectedAccountRepository: ConnectedAccountRepository,
) {}
@Process(GoogleCalendarSyncJob.name)
@ -29,9 +34,22 @@ export class GoogleCalendarSyncJob {
`google calendar sync for workspace ${data.workspaceId} and account ${data.connectedAccountId}`,
);
try {
const { connectedAccountId, workspaceId } = data;
const connectedAccount = await this.connectedAccountRepository.getById(
connectedAccountId,
workspaceId,
);
if (!connectedAccount) {
throw new Error(
`No connected account found for ${connectedAccountId} in workspace ${workspaceId}`,
);
}
await this.googleAPIsRefreshAccessTokenService.refreshAndSaveAccessToken(
data.workspaceId,
data.connectedAccountId,
connectedAccount,
workspaceId,
);
} catch (e) {
this.logger.error(

View File

@ -108,9 +108,8 @@ export class GoogleCalendarSyncService {
const calendarChannelId = calendarChannel.id;
const { events, nextSyncToken } = await this.getEventsFromGoogleCalendar(
refreshToken,
connectedAccount,
workspaceId,
connectedAccountId,
emailOrDomainToReimport,
syncToken,
);
@ -321,9 +320,8 @@ export class GoogleCalendarSyncService {
}
public async getEventsFromGoogleCalendar(
refreshToken: string,
connectedAccount: ConnectedAccountWorkspaceEntity,
workspaceId: string,
connectedAccountId: string,
emailOrDomainToReimport?: string,
syncToken?: string,
): Promise<{
@ -332,7 +330,7 @@ export class GoogleCalendarSyncService {
}> {
const googleCalendarClient =
await this.googleCalendarClientProvider.getGoogleCalendarClient(
refreshToken,
connectedAccount,
);
const startTime = Date.now();
@ -360,7 +358,7 @@ export class GoogleCalendarSyncService {
await this.calendarChannelRepository.update(
{
id: connectedAccountId,
id: connectedAccount.id,
},
{
syncCursor: '',
@ -368,7 +366,7 @@ export class GoogleCalendarSyncService {
);
this.logger.log(
`Sync token is no longer valid for connected account ${connectedAccountId} in workspace ${workspaceId}, resetting sync cursor.`,
`Sync token is no longer valid for connected account ${connectedAccount.id} in workspace ${workspaceId}, resetting sync cursor.`,
);
return {
@ -399,9 +397,9 @@ export class GoogleCalendarSyncService {
const endTime = Date.now();
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${connectedAccountId} getting events list in ${
endTime - startTime
}ms.`,
`google calendar sync for workspace ${workspaceId} and account ${
connectedAccount.id
} getting events list in ${endTime - startTime}ms.`,
);
return { events, nextSyncToken };

View File

@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
import { EnvironmentModule } from 'src/engine/integrations/environment/environment.module';
import { GoogleCalendarClientProvider } from 'src/modules/calendar/services/providers/google-calendar/google-calendar.provider';
import { OAuth2ClientManagerModule } from 'src/modules/connected-account/oauth2-client-manager/oauth2-client-manager.module';
@Module({
imports: [EnvironmentModule],
imports: [EnvironmentModule, OAuth2ClientManagerModule],
providers: [GoogleCalendarClientProvider],
exports: [GoogleCalendarClientProvider],
})

View File

@ -1,18 +1,21 @@
import { Injectable } from '@nestjs/common';
import { OAuth2Client } from 'google-auth-library';
import { calendar_v3 as calendarV3, google } from 'googleapis';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
import { OAuth2ClientManagerService } from 'src/modules/connected-account/oauth2-client-manager/services/oauth2-client-manager.service';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
@Injectable()
export class GoogleCalendarClientProvider {
constructor(private readonly environmentService: EnvironmentService) {}
constructor(
private readonly oAuth2ClientManagerService: OAuth2ClientManagerService,
) {}
public async getGoogleCalendarClient(
refreshToken: string,
connectedAccount: ConnectedAccountWorkspaceEntity,
): Promise<calendarV3.Calendar> {
const oAuth2Client = await this.getOAuth2Client(refreshToken);
const oAuth2Client =
await this.oAuth2ClientManagerService.getOAuth2Client(connectedAccount);
const googleCalendarClient = google.calendar({
version: 'v3',
@ -21,24 +24,4 @@ export class GoogleCalendarClientProvider {
return googleCalendarClient;
}
private async getOAuth2Client(refreshToken: string): Promise<OAuth2Client> {
const googleCalendarClientId = this.environmentService.get(
'AUTH_GOOGLE_CLIENT_ID',
);
const googleCalendarClientSecret = this.environmentService.get(
'AUTH_GOOGLE_CLIENT_SECRET',
);
const oAuth2Client = new google.auth.OAuth2(
googleCalendarClientId,
googleCalendarClientSecret,
);
oAuth2Client.setCredentials({
refresh_token: refreshToken,
});
return oAuth2Client;
}
}