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;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
companyId: string;
|
||||
companyId?: string;
|
||||
}[],
|
||||
workspaceId: string,
|
||||
transactionManager?: EntityManager,
|
||||
|
||||
@ -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/perso
|
||||
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 { 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.com
|
||||
const 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],
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ import { getFirstNameAndLastNameFromHandleAndDisplayName } from 'src/workspace/m
|
||||
type 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()
|
||||
|
||||
@ -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 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user