Improve messaging/calendar create contact performance (#5314)

In this PR, I'm refactoring the way we associate messageParticipant post
person/company creation. Instead of looking a all person without
participant, we are passing the one that were just created.

Also, I'm making sure the message and messageParticipant creation
transaction is commited before creating person/company creation (and
then messageParticipant association)
This commit is contained in:
Charles Bochet
2024-05-06 23:43:18 +02:00
committed by GitHub
parent 5f467ab5ca
commit a2017eaeb7
11 changed files with 154 additions and 336 deletions

View File

@ -7,6 +7,7 @@ import { PersonRepository } from 'src/modules/person/repositories/person.reposit
import { getFirstNameAndLastNameFromHandleAndDisplayName } from 'src/modules/calendar-messaging-participant/utils/get-first-name-and-last-name-from-handle-and-display-name.util';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { PersonObjectMetadata } from 'src/modules/person/standard-objects/person.object-metadata';
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
type ContactToCreate = {
handle: string;
@ -50,16 +51,16 @@ export class CreateContactService {
});
}
public async createContacts(
public async createPeople(
contactsToCreate: ContactToCreate[],
workspaceId: string,
transactionManager?: EntityManager,
): Promise<void> {
if (contactsToCreate.length === 0) return;
): Promise<ObjectRecord<PersonObjectMetadata>[]> {
if (contactsToCreate.length === 0) return [];
const formattedContacts = this.formatContacts(contactsToCreate);
await this.personRepository.createPeople(
return await this.personRepository.createPeople(
formattedContacts,
workspaceId,
transactionManager,

View File

@ -20,6 +20,7 @@ import { MessageParticipantService } from 'src/modules/messaging/services/messag
import { CalendarEventParticipantService } from 'src/modules/calendar/services/calendar-event-participant/calendar-event-participant.service';
import { filterOutContactsFromCompanyOrWorkspace } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/filter-out-contacts-from-company-or-workspace.util';
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
@Injectable()
export class CreateCompanyAndContactService {
@ -37,14 +38,14 @@ export class CreateCompanyAndContactService {
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
) {}
async createCompaniesAndContacts(
async createCompaniesAndPeople(
connectedAccountHandle: string,
contactsToCreate: Contacts,
workspaceId: string,
transactionManager?: EntityManager,
) {
): Promise<ObjectRecord<PersonObjectMetadata>[]> {
if (!contactsToCreate || contactsToCreate.length === 0) {
return;
return [];
}
// TODO: This is a feature that may be implemented in the future
@ -68,7 +69,7 @@ export class CreateCompanyAndContactService {
);
if (uniqueHandles.length === 0) {
return;
return [];
}
const alreadyCreatedContacts = await this.personRepository.getByEmails(
@ -120,7 +121,7 @@ export class CreateCompanyAndContactService {
: undefined,
}));
await this.createContactService.createContacts(
return await this.createContactService.createPeople(
formattedContactsToCreate,
workspaceId,
transactionManager,
@ -139,7 +140,7 @@ export class CreateCompanyAndContactService {
await workspaceDataSource?.transaction(
async (transactionManager: EntityManager) => {
await this.createCompaniesAndContacts(
const createdPeople = await this.createCompaniesAndPeople(
connectedAccountHandle,
contactsToCreate,
workspaceId,
@ -147,11 +148,13 @@ export class CreateCompanyAndContactService {
);
await this.messageParticipantService.updateMessageParticipantsAfterPeopleCreation(
createdPeople,
workspaceId,
transactionManager,
);
await this.calendarEventParticipantService.updateCalendarEventParticipantsAfterPeopleCreation(
createdPeople,
workspaceId,
transactionManager,
);