The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
3933 filter non work email from auto contact creation (#4131)
bosiraphael4e798ba2a399
diff --git a/packages/twenty-server/src/workspace/messaging/repositories/person/person.service.ts b/packages/twenty-server/src/workspace/messaging/repositories/person/person.service.tsindex d78f6c42219..912014db510 100644--- a/packages/twenty-server/src/workspace/messaging/repositories/person/person.service.ts+++ b/packages/twenty-server/src/workspace/messaging/repositories/person/person.service.ts@@ -35,7 +35,7 @@ export class PersonService {handle: string;firstName: string;lastName: string;- companyId: string;+ companyId?: string;}[],workspaceId: string,transactionManager?: EntityManager,diff --git a/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.service.ts b/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.service.tsindex 9ff76c479e5..1dbd48f43cd 100644--- a/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.service.ts+++ b/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.service.ts@@ -1,6 +1,7 @@import { Injectable } from '@nestjs/common';import { EntityManager } from 'typeorm';+import compact from 'lodash/compact';import { Participant } from 'src/workspace/messaging/types/gmail-message';import { getDomainNameFromHandle } from 'src/workspace/messaging/utils/get-domain-name-from-handle.util';@@ -10,6 +11,7 @@ import { PersonService } from 'src/workspace/messaging/repositories/person/persoimport { WorkspaceMemberService } from 'src/workspace/messaging/repositories/workspace-member/workspace-member.service';import { getUniqueParticipantsAndHandles } from 'src/workspace/messaging/utils/get-unique-participants-and-handles.util';import { filterOutParticipantsFromCompanyOrWorkspace } from 'src/workspace/messaging/utils/filter-out-participants-from-company-or-workspace.util';+import { isWorkEmail } from 'src/utils/is-work-email';@Injectable()export class CreateCompaniesAndContactsService {@@ -30,13 +32,15 @@ export class CreateCompaniesAndContactsService {return;}+ // TODO: This is a feature that may be implemented in the future+ const isContactAutoCreationForNonWorkEmailsEnabled = false;+const workspaceMembers =await this.workspaceMemberService.getAllByWorkspaceId(workspaceId,transactionManager,);- // TODO: use isWorkEmail so we can create a contact even if the email is a personal email ex: @gmail.comconst participantsFromOtherCompanies =filterOutParticipantsFromCompanyOrWorkspace(participants,@@ -59,18 +63,24 @@ export class CreateCompaniesAndContactsService {const filteredParticipants = uniqueParticipants.filter((participant) =>!alreadyCreatedContactEmails.includes(participant.handle) &&- participant.handle.includes('@'),+ participant.handle.includes('@') &&+ (isContactAutoCreationForNonWorkEmailsEnabled ||+ isWorkEmail(participant.handle)),);const filteredParticipantsWithCompanyDomainNames =filteredParticipants?.map((participant) => ({handle: participant.handle,displayName: participant.displayName,- companyDomainName: getDomainNameFromHandle(participant.handle),+ companyDomainName: isWorkEmail(participant.handle)+ ? getDomainNameFromHandle(participant.handle)+ : undefined,}));- const domainNamesToCreate = filteredParticipantsWithCompanyDomainNames.map(- (participant) => participant.companyDomainName,+ const domainNamesToCreate = compact(+ filteredParticipantsWithCompanyDomainNames.map(+ (participant) => participant.companyDomainName,+ ),);const companiesObject = await this.createCompaniesService.createCompanies(@@ -83,7 +93,9 @@ export class CreateCompaniesAndContactsService {(participant) => ({handle: participant.handle,displayName: participant.displayName,- companyId: companiesObject[participant.companyDomainName],+ companyId:+ participant.companyDomainName &&+ companiesObject[participant.companyDomainName],}),);diff --git a/packages/twenty-server/src/workspace/messaging/services/create-contact/create-contact.service.ts b/packages/twenty-server/src/workspace/messaging/services/create-contact/create-contact.service.tsindex e10ee703414..2e23af46bfd 100644--- a/packages/twenty-server/src/workspace/messaging/services/create-contact/create-contact.service.ts+++ b/packages/twenty-server/src/workspace/messaging/services/create-contact/create-contact.service.ts@@ -9,7 +9,7 @@ import { getFirstNameAndLastNameFromHandleAndDisplayName } from 'src/workspace/mtype ContactToCreate = {handle: string;displayName: string;- companyId: string;+ companyId?: string;};type FormattedContactToCreate = {@@ -17,7 +17,7 @@ type FormattedContactToCreate = {handle: string;firstName: string;lastName: string;- companyId: string;+ companyId?: string;};@Injectable()diff --git a/packages/twenty-server/src/workspace/messaging/utils/get-unique-participants-and-handles.util.ts b/packages/twenty-server/src/workspace/messaging/utils/get-unique-participants-and-handles.util.tsindex bfe455020b2..9e01ac02423 100644--- a/packages/twenty-server/src/workspace/messaging/utils/get-unique-participants-and-handles.util.ts+++ b/packages/twenty-server/src/workspace/messaging/utils/get-unique-participants-and-handles.util.ts@@ -1,5 +1,6 @@-import { Participant } from 'src/workspace/messaging/types/gmail-message';+import { uniq, uniqBy } from 'lodash';+import { Participant } from 'src/workspace/messaging/types/gmail-message';export function getUniqueParticipantsAndHandles(participants: Participant[]): {uniqueParticipants: Participant[];uniqueHandles: string[];@@ -8,17 +9,11 @@ export function getUniqueParticipantsAndHandles(participants: Participant[]): {return { uniqueParticipants: [], uniqueHandles: [] };}- const uniqueHandles = Array.from(- new Set(participants.map((participant) => participant.handle)),+ const uniqueHandles = uniq(+ participants.map((participant) => participant.handle),);- const uniqueParticipants = uniqueHandles.map((handle) => {- const participant = participants.find(- (participant) => participant.handle === handle,- );-- return participant;- }) as Participant[];+ const uniqueParticipants = uniqBy(participants, 'handle');return { uniqueParticipants, uniqueHandles };}