3933 filter non work email from auto contact creation (#4131)
* use isWorkEmail * working * improvement * Refactor lodash import in create-companies-and-contacts.service.ts * refactor lodash import
This commit is contained in:
@ -35,7 +35,7 @@ export class PersonService {
|
|||||||
handle: string;
|
handle: string;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
companyId: string;
|
companyId?: string;
|
||||||
}[],
|
}[],
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
transactionManager?: EntityManager,
|
transactionManager?: EntityManager,
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
|
|
||||||
import { EntityManager } from 'typeorm';
|
import { EntityManager } from 'typeorm';
|
||||||
|
import compact from 'lodash/compact';
|
||||||
|
|
||||||
import { Participant } from 'src/workspace/messaging/types/gmail-message';
|
import { Participant } from 'src/workspace/messaging/types/gmail-message';
|
||||||
import { getDomainNameFromHandle } from 'src/workspace/messaging/utils/get-domain-name-from-handle.util';
|
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/perso
|
|||||||
import { WorkspaceMemberService } from 'src/workspace/messaging/repositories/workspace-member/workspace-member.service';
|
import { 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 { 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 { filterOutParticipantsFromCompanyOrWorkspace } from 'src/workspace/messaging/utils/filter-out-participants-from-company-or-workspace.util';
|
||||||
|
import { isWorkEmail } from 'src/utils/is-work-email';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CreateCompaniesAndContactsService {
|
export class CreateCompaniesAndContactsService {
|
||||||
@ -30,13 +32,15 @@ export class CreateCompaniesAndContactsService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: This is a feature that may be implemented in the future
|
||||||
|
const isContactAutoCreationForNonWorkEmailsEnabled = false;
|
||||||
|
|
||||||
const workspaceMembers =
|
const workspaceMembers =
|
||||||
await this.workspaceMemberService.getAllByWorkspaceId(
|
await this.workspaceMemberService.getAllByWorkspaceId(
|
||||||
workspaceId,
|
workspaceId,
|
||||||
transactionManager,
|
transactionManager,
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: use isWorkEmail so we can create a contact even if the email is a personal email ex: @gmail.com
|
|
||||||
const participantsFromOtherCompanies =
|
const participantsFromOtherCompanies =
|
||||||
filterOutParticipantsFromCompanyOrWorkspace(
|
filterOutParticipantsFromCompanyOrWorkspace(
|
||||||
participants,
|
participants,
|
||||||
@ -59,18 +63,24 @@ export class CreateCompaniesAndContactsService {
|
|||||||
const filteredParticipants = uniqueParticipants.filter(
|
const filteredParticipants = uniqueParticipants.filter(
|
||||||
(participant) =>
|
(participant) =>
|
||||||
!alreadyCreatedContactEmails.includes(participant.handle) &&
|
!alreadyCreatedContactEmails.includes(participant.handle) &&
|
||||||
participant.handle.includes('@'),
|
participant.handle.includes('@') &&
|
||||||
|
(isContactAutoCreationForNonWorkEmailsEnabled ||
|
||||||
|
isWorkEmail(participant.handle)),
|
||||||
);
|
);
|
||||||
|
|
||||||
const filteredParticipantsWithCompanyDomainNames =
|
const filteredParticipantsWithCompanyDomainNames =
|
||||||
filteredParticipants?.map((participant) => ({
|
filteredParticipants?.map((participant) => ({
|
||||||
handle: participant.handle,
|
handle: participant.handle,
|
||||||
displayName: participant.displayName,
|
displayName: participant.displayName,
|
||||||
companyDomainName: getDomainNameFromHandle(participant.handle),
|
companyDomainName: isWorkEmail(participant.handle)
|
||||||
|
? getDomainNameFromHandle(participant.handle)
|
||||||
|
: undefined,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const domainNamesToCreate = filteredParticipantsWithCompanyDomainNames.map(
|
const domainNamesToCreate = compact(
|
||||||
(participant) => participant.companyDomainName,
|
filteredParticipantsWithCompanyDomainNames.map(
|
||||||
|
(participant) => participant.companyDomainName,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
const companiesObject = await this.createCompaniesService.createCompanies(
|
const companiesObject = await this.createCompaniesService.createCompanies(
|
||||||
@ -83,7 +93,9 @@ export class CreateCompaniesAndContactsService {
|
|||||||
(participant) => ({
|
(participant) => ({
|
||||||
handle: participant.handle,
|
handle: participant.handle,
|
||||||
displayName: participant.displayName,
|
displayName: participant.displayName,
|
||||||
companyId: companiesObject[participant.companyDomainName],
|
companyId:
|
||||||
|
participant.companyDomainName &&
|
||||||
|
companiesObject[participant.companyDomainName],
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import { getFirstNameAndLastNameFromHandleAndDisplayName } from 'src/workspace/m
|
|||||||
type ContactToCreate = {
|
type ContactToCreate = {
|
||||||
handle: string;
|
handle: string;
|
||||||
displayName: string;
|
displayName: string;
|
||||||
companyId: string;
|
companyId?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type FormattedContactToCreate = {
|
type FormattedContactToCreate = {
|
||||||
@ -17,7 +17,7 @@ type FormattedContactToCreate = {
|
|||||||
handle: string;
|
handle: string;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
companyId: string;
|
companyId?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
|||||||
@ -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[]): {
|
export function getUniqueParticipantsAndHandles(participants: Participant[]): {
|
||||||
uniqueParticipants: Participant[];
|
uniqueParticipants: Participant[];
|
||||||
uniqueHandles: string[];
|
uniqueHandles: string[];
|
||||||
@ -8,17 +9,11 @@ export function getUniqueParticipantsAndHandles(participants: Participant[]): {
|
|||||||
return { uniqueParticipants: [], uniqueHandles: [] };
|
return { uniqueParticipants: [], uniqueHandles: [] };
|
||||||
}
|
}
|
||||||
|
|
||||||
const uniqueHandles = Array.from(
|
const uniqueHandles = uniq(
|
||||||
new Set(participants.map((participant) => participant.handle)),
|
participants.map((participant) => participant.handle),
|
||||||
);
|
);
|
||||||
|
|
||||||
const uniqueParticipants = uniqueHandles.map((handle) => {
|
const uniqueParticipants = uniqBy(participants, 'handle');
|
||||||
const participant = participants.find(
|
|
||||||
(participant) => participant.handle === handle,
|
|
||||||
);
|
|
||||||
|
|
||||||
return participant;
|
|
||||||
}) as Participant[];
|
|
||||||
|
|
||||||
return { uniqueParticipants, uniqueHandles };
|
return { uniqueParticipants, uniqueHandles };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user