The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Fix activity targets after initial company assignment (#26613)
Félix Malfaite3ad36841538
diff --git a/packages/twenty-server/src/modules/match-participant/jobs/reconcile-person-company-targets.job.ts b/packages/twenty-server/src/modules/match-participant/jobs/reconcile-person-company-targets.job.tsnew file mode 100644index 00000000000..756eae0621a--- /dev/null+++ b/packages/twenty-server/src/modules/match-participant/jobs/reconcile-person-company-targets.job.ts@@ -0,0 +1,25 @@+import { Scope } from '@nestjs/common';++import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';+import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';+import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';+import { ParticipantTargetReconciliationService } from 'src/modules/match-participant/participant-target-reconciliation.service';++export type ReconcilePersonCompanyTargetsJobData = {+ workspaceId: string;+ personIds: string[];+};++@Processor({ queueName: MessageQueue.messagingQueue, scope: Scope.REQUEST })+export class ReconcilePersonCompanyTargetsJob {+ constructor(+ private readonly participantTargetReconciliationService: ParticipantTargetReconciliationService,+ ) {}++ @Process(ReconcilePersonCompanyTargetsJob.name)+ async handle(data: ReconcilePersonCompanyTargetsJobData): Promise<void> {+ await this.participantTargetReconciliationService.reconcileTargetsForPeople(+ data,+ );+ }+}diff --git a/packages/twenty-server/src/modules/match-participant/listeners/participant-target-person.listener.ts b/packages/twenty-server/src/modules/match-participant/listeners/participant-target-person.listener.tsnew file mode 100644index 00000000000..40a391640ee--- /dev/null+++ b/packages/twenty-server/src/modules/match-participant/listeners/participant-target-person.listener.ts@@ -0,0 +1,50 @@+import { Injectable } from '@nestjs/common';++import { type ObjectRecordUpdateEvent } from 'twenty-shared/database-events';+import { isDefined } from 'twenty-shared/utils';++import { OnDatabaseBatchEvent } from 'src/engine/api/graphql/graphql-query-runner/decorators/on-database-batch-event.decorator';+import { DatabaseEventAction } from 'src/engine/api/graphql/graphql-query-runner/enums/database-event-action';+import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';+import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';+import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';+import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/types/workspace-event-batch.type';+import {+ ReconcilePersonCompanyTargetsJob,+ type ReconcilePersonCompanyTargetsJobData,+} from 'src/modules/match-participant/jobs/reconcile-person-company-targets.job';+import { type PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';++@Injectable()+export class ParticipantTargetPersonListener {+ constructor(+ @InjectMessageQueue(MessageQueue.messagingQueue)+ private readonly messageQueueService: MessageQueueService,+ ) {}++ @OnDatabaseBatchEvent('person', DatabaseEventAction.UPDATED)+ async handleUpdatedEvent(+ payload: WorkspaceEventBatch<+ ObjectRecordUpdateEvent<PersonWorkspaceEntity>+ >,+ ) {+ const personIds = payload.events+ .filter(+ ({ properties }) =>+ !isDefined(properties.before.companyId) &&+ isDefined(properties.after.companyId),+ )+ .map(({ recordId }) => recordId);++ if (personIds.length === 0) {+ return;+ }++ // Enrichment can attach a company long after participants were matched,+ // without changing an email address or causing another provider sync.+ await this.messageQueueService.add<ReconcilePersonCompanyTargetsJobData>(+ ReconcilePersonCompanyTargetsJob.name,+ { workspaceId: payload.workspaceId, personIds },+ );+ }+}diff --git a/packages/twenty-server/src/modules/match-participant/match-participant.module.ts b/packages/twenty-server/src/modules/match-participant/match-participant.module.tsindex f1fbe3eabbd..6de776a797c 100644--- a/packages/twenty-server/src/modules/match-participant/match-participant.module.ts+++ b/packages/twenty-server/src/modules/match-participant/match-participant.module.ts@@ -1,11 +1,18 @@import { Module } from '@nestjs/common';+import { ReconcilePersonCompanyTargetsJob } from 'src/modules/match-participant/jobs/reconcile-person-company-targets.job';+import { ParticipantTargetPersonListener } from 'src/modules/match-participant/listeners/participant-target-person.listener';import { MatchParticipantService } from 'src/modules/match-participant/match-participant.service';import { ParticipantTargetReconciliationService } from 'src/modules/match-participant/participant-target-reconciliation.service';@Module({imports: [],- providers: [MatchParticipantService, ParticipantTargetReconciliationService],+ providers: [+ MatchParticipantService,+ ParticipantTargetReconciliationService,+ ReconcilePersonCompanyTargetsJob,+ ParticipantTargetPersonListener,+ ],exports: [MatchParticipantService, ParticipantTargetReconciliationService],})export class MatchParticipantModule {}diff --git a/packages/twenty-server/src/modules/match-participant/participant-target-reconciliation.service.ts b/packages/twenty-server/src/modules/match-participant/participant-target-reconciliation.service.tsindex b228b15904f..ba23141499c 100644--- a/packages/twenty-server/src/modules/match-participant/participant-target-reconciliation.service.ts+++ b/packages/twenty-server/src/modules/match-participant/participant-target-reconciliation.service.ts@@ -3,11 +3,12 @@ import { Injectable } from '@nestjs/common';import chunk from 'lodash.chunk';import { QUERY_MAX_RECORDS } from 'twenty-shared/constants';import { isDefined } from 'twenty-shared/utils';-import { In, type ObjectLiteral } from 'typeorm';+import { In, MoreThan, type ObjectLiteral } from 'typeorm';import { type WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace-repository';import { getWorkspaceContext } from 'src/engine/twenty-orm/storage/orm-workspace-context.storage';import { type WorkspaceTransactionScope } from 'src/engine/twenty-orm/types/workspace-transaction-scope.type';+import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';import { getWorkspaceRepositoryWithOptionalTransaction } from 'src/engine/twenty-orm/utils/get-workspace-repository-with-optional-transaction.util';import { WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';import { type CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';@@ -33,6 +34,63 @@ type TargetWorkspaceEntity = Omit<ExistingTarget, 'parentId' | 'deletedAt'> & {export class ParticipantTargetReconciliationService {constructor(private readonly workspaceOrmManager: WorkspaceOrmManager) {}+ public async reconcileTargetsForPeople({+ personIds,+ workspaceId,+ }: {+ personIds: string[];+ workspaceId: string;+ }): Promise<void> {+ await this.workspaceOrmManager.executeInWorkspaceContext(async () => {+ for (const objectMetadataName of [+ 'messageParticipant',+ 'calendarEventParticipant',+ ] as const) {+ for (const personIdChunk of chunk(+ [...new Set(personIds)],+ QUERY_MAX_RECORDS,+ )) {+ let lastParticipantId: string | undefined;+ let hasMoreParticipants = true;++ while (hasMoreParticipants) {+ await this.workspaceOrmManager.runInWorkspaceTransaction(+ async (transactionScope) => {+ const participantRepository = await this.getRepository<+ | MessageParticipantWorkspaceEntity+ | CalendarEventParticipantWorkspaceEntity+ >(objectMetadataName, transactionScope);+ const participants = await participantRepository.find({+ where: {+ personId: In(personIdChunk),+ ...(isDefined(lastParticipantId)+ ? { id: MoreThan(lastParticipantId) }+ : {}),+ },+ order: { id: 'ASC' },+ take: QUERY_MAX_RECORDS,+ });++ await this.reconcileParticipantTargets({+ objectMetadataName,+ sourceRecordIds: participants.map((participant) =>+ 'messageId' in participant+ ? participant.messageId+ : participant.calendarEventId,+ ),+ transactionScope,+ });++ lastParticipantId = participants[participants.length - 1]?.id;+ hasMoreParticipants = participants.length === QUERY_MAX_RECORDS;+ },+ );+ }+ }+ }+ }, buildSystemAuthContext(workspaceId));+ }+public async reconcileParticipantTargets({sourceRecordIds,objectMetadataName,diff --git a/packages/twenty-server/test/integration/graphql/suites/person-company-targets.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/person-company-targets.integration-spec.tsnew file mode 100644index 00000000000..79b03027774--- /dev/null+++ b/packages/twenty-server/test/integration/graphql/suites/person-company-targets.integration-spec.ts@@ -0,0 +1,343 @@+import { randomUUID } from 'crypto';++import { createManyOperationFactory } from 'test/integration/graphql/utils/create-many-operation-factory.util';+import { createOneOperation } from 'test/integration/graphql/utils/create-one-operation.util';+import { deleteOneOperationFactory } from 'test/integration/graphql/utils/delete-one-operation-factory.util';+import { destroyOneOperationFactory } from 'test/integration/graphql/utils/destroy-one-operation-factory.util';+import { findManyOperationFactory } from 'test/integration/graphql/utils/find-many-operation-factory.util';+import { makeGraphqlApiRequest } from 'test/integration/graphql/utils/make-graphql-api-request.util';+import { updateOneOperationFactory } from 'test/integration/graphql/utils/update-one-operation-factory.util';+import { waitForAllJobsToFinish } from 'test/integration/utils/wait-for-all-jobs-to-finish.util';+import { QUERY_MAX_RECORDS } from 'twenty-shared/constants';++type Target = {+ id: string;+ targetCompanyId: string | null;+ targetPersonId: string | null;+ isAutomaticallyAssigned: boolean;+ isManuallyAssigned: boolean;+};++describe('communication targets after initial company assignment', () => {+ let personId: string;+ let companyId: string;+ let otherCompanyId: string;+ let messageThreadId: string;+ let calendarEventId: string;+ const fixtures: { objectName: string; id: string }[] = [];++ const create = async (objectName: string, input: object) => {+ const id = randomUUID();+ const response = await createOneOperation({+ objectMetadataSingularName: objectName,+ input: { id, ...input },+ });++ expect(response.errors).toBeUndefined();+ fixtures.push({ objectName, id });++ return id;+ };++ const updateCompany = async (newCompanyId: string) => {+ const response = await makeGraphqlApiRequest(+ updateOneOperationFactory({+ objectMetadataSingularName: 'person',+ recordId: personId,+ gqlFields: 'id companyId',+ data: { companyId: newCompanyId },+ }),+ );++ expect(response.body.errors).toBeUndefined();+ await waitForAllJobsToFinish();+ };++ const findTargets = async (+ objectName: 'messageThreadTarget' | 'calendarEventTarget',+ ): Promise<Target[]> => {+ const parentField =+ objectName === 'messageThreadTarget'+ ? 'messageThreadId'+ : 'calendarEventId';+ const parentId =+ objectName === 'messageThreadTarget' ? messageThreadId : calendarEventId;+ const response = await makeGraphqlApiRequest(+ findManyOperationFactory({+ objectMetadataSingularName: objectName,+ objectMetadataPluralName: `${objectName}s`,+ filter: { [parentField]: { eq: parentId } },+ gqlFields:+ 'id targetCompanyId targetPersonId isAutomaticallyAssigned isManuallyAssigned',+ }),+ );++ expect(response.body.errors).toBeUndefined();++ const targets: Target[] = response.body.data[`${objectName}s`].edges.map(+ ({ node }: { node: Target }) => node,+ );++ for (const target of targets) {+ if (!fixtures.some(({ id }) => id === target.id)) {+ fixtures.push({ objectName, id: target.id });+ }+ }++ return targets;+ };++ beforeEach(async () => {+ companyId = await create('company', {+ name: 'Initial attribution company',+ });+ otherCompanyId = await create('company', {+ name: 'Manual attribution company',+ });+ // Imported participants can have identities without email handles.+ personId = await create('person', {+ name: { firstName: 'Imported contact' },+ });+ messageThreadId = await create('messageThread', {});+ const messageId = await create('message', {+ messageThreadId,+ subject: 'Initial company attribution',+ receivedAt: '2026-01-01T12:00:00.000Z',+ });++ await create('messageParticipant', {+ messageId,+ personId,+ handle: 'urn:contact:imported',+ role: 'FROM',+ });+ calendarEventId = await create('calendarEvent', {+ title: 'Initial company attribution',+ startsAt: '2026-01-01T12:00:00.000Z',+ endsAt: '2026-01-01T13:00:00.000Z',+ });+ await create('calendarEventParticipant', {+ calendarEventId,+ personId,+ handle: 'urn:contact:imported',+ });+ await create('messageThreadTarget', {+ messageThreadId,+ targetPersonId: personId,+ isAutomaticallyAssigned: true,+ isManuallyAssigned: false,+ });+ await create('calendarEventTarget', {+ calendarEventId,+ targetPersonId: personId,+ isAutomaticallyAssigned: true,+ isManuallyAssigned: false,+ });+ await waitForAllJobsToFinish();+ });++ afterEach(async () => {+ await findTargets('messageThreadTarget');+ await findTargets('calendarEventTarget');++ for (const { objectName, id } of fixtures.splice(0).reverse()) {+ const response = await makeGraphqlApiRequest(+ destroyOneOperationFactory({+ objectMetadataSingularName: objectName,+ recordId: id,+ gqlFields: 'id',+ }),+ );++ expect(response.body.errors).toBeUndefined();+ }+ });++ it('links existing emails and events when the first company is assigned, without rematching participants', async () => {+ await updateCompany(companyId);++ for (const objectName of [+ 'messageThreadTarget',+ 'calendarEventTarget',+ ] as const) {+ const targets = await findTargets(objectName);++ expect(targets).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ targetCompanyId: companyId,+ isAutomaticallyAssigned: true,+ isManuallyAssigned: false,+ }),+ expect.objectContaining({ targetPersonId: personId }),+ ]),+ );+ expect(+ targets.filter((target) => target.targetCompanyId === companyId),+ ).toHaveLength(1);+ }+ });++ it('preserves manual attribution and does not recreate explicitly removed company targets', async () => {+ for (const objectName of [+ 'messageThreadTarget',+ 'calendarEventTarget',+ ] as const) {+ const parent =+ objectName === 'messageThreadTarget'+ ? { messageThreadId }+ : { calendarEventId };++ await create(objectName, {+ ...parent,+ targetCompanyId: otherCompanyId,+ isManuallyAssigned: true,+ isAutomaticallyAssigned: false,+ });+ const removedTargetId = await create(objectName, {+ ...parent,+ targetCompanyId: companyId,+ isManuallyAssigned: true,+ isAutomaticallyAssigned: false,+ });+ const response = await makeGraphqlApiRequest(+ deleteOneOperationFactory({+ objectMetadataSingularName: objectName,+ recordId: removedTargetId,+ gqlFields: 'id',+ }),+ );++ expect(response.body.errors).toBeUndefined();+ }++ await updateCompany(companyId);++ for (const objectName of [+ 'messageThreadTarget',+ 'calendarEventTarget',+ ] as const) {+ const targets = await findTargets(objectName);++ expect(targets).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ targetCompanyId: otherCompanyId,+ isManuallyAssigned: true,+ }),+ expect.objectContaining({ targetPersonId: personId }),+ ]),+ );+ expect(+ targets.some((target) => target.targetCompanyId === companyId),+ ).toBe(false);+ }+ });++ it('does not move the existing company attribution on a later company reassignment', async () => {+ await updateCompany(companyId);+ await updateCompany(otherCompanyId);++ for (const objectName of [+ 'messageThreadTarget',+ 'calendarEventTarget',+ ] as const) {+ const targets = await findTargets(objectName);++ expect(+ targets.some((target) => target.targetCompanyId === companyId),+ ).toBe(true);+ expect(+ targets.some((target) => target.targetCompanyId === otherCompanyId),+ ).toBe(false);+ }+ });++ it('reconciles activity beyond the first page of matched participants', async () => {+ const secondThreadId = await create('messageThread', {});+ const secondMessageId = await create('message', {+ messageThreadId: secondThreadId,+ receivedAt: '2026-01-02T12:00:00.000Z',+ });+ const secondEventId = await create('calendarEvent', {+ title: 'Activity beyond the first page',+ startsAt: '2026-01-02T12:00:00.000Z',+ endsAt: '2026-01-02T13:00:00.000Z',+ });+ const firstMessage = fixtures.find(+ ({ objectName }) => objectName === 'message',+ );++ expect(firstMessage).toBeDefined();++ for (const objectName of [+ 'messageParticipant',+ 'calendarEventParticipant',+ ] as const) {+ const parent =+ objectName === 'messageParticipant'+ ? { messageId: firstMessage!.id, role: 'FROM' }+ : { calendarEventId };++ // Keep each API mutation within the test server's 100-record write limit.+ for (let offset = 0; offset < QUERY_MAX_RECORDS; offset += 100) {+ const data = Array.from(+ { length: Math.min(100, QUERY_MAX_RECORDS - offset) },+ (_, index) => ({+ id: randomUUID(),+ ...parent,+ personId,+ handle: `urn:contact:page:${offset + index}`,+ }),+ );+ const response = await makeGraphqlApiRequest(+ createManyOperationFactory({+ objectMetadataSingularName: objectName,+ objectMetadataPluralName: `${objectName}s`,+ gqlFields: 'id',+ data,+ }),+ );++ expect(response.body.errors).toBeUndefined();+ fixtures.push(...data.map(({ id }) => ({ objectName, id })));+ }++ const lastParticipantId = 'ffffffff-ffff-4fff-8fff-ffffffffffff';+ const response = await createOneOperation({+ objectMetadataSingularName: objectName,+ input: {+ id: lastParticipantId,+ personId,+ handle: 'urn:contact:last-page',+ ...(objectName === 'messageParticipant'+ ? { messageId: secondMessageId, role: 'FROM' }+ : { calendarEventId: secondEventId }),+ },+ });++ expect(response.errors).toBeUndefined();+ fixtures.push({ objectName, id: lastParticipantId });+ }++ await updateCompany(companyId);+ await findTargets('messageThreadTarget');+ await findTargets('calendarEventTarget');+ messageThreadId = secondThreadId;+ calendarEventId = secondEventId;++ for (const objectName of [+ 'messageThreadTarget',+ 'calendarEventTarget',+ ] as const) {+ expect(await findTargets(objectName)).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ targetCompanyId: companyId,+ isAutomaticallyAssigned: true,+ }),+ ]),+ );+ }+ });+});