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:
@ -16,7 +16,7 @@ import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/perso
|
||||
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||
import { getUniqueContactsAndHandles } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/get-unique-contacts-and-handles.util';
|
||||
import { CalendarEventParticipantService } from 'src/modules/calendar/services/calendar-event-participant/calendar-event-participant.service';
|
||||
import { filterOutContactsFromCompanyOrWorkspace } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/filter-out-contacts-from-company-or-workspace.util';
|
||||
import { filterOutSelfAndContactsFromCompanyOrWorkspace } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/filter-out-contacts-from-company-or-workspace.util';
|
||||
import { MessagingMessageParticipantService } from 'src/modules/messaging/common/services/messaging-message-participant.service';
|
||||
import { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
|
||||
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-event-participant.workspace-entity';
|
||||
@ -43,7 +43,7 @@ export class CreateCompanyAndContactService {
|
||||
) {}
|
||||
|
||||
async createCompaniesAndPeople(
|
||||
connectedAccountHandle: string,
|
||||
connectedAccount: ConnectedAccountWorkspaceEntity,
|
||||
contactsToCreate: Contact[],
|
||||
workspaceId: string,
|
||||
transactionManager?: EntityManager,
|
||||
@ -62,9 +62,9 @@ export class CreateCompanyAndContactService {
|
||||
);
|
||||
|
||||
const contactsToCreateFromOtherCompanies =
|
||||
filterOutContactsFromCompanyOrWorkspace(
|
||||
filterOutSelfAndContactsFromCompanyOrWorkspace(
|
||||
contactsToCreate,
|
||||
connectedAccountHandle,
|
||||
connectedAccount,
|
||||
workspaceMembers,
|
||||
);
|
||||
|
||||
@ -150,7 +150,7 @@ export class CreateCompanyAndContactService {
|
||||
await this.workspaceDataSource?.transaction(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const createdPeople = await this.createCompaniesAndPeople(
|
||||
connectedAccount.handle,
|
||||
connectedAccount,
|
||||
contactsBatch,
|
||||
workspaceId,
|
||||
transactionManager,
|
||||
|
||||
@ -1,13 +1,16 @@
|
||||
import { getDomainNameFromHandle } from 'src/modules/calendar-messaging-participant/utils/get-domain-name-from-handle.util';
|
||||
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||
import { Contact } from 'src/modules/connected-account/auto-companies-and-contacts-creation/types/contact.type';
|
||||
|
||||
export function filterOutContactsFromCompanyOrWorkspace(
|
||||
export function filterOutSelfAndContactsFromCompanyOrWorkspace(
|
||||
contacts: Contact[],
|
||||
selfHandle: string,
|
||||
connectedAccount: ConnectedAccountWorkspaceEntity,
|
||||
workspaceMembers: WorkspaceMemberWorkspaceEntity[],
|
||||
): Contact[] {
|
||||
const selfDomainName = getDomainNameFromHandle(selfHandle);
|
||||
const selfDomainName = getDomainNameFromHandle(connectedAccount.handle);
|
||||
|
||||
const emailAliases = connectedAccount.emailAliases?.split(',') || [];
|
||||
|
||||
const workspaceMembersMap = workspaceMembers.reduce(
|
||||
(map, workspaceMember) => {
|
||||
@ -21,6 +24,7 @@ export function filterOutContactsFromCompanyOrWorkspace(
|
||||
return contacts.filter(
|
||||
(contact) =>
|
||||
getDomainNameFromHandle(contact.handle) !== selfDomainName &&
|
||||
!workspaceMembersMap[contact.handle],
|
||||
!workspaceMembersMap[contact.handle] &&
|
||||
!emailAliases.includes(contact.handle),
|
||||
);
|
||||
}
|
||||
|
||||
@ -0,0 +1,43 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { google } from 'googleapis';
|
||||
|
||||
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 GoogleEmailAliasManagerService {
|
||||
constructor(
|
||||
private readonly oAuth2ClientManagerService: OAuth2ClientManagerService,
|
||||
) {}
|
||||
|
||||
public async getEmailAliases(
|
||||
connectedAccount: ConnectedAccountWorkspaceEntity,
|
||||
) {
|
||||
const oAuth2Client =
|
||||
await this.oAuth2ClientManagerService.getOAuth2Client(connectedAccount);
|
||||
|
||||
const people = google.people({
|
||||
version: 'v1',
|
||||
auth: oAuth2Client,
|
||||
});
|
||||
|
||||
const emailsResponse = await people.people.get({
|
||||
resourceName: 'people/me',
|
||||
personFields: 'emailAddresses',
|
||||
});
|
||||
|
||||
const emailAddresses = emailsResponse.data.emailAddresses;
|
||||
|
||||
const emailAliases =
|
||||
emailAddresses
|
||||
?.filter((emailAddress) => {
|
||||
return emailAddress.metadata?.primary !== true;
|
||||
})
|
||||
.map((emailAddress) => {
|
||||
return emailAddress.value || '';
|
||||
}) || [];
|
||||
|
||||
return emailAliases;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
|
||||
import { GoogleEmailAliasManagerService } from 'src/modules/connected-account/email-alias-manager/drivers/google/google-email-alias-manager.service';
|
||||
import { EmailAliasManagerService } from 'src/modules/connected-account/email-alias-manager/services/email-alias-manager.service';
|
||||
import { OAuth2ClientManagerModule } from 'src/modules/connected-account/oauth2-client-manager/oauth2-client-manager.module';
|
||||
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ObjectMetadataRepositoryModule.forFeature([
|
||||
ConnectedAccountWorkspaceEntity,
|
||||
]),
|
||||
OAuth2ClientManagerModule,
|
||||
],
|
||||
providers: [EmailAliasManagerService, GoogleEmailAliasManagerService],
|
||||
exports: [EmailAliasManagerService],
|
||||
})
|
||||
export class EmailAliasManagerModule {}
|
||||
@ -0,0 +1,41 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
||||
import { GoogleEmailAliasManagerService } from 'src/modules/connected-account/email-alias-manager/drivers/google/google-email-alias-manager.service';
|
||||
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 EmailAliasManagerService {
|
||||
constructor(
|
||||
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
|
||||
private readonly connectedAccountRepository: ConnectedAccountRepository,
|
||||
private readonly googleEmailAliasManagerService: GoogleEmailAliasManagerService,
|
||||
) {}
|
||||
|
||||
public async refreshEmailAliases(
|
||||
connectedAccount: ConnectedAccountWorkspaceEntity,
|
||||
workspaceId: string,
|
||||
) {
|
||||
let emailAliases: string[];
|
||||
|
||||
switch (connectedAccount.provider) {
|
||||
case 'google':
|
||||
emailAliases =
|
||||
await this.googleEmailAliasManagerService.getEmailAliases(
|
||||
connectedAccount,
|
||||
);
|
||||
break;
|
||||
default:
|
||||
throw new Error(
|
||||
`Email alias manager for provider ${connectedAccount.provider} is not implemented`,
|
||||
);
|
||||
}
|
||||
|
||||
await this.connectedAccountRepository.updateEmailAliases(
|
||||
emailAliases,
|
||||
connectedAccount.id,
|
||||
workspaceId,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { OAuth2Client } from 'google-auth-library';
|
||||
import { google } from 'googleapis';
|
||||
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
|
||||
@Injectable()
|
||||
export class GoogleOAuth2ClientManagerService {
|
||||
constructor(private readonly environmentService: EnvironmentService) {}
|
||||
|
||||
public async getOAuth2Client(refreshToken: string): Promise<OAuth2Client> {
|
||||
const gmailClientId = this.environmentService.get('AUTH_GOOGLE_CLIENT_ID');
|
||||
const gmailClientSecret = this.environmentService.get(
|
||||
'AUTH_GOOGLE_CLIENT_SECRET',
|
||||
);
|
||||
|
||||
const oAuth2Client = new google.auth.OAuth2(
|
||||
gmailClientId,
|
||||
gmailClientSecret,
|
||||
);
|
||||
|
||||
oAuth2Client.setCredentials({
|
||||
refresh_token: refreshToken,
|
||||
});
|
||||
|
||||
return oAuth2Client;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { GoogleOAuth2ClientManagerService } from 'src/modules/connected-account/oauth2-client-manager/drivers/google/google-oauth2-client-manager.service';
|
||||
import { OAuth2ClientManagerService } from 'src/modules/connected-account/oauth2-client-manager/services/oauth2-client-manager.service';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
providers: [OAuth2ClientManagerService, GoogleOAuth2ClientManagerService],
|
||||
exports: [OAuth2ClientManagerService],
|
||||
})
|
||||
export class OAuth2ClientManagerModule {}
|
||||
@ -0,0 +1,30 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { OAuth2Client } from 'google-auth-library';
|
||||
|
||||
import { GoogleOAuth2ClientManagerService } from 'src/modules/connected-account/oauth2-client-manager/drivers/google/google-oauth2-client-manager.service';
|
||||
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||
|
||||
@Injectable()
|
||||
export class OAuth2ClientManagerService {
|
||||
constructor(
|
||||
private readonly googleOAuth2ClientManagerService: GoogleOAuth2ClientManagerService,
|
||||
) {}
|
||||
|
||||
public async getOAuth2Client(
|
||||
connectedAccount: ConnectedAccountWorkspaceEntity,
|
||||
): Promise<OAuth2Client> {
|
||||
const { refreshToken } = connectedAccount;
|
||||
|
||||
switch (connectedAccount.provider) {
|
||||
case 'google':
|
||||
return this.googleOAuth2ClientManagerService.getOAuth2Client(
|
||||
refreshToken,
|
||||
);
|
||||
default:
|
||||
throw new Error(
|
||||
`OAuth2 client manager for provider ${connectedAccount.provider} is not implemented`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -306,4 +306,22 @@ export class ConnectedAccountRepository {
|
||||
|
||||
return connectedAccount;
|
||||
}
|
||||
|
||||
public async updateEmailAliases(
|
||||
emailAliases: string[],
|
||||
connectedAccountId: string,
|
||||
workspaceId: string,
|
||||
transactionManager?: EntityManager,
|
||||
) {
|
||||
const dataSourceSchema =
|
||||
this.workspaceDataSourceService.getSchemaName(workspaceId);
|
||||
|
||||
await this.workspaceDataSourceService.executeRawQuery(
|
||||
`UPDATE ${dataSourceSchema}."connectedAccount" SET "emailAliases" = $1 WHERE "id" = $2`,
|
||||
// TODO: modify emailAliases to be of fieldmetadatatype array
|
||||
[emailAliases.join(','), connectedAccountId],
|
||||
workspaceId,
|
||||
transactionManager,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,33 +16,27 @@ export class GoogleAPIRefreshAccessTokenService {
|
||||
) {}
|
||||
|
||||
async refreshAndSaveAccessToken(
|
||||
connectedAccount: ConnectedAccountWorkspaceEntity,
|
||||
workspaceId: string,
|
||||
connectedAccountId: string,
|
||||
): Promise<string> {
|
||||
const connectedAccount = await this.connectedAccountRepository.getById(
|
||||
connectedAccountId,
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
if (!connectedAccount) {
|
||||
throw new Error(
|
||||
`No connected account found for ${connectedAccountId} in workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const refreshToken = connectedAccount.refreshToken;
|
||||
|
||||
if (!refreshToken) {
|
||||
throw new Error(
|
||||
`No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId}`,
|
||||
`No refresh token found for connected account ${connectedAccount.id} in workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const accessToken = await this.refreshAccessToken(refreshToken);
|
||||
|
||||
await this.connectedAccountRepository.updateAccessToken(
|
||||
accessToken,
|
||||
connectedAccountId,
|
||||
connectedAccount.id,
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
await this.connectedAccountRepository.updateAccessToken(
|
||||
accessToken,
|
||||
connectedAccount.id,
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
|
||||
@ -17,6 +17,8 @@ import { WorkspaceField } from 'src/engine/twenty-orm/decorators/workspace-field
|
||||
import { WorkspaceRelation } from 'src/engine/twenty-orm/decorators/workspace-relation.decorator';
|
||||
import { WorkspaceIsNullable } from 'src/engine/twenty-orm/decorators/workspace-is-nullable.decorator';
|
||||
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
||||
import { FeatureFlagKeys } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
import { WorkspaceGate } from 'src/engine/twenty-orm/decorators/workspace-gate.decorator';
|
||||
import { WorkspaceJoinColumn } from 'src/engine/twenty-orm/decorators/workspace-join-column.decorator';
|
||||
|
||||
export enum ConnectedAccountProvider {
|
||||
@ -89,6 +91,18 @@ export class ConnectedAccountWorkspaceEntity extends BaseWorkspaceEntity {
|
||||
@WorkspaceIsNullable()
|
||||
authFailedAt: Date | null;
|
||||
|
||||
@WorkspaceField({
|
||||
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.emailAliases,
|
||||
type: FieldMetadataType.TEXT,
|
||||
label: 'Email Aliases',
|
||||
description: 'Email Aliases',
|
||||
icon: 'IconMail',
|
||||
})
|
||||
@WorkspaceGate({
|
||||
featureFlag: FeatureFlagKeys.IsMessagingAliasFetchingEnabled,
|
||||
})
|
||||
emailAliases: string;
|
||||
|
||||
@WorkspaceRelation({
|
||||
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.accountOwner,
|
||||
type: RelationMetadataType.MANY_TO_ONE,
|
||||
|
||||
Reference in New Issue
Block a user