3807 auto creation of contact when importing emails (#3888)
* Add CreateContactService to messaging services * Add logic to create a contact if it doesn't exist * Add name * Improvements * contact creation working * fix bug * Add IsPersonEmailService to check if an email is personal or not * filter is working * improve filter * create companies and people * Refactor createContactFromHandleAndDisplayName to createContactAndCompanyFromHandleAndDisplayName * improve regex * reorganizing services * updates * reorganize folders * wip * use transaction * wip * wip * wip * batch queries * almost working * working
This commit is contained in:
@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { CreateContactService } from 'src/workspace/messaging/create-contact/create-contact.service';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
providers: [CreateContactService],
|
||||
exports: [CreateContactService],
|
||||
})
|
||||
export class CreateContactModule {}
|
||||
@ -0,0 +1,88 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { EntityManager } from 'typeorm';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
import { DataSourceEntity } from 'src/metadata/data-source/data-source.entity';
|
||||
import { capitalize } from 'src/utils/capitalize';
|
||||
|
||||
type ContactToCreate = {
|
||||
handle: string;
|
||||
displayName: string;
|
||||
companyId: string;
|
||||
};
|
||||
|
||||
type FormattedContactToCreate = {
|
||||
id: string;
|
||||
handle: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
companyId: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class CreateContactService {
|
||||
constructor() {}
|
||||
|
||||
formatContacts(
|
||||
contactsToCreate: ContactToCreate[],
|
||||
): FormattedContactToCreate[] {
|
||||
return contactsToCreate.map((contact) => {
|
||||
const { handle, displayName, companyId } = contact;
|
||||
|
||||
const contactFirstName = displayName.split(' ')[0];
|
||||
const contactLastName = displayName.split(' ')[1];
|
||||
|
||||
const contactFullNameFromHandle = handle.split('@')[0];
|
||||
const contactFirstNameFromHandle =
|
||||
contactFullNameFromHandle.split('.')[0];
|
||||
const contactLastNameFromHandle = contactFullNameFromHandle.split('.')[1];
|
||||
|
||||
const id = v4();
|
||||
|
||||
return {
|
||||
id,
|
||||
handle,
|
||||
firstName: capitalize(
|
||||
contactFirstName || contactFirstNameFromHandle || '',
|
||||
),
|
||||
lastName: capitalize(
|
||||
contactLastName || contactLastNameFromHandle || '',
|
||||
),
|
||||
companyId,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async createContacts(
|
||||
contactsToCreate: ContactToCreate[],
|
||||
dataSourceMetadata: DataSourceEntity,
|
||||
manager: EntityManager,
|
||||
): Promise<void> {
|
||||
if (contactsToCreate.length === 0) return;
|
||||
|
||||
const formattedContacts = this.formatContacts(contactsToCreate);
|
||||
|
||||
const valuesString = formattedContacts
|
||||
.map(
|
||||
(_, index) =>
|
||||
`($${index * 5 + 1}, $${index * 5 + 2}, $${index * 5 + 3}, $${
|
||||
index * 5 + 4
|
||||
}, $${index * 5 + 5})`,
|
||||
)
|
||||
.join(', ');
|
||||
|
||||
await manager.query(
|
||||
`INSERT INTO ${dataSourceMetadata.schema}.person (id, email, "nameFirstName", "nameLastName", "companyId") VALUES ${valuesString}`,
|
||||
formattedContacts
|
||||
.map((contact) => [
|
||||
contact.id,
|
||||
contact.handle,
|
||||
contact.firstName,
|
||||
contact.lastName,
|
||||
contact.companyId,
|
||||
])
|
||||
.flat(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user