Skip to content

Commit

Permalink
Fix contact creation and rename email aliases to handle aliases (twen…
Browse files Browse the repository at this point in the history
…tyhq#6176)

Fix contact creation (linked to twentyhq#6162) and rename email aliases to
handle aliases
  • Loading branch information
bosiraphael authored Jul 9, 2024
1 parent 881613e commit 2838700
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const TwoConnectedAccounts: Story = {
provider: 'google',
accountOwnerId: '20202020-03f2-4d83-b0d5-2ec2bcee72d4',
lastSyncHistoryId: '',
emailAliases: '',
handleAliases: '',
handle: 'test.test@gmail.com',
authFailedAt: null,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const TwoConnectedAccounts: Story = {
provider: 'google',
accountOwnerId: '20202020-03f2-4d83-b0d5-2ec2bcee72d4',
lastSyncHistoryId: '',
emailAliases: '',
handleAliases: '',
handle: 'test.test@gmail.com',
authFailedAt: null,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const CONNECTED_ACCOUNT_STANDARD_FIELD_IDS = {
authFailedAt: '20202020-d268-4c6b-baff-400d402b430a',
messageChannels: '20202020-24f7-4362-8468-042204d1e445',
calendarChannels: '20202020-af4a-47bb-99ec-51911c1d3977',
emailAliases: '20202020-8a3d-46be-814f-6228af16c47b',
handleAliases: '20202020-8a3d-46be-814f-6228af16c47b',
};

export const EVENT_STANDARD_FIELD_IDS = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Contact } from 'src/modules/connected-account/auto-companies-and-contacts-creation/types/contact.type';
import { getDomainNameFromHandle } from 'src/modules/connected-account/auto-companies-and-contacts-creation/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';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';

export function filterOutSelfAndContactsFromCompanyOrWorkspace(
contacts: Contact[],
Expand All @@ -10,7 +10,7 @@ export function filterOutSelfAndContactsFromCompanyOrWorkspace(
): Contact[] {
const selfDomainName = getDomainNameFromHandle(connectedAccount.handle);

const emailAliases = connectedAccount.emailAliases?.split(',') || [];
const handleAliases = connectedAccount.handleAliases?.split(',') || [];

const workspaceMembersMap = workspaceMembers.reduce(
(map, workspaceMember) => {
Expand All @@ -25,6 +25,6 @@ export function filterOutSelfAndContactsFromCompanyOrWorkspace(
(contact) =>
getDomainNameFromHandle(contact.handle) !== selfDomainName &&
!workspaceMembersMap[contact.handle] &&
!emailAliases.includes(contact.handle),
!handleAliases.includes(contact.handle),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class GoogleEmailAliasManagerService {
private readonly oAuth2ClientManagerService: OAuth2ClientManagerService,
) {}

public async getEmailAliases(
public async getHandleAliases(
connectedAccount: ConnectedAccountWorkspaceEntity,
) {
const oAuth2Client =
Expand All @@ -29,7 +29,7 @@ export class GoogleEmailAliasManagerService {

const emailAddresses = emailsResponse.data.emailAddresses;

const emailAliases =
const handleAliases =
emailAddresses
?.filter((emailAddress) => {
return emailAddress.metadata?.primary !== true;
Expand All @@ -38,6 +38,6 @@ export class GoogleEmailAliasManagerService {
return emailAddress.value || '';
}) || [];

return emailAliases;
return handleAliases;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ export class EmailAliasManagerService {
private readonly googleEmailAliasManagerService: GoogleEmailAliasManagerService,
) {}

public async refreshEmailAliases(
public async refreshHandleAliases(
connectedAccount: ConnectedAccountWorkspaceEntity,
workspaceId: string,
) {
let emailAliases: string[];
let handleAliases: string[];

switch (connectedAccount.provider) {
case 'google':
emailAliases =
await this.googleEmailAliasManagerService.getEmailAliases(
handleAliases =
await this.googleEmailAliasManagerService.getHandleAliases(
connectedAccount,
);
break;
Expand All @@ -32,8 +32,8 @@ export class EmailAliasManagerService {
);
}

await this.connectedAccountRepository.updateEmailAliases(
emailAliases,
await this.connectedAccountRepository.updateHandleAliases(
handleAliases,
connectedAccount.id,
workspaceId,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ export class ConnectedAccountRepository {
return connectedAccount;
}

public async updateEmailAliases(
emailAliases: string[],
public async updateHandleAliases(
handleAliases: string[],
connectedAccountId: string,
workspaceId: string,
transactionManager?: EntityManager,
Expand All @@ -317,9 +317,9 @@ export class ConnectedAccountRepository {
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],
`UPDATE ${dataSourceSchema}."connectedAccount" SET "handleAliases" = $1 WHERE "id" = $2`,
// TODO: modify handleAliases to be of fieldmetadatatype array
[handleAliases.join(','), connectedAccountId],
workspaceId,
transactionManager,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { Relation } from 'src/engine/workspace-manager/workspace-sync-metadata/interfaces/relation.interface';

import { FeatureFlagKeys } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import {
RelationMetadataType,
RelationOnDeleteAction,
} from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
import { CONNECTED_ACCOUNT_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-ids';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
import { BaseWorkspaceEntity } from 'src/engine/twenty-orm/base.workspace-entity';
import { WorkspaceEntity } from 'src/engine/twenty-orm/decorators/workspace-entity.decorator';
import { WorkspaceIsSystem } from 'src/engine/twenty-orm/decorators/workspace-is-system.decorator';
import { WorkspaceIsNotAuditLogged } from 'src/engine/twenty-orm/decorators/workspace-is-not-audit-logged.decorator';
import { WorkspaceField } from 'src/engine/twenty-orm/decorators/workspace-field.decorator';
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 { WorkspaceIsNotAuditLogged } from 'src/engine/twenty-orm/decorators/workspace-is-not-audit-logged.decorator';
import { WorkspaceIsNullable } from 'src/engine/twenty-orm/decorators/workspace-is-nullable.decorator';
import { WorkspaceIsSystem } from 'src/engine/twenty-orm/decorators/workspace-is-system.decorator';
import { WorkspaceJoinColumn } from 'src/engine/twenty-orm/decorators/workspace-join-column.decorator';
import { WorkspaceRelation } from 'src/engine/twenty-orm/decorators/workspace-relation.decorator';
import { CONNECTED_ACCOUNT_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-ids';
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';

export enum ConnectedAccountProvider {
GOOGLE = 'google',
Expand Down Expand Up @@ -92,16 +92,16 @@ export class ConnectedAccountWorkspaceEntity extends BaseWorkspaceEntity {
authFailedAt: Date | null;

@WorkspaceField({
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.emailAliases,
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.handleAliases,
type: FieldMetadataType.TEXT,
label: 'Email Aliases',
description: 'Email Aliases',
label: 'Handle Aliases',
description: 'Handle Aliases',
icon: 'IconMail',
})
@WorkspaceGate({
featureFlag: FeatureFlagKeys.IsMessagingAliasFetchingEnabled,
})
emailAliases: string;
handleAliases: string;

@WorkspaceRelation({
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.accountOwner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class MessagingMessageService {

const messageDirection =
connectedAccount.handle === message.fromHandle ||
connectedAccount.emailAliases?.includes(message.fromHandle)
connectedAccount.handleAliases?.includes(message.fromHandle)
? 'outgoing'
: 'incoming';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class MessagingGmailMessagesImportService {
)
) {
try {
await this.emailAliasManagerService.refreshEmailAliases(
await this.emailAliasManagerService.refreshHandleAliases(
connectedAccount,
workspaceId,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class MessagingSaveMessagesAndEnqueueContactCreationService {
workspaceId,
);

const emailAliases = connectedAccount.emailAliases?.split(',') || [];
const handleAliases = connectedAccount.handleAliases?.split(',') || [];

let savedMessageParticipants: MessageParticipantWorkspaceEntity[] = [];

Expand All @@ -80,10 +80,11 @@ export class MessagingSaveMessagesAndEnqueueContactCreationService {
'';

const isMessageSentByConnectedAccount =
emailAliases.includes(fromHandle);
handleAliases.includes(fromHandle) ||
fromHandle === connectedAccount.handle;

const isParticipantConnectedAccount =
emailAliases.includes(participant.handle) ||
handleAliases.includes(participant.handle) ||
participant.handle === connectedAccount.handle;

const isExcludedByNonProfessionalEmails =
Expand Down

0 comments on commit 2838700

Please sign in to comment.