The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Match participants on the transaction that inserted them (#24525)
neo7730dce0b5b13a7
diff --git a/.github/workflows/ci-server.yaml b/.github/workflows/ci-server.yamlindex c03d703015a..15c2bd4470c 100644--- a/.github/workflows/ci-server.yaml+++ b/.github/workflows/ci-server.yaml@@ -322,7 +322,7 @@ jobs:# the 16-way split the unflagged shards use.ORM_V2_SHARD_COUNTER: 5TEST_FORCED_FEATURE_FLAGS: ${{ matrix.orm-v2 && 'IS_ORM_V2_READ_PATH_ENABLED' || '' }}- ORM_V2_TEST_PATH_PATTERN: '(find-many|find-one|find-duplicates|group-by|composite-field-pagination|filter-by-relation-field|order-by-relation-field|nested-relation|soft-deleted-relation|relation-join-rls|object-records-permissions|merge-many|mutate-by-relation-field|mutation-relation-filter-rls|upsert|unique-field|object-generated|files-field|timeline-from-object-record)'+ ORM_V2_TEST_PATH_PATTERN: '(find-many|find-one|find-duplicates|group-by|composite-field-pagination|filter-by-relation-field|order-by-relation-field|nested-relation|soft-deleted-relation|relation-join-rls|object-records-permissions|merge-many|mutate-by-relation-field|mutation-relation-filter-rls|upsert|unique-field|object-generated|files-field|timeline-from-object-record|participant-matching)'steps:- name: Fetch custom Github Actions and base branch historyuses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service.ts b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service.tsindex 22bd2381e23..001a5ea03ce 100644--- a/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service.ts+++ b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service.ts@@ -178,6 +178,7 @@ export class CalendarEventParticipantService {objectMetadataName: 'calendarEventParticipant',matchWith: 'workspaceMemberAndPerson',workspaceId,+ transactionScope,});},authContext,diff --git a/packages/twenty-server/src/modules/match-participant/match-participant.service.ts b/packages/twenty-server/src/modules/match-participant/match-participant.service.tsindex 9b098b2cf01..54de38ee7cb 100644--- a/packages/twenty-server/src/modules/match-participant/match-participant.service.ts+++ b/packages/twenty-server/src/modules/match-participant/match-participant.service.ts@@ -5,6 +5,7 @@ import { isDefined } from 'twenty-shared/utils';import { Any, In } from 'typeorm';import { GlobalWorkspaceOrmManager } from 'src/engine/twenty-orm/global-workspace-datasource/global-workspace-orm.manager';+import { type WorkspaceTransactionScope } from 'src/engine/twenty-orm/global-workspace-datasource/types/workspace-transaction-scope.type';import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';import { type CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';@@ -16,6 +17,12 @@ import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/statype ObjectMetadataName = 'messageParticipant' | 'calendarEventParticipant';+type GetParticipantRepositoryArgs = {+ workspaceId: string;+ objectMetadataName: ObjectMetadataName;+ transactionScope?: WorkspaceTransactionScope;+};+type MatchParticipantsForWorkspaceMembersArgs = {participantMatching: {workspaceMemberIds: string[];@@ -48,6 +55,7 @@ type MatchParticipantsArgs<objectMetadataName: ObjectMetadataName;matchWith: 'workspaceMemberOnly' | 'personOnly' | 'workspaceMemberAndPerson';workspaceId: string;+ transactionScope?: WorkspaceTransactionScope;};@Injectable()@@ -61,17 +69,30 @@ export class MatchParticipantService<private readonly globalWorkspaceOrmManager: GlobalWorkspaceOrmManager,) {}- private async getParticipantRepository(- workspaceId: string,- objectMetadataName: 'messageParticipant' | 'calendarEventParticipant',- ) {+ private async getParticipantRepository({+ workspaceId,+ objectMetadataName,+ transactionScope,+ }: GetParticipantRepositoryArgs) {if (objectMetadataName === 'messageParticipant') {+ if (isDefined(transactionScope)) {+ return transactionScope.getRepository<MessageParticipantWorkspaceEntity>(+ objectMetadataName,+ );+ }+return await this.globalWorkspaceOrmManager.getRepository<MessageParticipantWorkspaceEntity>(workspaceId,objectMetadataName,);}+ if (isDefined(transactionScope)) {+ return transactionScope.getRepository<CalendarEventParticipantWorkspaceEntity>(+ objectMetadataName,+ );+ }+return await this.globalWorkspaceOrmManager.getRepository<CalendarEventParticipantWorkspaceEntity>(workspaceId,objectMetadataName,@@ -83,6 +104,7 @@ export class MatchParticipantService<objectMetadataName,matchWith = 'workspaceMemberAndPerson',workspaceId,+ transactionScope,}: MatchParticipantsArgs<ParticipantWorkspaceEntity>) {if (participants.length === 0) {return;@@ -95,10 +117,11 @@ export class MatchParticipantService<{ shouldBypassPermissionChecks: true },);- const participantRepository = await this.getParticipantRepository(+ const participantRepository = await this.getParticipantRepository({workspaceId,objectMetadataName,- );+ transactionScope,+ });const workspaceMemberRepository =await this.globalWorkspaceOrmManager.getRepository<WorkspaceMemberWorkspaceEntity>(@@ -208,10 +231,10 @@ export class MatchParticipantService<const authContext = buildSystemAuthContext(workspaceId);await this.globalWorkspaceOrmManager.executeInWorkspaceContext(async () => {- const participantRepository = await this.getParticipantRepository(+ const participantRepository = await this.getParticipantRepository({workspaceId,objectMetadataName,- );+ });const participants = await participantRepository.find({where: {@@ -243,10 +266,10 @@ export class MatchParticipantService<const authContext = buildSystemAuthContext(workspaceId);await this.globalWorkspaceOrmManager.executeInWorkspaceContext(async () => {- const participantRepository = await this.getParticipantRepository(+ const participantRepository = await this.getParticipantRepository({workspaceId,objectMetadataName,- );+ });let participantsMatchingPersonEmails: ParticipantWorkspaceEntity[] = [];let participantsMatchingPersonId: ParticipantWorkspaceEntity[] = [];diff --git a/packages/twenty-server/src/modules/messaging/message-participant-manager/services/messaging-message-participant.service.ts b/packages/twenty-server/src/modules/messaging/message-participant-manager/services/messaging-message-participant.service.tsindex 57dbf2d2f3e..39c8557a01c 100644--- a/packages/twenty-server/src/modules/messaging/message-participant-manager/services/messaging-message-participant.service.ts+++ b/packages/twenty-server/src/modules/messaging/message-participant-manager/services/messaging-message-participant.service.ts@@ -74,6 +74,7 @@ export class MessagingMessageParticipantService {objectMetadataName: 'messageParticipant',matchWith: 'workspaceMemberAndPerson',workspaceId,+ transactionScope,});},authContext,diff --git a/packages/twenty-server/test/integration/google/messaging/participant-matching-existing-person.integration-spec.ts b/packages/twenty-server/test/integration/google/messaging/participant-matching-existing-person.integration-spec.tsnew file mode 100644index 00000000000..8a5619a0967--- /dev/null+++ b/packages/twenty-server/test/integration/google/messaging/participant-matching-existing-person.integration-spec.ts@@ -0,0 +1,131 @@+import { randomUUID } from 'node:crypto';++import {+ ConnectedAccountProvider,+ MessageChannelContactAutoCreationPolicy,+} from 'twenty-shared/types';++import { CalendarChannelEntity } from 'src/engine/metadata-modules/calendar-channel/entities/calendar-channel.entity';+import { MessageChannelEntity } from 'src/engine/metadata-modules/message-channel/entities/message-channel.entity';++import { createOneOperationFactory } from 'test/integration/graphql/utils/create-one-operation-factory.util';+import { makeGraphqlAPIRequest } from 'test/integration/graphql/utils/make-graphql-api-request.util';+import { googleCalendarEvent } from 'test/integration/google/mocks/google-calendar-event.util';+import { gmailMessage } from 'test/integration/google/mocks/gmail-message.util';+import { setupGoogleMock } from 'test/integration/google/mocks/setup-google-mock.util';+import { connectMessagingAccount } from 'test/integration/utils/connect-messaging-account.util';+import { findRecordNodesByFilter } from 'test/integration/utils/find-records-by-filter.util';+import { getCoreRepository } from 'test/integration/utils/get-core-repository.util';+import { runCalendarChannelEventsImport } from 'test/integration/utils/run-calendar-channel-events-import.util';+import { runCalendarChannelListFetch } from 'test/integration/utils/run-calendar-channel-list-fetch.util';+import { runMessageChannelSync } from 'test/integration/utils/run-message-channel-sync.util';+import { waitForAllJobsToFinish } from 'test/integration/utils/wait-for-all-jobs-to-finish.util';++const HANDLE = '[email protected]';++const KNOWN_SENDER = `known-sender-${randomUUID()}@acme.com`;+const KNOWN_ATTENDEE = `known-attendee-${randomUUID()}@acme.com`;++const findParticipantPersonIds = async (+ objectMetadataSingularName: string,+ objectMetadataPluralName: string,+ handle: string,+) => {+ const participants = await findRecordNodesByFilter<{+ personId: string | null;+ }>(objectMetadataSingularName, objectMetadataPluralName, 'personId', {+ handle: { eq: handle },+ });++ return participants.map((participant) => participant.personId);+};++const createPerson = async (primaryEmail: string) => {+ const response = await makeGraphqlAPIRequest(+ createOneOperationFactory({+ objectMetadataSingularName: 'person',+ gqlFields: 'id',+ data: { emails: { primaryEmail } },+ }),+ );++ expect(response.body.errors).toBeUndefined();++ return response.body.data.createPerson.id as string;+};++describe('Participant matching when the person already exists (integration)', () => {+ const gmail = setupGoogleMock({+ handle: HANDLE,+ inbox: [gmailMessage({ from: KNOWN_SENDER, to: HANDLE })],+ });++ let channel: Awaited<ReturnType<typeof connectMessagingAccount>>;+ let senderPersonId: string;+ let attendeePersonId: string;++ beforeAll(async () => {+ senderPersonId = await createPerson(KNOWN_SENDER);+ attendeePersonId = await createPerson(KNOWN_ATTENDEE);++ await waitForAllJobsToFinish();++ channel = await connectMessagingAccount({+ provider: ConnectedAccountProvider.GOOGLE,+ handle: HANDLE,+ });++ await getCoreRepository<MessageChannelEntity>(MessageChannelEntity).update(+ { id: channel.channelId },+ {+ isContactAutoCreationEnabled: false,+ contactAutoCreationPolicy: MessageChannelContactAutoCreationPolicy.NONE,+ },+ );++ await getCoreRepository<CalendarChannelEntity>(+ CalendarChannelEntity,+ ).update(+ { id: channel.calendarChannelId },+ { isContactAutoCreationEnabled: false },+ );++ await runMessageChannelSync(channel.channelId);++ gmail.serveCalendarEvents([+ googleCalendarEvent({+ summary: `Calendar event ${randomUUID()}`,+ attendees: [{ email: KNOWN_ATTENDEE }],+ }),+ ]);++ await runCalendarChannelListFetch(channel.calendarChannelId);+ await runCalendarChannelEventsImport(channel.calendarChannelId);++ await waitForAllJobsToFinish();+ }, 180000);++ afterAll(async () => {+ await channel?.cleanup().catch(() => undefined);+ });++ it('links the imported message participant to the person that already owned the handle', async () => {+ expect(+ await findParticipantPersonIds(+ 'messageParticipant',+ 'messageParticipants',+ KNOWN_SENDER,+ ),+ ).toEqual([senderPersonId]);+ }, 60000);++ it('links the imported calendar event participant to the person that already owned the handle', async () => {+ expect(+ await findParticipantPersonIds(+ 'calendarEventParticipant',+ 'calendarEventParticipants',+ KNOWN_ATTENDEE,+ ),+ ).toEqual([attendeePersonId]);+ }, 60000);+});