Skip to content

Commit

Permalink
Improve messaging/calendar create contact performance (twentyhq#5314)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
charlesBochet committed May 6, 2024
1 parent 5f467ab commit a2017ea
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export class CalendarEventParticipantRepository {
handle: 'text',
displayName: 'text',
isOrganizer: 'boolean',
responseStatus: `${dataSourceSchema}."calendarEventParticipant_responsestatus_enum"`,
responseStatus: `${dataSourceSchema}."calendarEventParticipant_responseStatus_enum"`,
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CalendarEventParticipant } from 'src/modules/calendar/types/calendar-ev
import { CalendarEventParticipantRepository } from 'src/modules/calendar/repositories/calendar-event-participant.repository';
import { CalendarEventParticipantObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-participant.object-metadata';
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';

@Injectable()
export class CalendarEventParticipantService {
Expand All @@ -24,11 +25,13 @@ export class CalendarEventParticipantService {
) {}

public async updateCalendarEventParticipantsAfterPeopleCreation(
createdPeople: ObjectRecord<PersonObjectMetadata>[],
workspaceId: string,
transactionManager?: EntityManager,
): Promise<void> {
const participants =
await this.calendarEventParticipantRepository.getWithoutPersonIdAndWorkspaceMemberId(
await this.calendarEventParticipantRepository.getByHandles(
createdPeople.map((person) => person.email),
workspaceId,
);

Expand Down Expand Up @@ -102,7 +105,7 @@ export class CalendarEventParticipantService {
handle: 'text',
displayName: 'text',
isOrganizer: 'boolean',
responseStatus: `${dataSourceSchema}."calendarEventParticipant_responsestatus_enum"`,
responseStatus: `${dataSourceSchema}."calendarEventParticipant_responseStatus_enum"`,
personId: 'uuid',
workspaceMemberId: 'uuid',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -68,7 +69,7 @@ export class CreateCompanyAndContactService {
);

if (uniqueHandles.length === 0) {
return;
return [];
}

const alreadyCreatedContacts = await this.personRepository.getByEmails(
Expand Down Expand Up @@ -120,7 +121,7 @@ export class CreateCompanyAndContactService {
: undefined,
}));

await this.createContactService.createContacts(
return await this.createContactService.createPeople(
formattedContactsToCreate,
workspaceId,
transactionManager,
Expand All @@ -139,19 +140,21 @@ export class CreateCompanyAndContactService {

await workspaceDataSource?.transaction(
async (transactionManager: EntityManager) => {
await this.createCompaniesAndContacts(
const createdPeople = await this.createCompaniesAndPeople(
connectedAccountHandle,
contactsToCreate,
workspaceId,
transactionManager,
);

await this.messageParticipantService.updateMessageParticipantsAfterPeopleCreation(
createdPeople,
workspaceId,
transactionManager,
);

await this.calendarEventParticipantService.updateCalendarEventParticipantsAfterPeopleCreation(
createdPeople,
workspaceId,
transactionManager,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/works
import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata';
import { FetchMessagesByBatchesModule } from 'src/modules/messaging/services/fetch-messages-by-batches/fetch-messages-by-batches.module';
import { GmailFetchMessageContentFromCacheService } from 'src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.service';
import { MessageParticipantModule } from 'src/modules/messaging/services/message-participant/message-participant.module';
import { MessageModule } from 'src/modules/messaging/services/message/message.module';
import { SaveMessageAndEmitContactCreationEventModule } from 'src/modules/messaging/services/save-message-and-emit-contact-creation-event/save-message-and-emit-contact-creation-event.module';
import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel.object-metadata';

@Module({
Expand All @@ -16,9 +16,10 @@ import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-obj
ConnectedAccountObjectMetadata,
MessageChannelObjectMetadata,
]),
SaveMessageAndEmitContactCreationEventModule,
MessageModule,
WorkspaceDataSourceModule,
MessageModule,
MessageParticipantModule,
],
providers: [GmailFetchMessageContentFromCacheService],
exports: [GmailFetchMessageContentFromCacheService],
Expand Down
Loading

0 comments on commit a2017ea

Please sign in to comment.