Skip to content

The open alternative to Salesforce, designed for AI.

Skip participant matching jobs when no person email is involved (#26058)

neo773ac91b55ad361
diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant-person.listener.ts b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant-person.listener.ts
index 07005788658..a23f19cb92c 100644
--- a/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant-person.listener.ts
+++ b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant-person.listener.ts
@@ -1,6 +1,5 @@
import { Injectable } from '@nestjs/common';
-import { isDefined } from 'twenty-shared/utils';
import {
type ObjectRecordCreateEvent,
type ObjectRecordDeleteEvent,
@@ -18,6 +17,7 @@ import {
CalendarEventParticipantMatchParticipantJob,
type CalendarEventParticipantMatchParticipantJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-match-participant.job';
+import { getPersonEmails } from 'src/modules/match-participant/utils/get-person-emails.util';
import { type PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
@Injectable()
@@ -35,20 +35,19 @@ export class CalendarEventParticipantPersonListener {
) {
const personWithEmails = payload.events.filter(
(eventPayload) =>
- isDefined(eventPayload.properties.after.emails?.primaryEmail) ||
- isDefined(eventPayload.properties.after.emails?.additionalEmails),
+ getPersonEmails(eventPayload.properties.after.emails).length > 0,
);
+ if (personWithEmails.length === 0) {
+ return;
+ }
+
const personIds = personWithEmails.map(
(eventPayload) => eventPayload.recordId,
);
- const personEmails = personWithEmails
- .flatMap((eventPayload) => [
- eventPayload.properties.after.emails.primaryEmail,
- ...((eventPayload.properties.after.emails?.additionalEmails ??
- []) as string[]),
- ])
- .filter(isDefined);
+ const personEmails = personWithEmails.flatMap((eventPayload) =>
+ getPersonEmails(eventPayload.properties.after.emails),
+ );
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
CalendarEventParticipantMatchParticipantJob.name,
@@ -69,23 +68,23 @@ export class CalendarEventParticipantPersonListener {
ObjectRecordUpdateEvent<PersonWorkspaceEntity>
>,
) {
- const personWithEmails = payload.events.filter((eventPayload) =>
+ const personWithUpdatedEmails = payload.events.filter((eventPayload) =>
objectRecordUpdateEventChangedProperties(
eventPayload.properties.before,
eventPayload.properties.after,
).includes('emails'),
);
- const personIds = personWithEmails.map(
+ if (personWithUpdatedEmails.length === 0) {
+ return;
+ }
+
+ const personIds = personWithUpdatedEmails.map(
(eventPayload) => eventPayload.recordId,
);
- const personEmails = personWithEmails
- .flatMap((eventPayload) => [
- eventPayload.properties.after.emails.primaryEmail,
- ...((eventPayload.properties.after.emails?.additionalEmails ??
- []) as string[]),
- ])
- .filter(isDefined);
+ const personEmails = personWithUpdatedEmails.flatMap((eventPayload) =>
+ getPersonEmails(eventPayload.properties.after.emails),
+ );
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
CalendarEventParticipantMatchParticipantJob.name,
@@ -106,19 +105,13 @@ export class CalendarEventParticipantPersonListener {
ObjectRecordDeleteEvent<PersonWorkspaceEntity>
>,
) {
- const peopleHavingEmails = payload.events.filter(
- (eventPayload) =>
- isDefined(eventPayload.properties.before.emails?.primaryEmail) ||
- isDefined(eventPayload.properties.before.emails?.additionalEmails),
+ const personEmails = payload.events.flatMap((eventPayload) =>
+ getPersonEmails(eventPayload.properties.before.emails),
);
- const personEmails = peopleHavingEmails
- .flatMap((eventPayload) => [
- eventPayload.properties.before.emails.primaryEmail,
- ...((eventPayload.properties.before.emails?.additionalEmails ??
- []) as string[]),
- ])
- .filter(isDefined);
+ if (personEmails.length === 0) {
+ return;
+ }
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
CalendarEventParticipantMatchParticipantJob.name,
diff --git a/packages/twenty-server/src/modules/match-participant/utils/__tests__/get-person-emails.util.spec.ts b/packages/twenty-server/src/modules/match-participant/utils/__tests__/get-person-emails.util.spec.ts
new file mode 100644
index 00000000000..877eb8bc818
--- /dev/null
+++ b/packages/twenty-server/src/modules/match-participant/utils/__tests__/get-person-emails.util.spec.ts
@@ -0,0 +1,36 @@
+import { getPersonEmails } from 'src/modules/match-participant/utils/get-person-emails.util';
+
+describe('getPersonEmails', () => {
+ it('should return the primary email followed by the additional emails', () => {
+ expect(
+ getPersonEmails({
+ primaryEmail: '[email protected]',
+ additionalEmails: ['[email protected]'],
+ }),
+ });
+
+ it('should return no emails for a person whose emails field is cleared', () => {
+ expect(getPersonEmails({ primaryEmail: '', additionalEmails: [] })).toEqual(
+ [],
+ );
+ });
+
+ it('should return the additional emails when there is no primary email', () => {
+ expect(
+ getPersonEmails({
+ primaryEmail: '',
+ additionalEmails: ['[email protected]'],
+ }),
+ ).toEqual(['[email protected]']);
+ });
+
+ it('should return the primary email when additional emails are null', () => {
+ expect(
+ getPersonEmails({
+ primaryEmail: '[email protected]',
+ additionalEmails: null,
+ }),
+ ).toEqual(['[email protected]']);
+ });
+});
diff --git a/packages/twenty-server/src/modules/match-participant/utils/get-person-emails.util.ts b/packages/twenty-server/src/modules/match-participant/utils/get-person-emails.util.ts
new file mode 100644
index 00000000000..ff668c20684
--- /dev/null
+++ b/packages/twenty-server/src/modules/match-participant/utils/get-person-emails.util.ts
@@ -0,0 +1,7 @@
+import { isNonEmptyString } from '@sniptt/guards';
+import { type EmailsMetadata } from 'twenty-shared/types';
+
+export const getPersonEmails = (emails: EmailsMetadata | undefined): string[] =>
+ [emails?.primaryEmail, ...(emails?.additionalEmails ?? [])].filter(
+ isNonEmptyString,
+ );
diff --git a/packages/twenty-server/src/modules/messaging/message-participant-manager/listeners/message-participant-person.listener.ts b/packages/twenty-server/src/modules/messaging/message-participant-manager/listeners/message-participant-person.listener.ts
index fcf8676a603..9cb5f27734c 100644
--- a/packages/twenty-server/src/modules/messaging/message-participant-manager/listeners/message-participant-person.listener.ts
+++ b/packages/twenty-server/src/modules/messaging/message-participant-manager/listeners/message-participant-person.listener.ts
@@ -1,6 +1,5 @@
import { Injectable } from '@nestjs/common';
-import { isDefined } from 'twenty-shared/utils';
import {
type ObjectRecordCreateEvent,
type ObjectRecordDeleteEvent,
@@ -14,6 +13,7 @@ import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decora
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 { getPersonEmails } from 'src/modules/match-participant/utils/get-person-emails.util';
import {
MessageParticipantMatchParticipantJob,
type MessageParticipantMatchParticipantJobData,
@@ -35,20 +35,19 @@ export class MessageParticipantPersonListener {
) {
const personWithEmails = payload.events.filter(
(eventPayload) =>
- isDefined(eventPayload.properties.after.emails?.primaryEmail) ||
- isDefined(eventPayload.properties.after.emails?.additionalEmails),
+ getPersonEmails(eventPayload.properties.after.emails).length > 0,
);
+ if (personWithEmails.length === 0) {
+ return;
+ }
+
const personIds = personWithEmails.map(
(eventPayload) => eventPayload.recordId,
);
- const personEmails = personWithEmails
- .flatMap((eventPayload) => [
- eventPayload.properties.after.emails.primaryEmail,
- ...((eventPayload.properties.after.emails?.additionalEmails ??
- []) as string[]),
- ])
- .filter(isDefined);
+ const personEmails = personWithEmails.flatMap((eventPayload) =>
+ getPersonEmails(eventPayload.properties.after.emails),
+ );
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
MessageParticipantMatchParticipantJob.name,
@@ -69,23 +68,23 @@ export class MessageParticipantPersonListener {
ObjectRecordUpdateEvent<PersonWorkspaceEntity>
>,
) {
- const personWithEmails = payload.events.filter((eventPayload) =>
+ const personWithUpdatedEmails = payload.events.filter((eventPayload) =>
objectRecordUpdateEventChangedProperties(
eventPayload.properties.before,
eventPayload.properties.after,
).includes('emails'),
);
- const personIds = personWithEmails.map(
+ if (personWithUpdatedEmails.length === 0) {
+ return;
+ }
+
+ const personIds = personWithUpdatedEmails.map(
(eventPayload) => eventPayload.recordId,
);
- const personEmails = personWithEmails
- .flatMap((eventPayload) => [
- eventPayload.properties.after.emails.primaryEmail,
- ...((eventPayload.properties.after.emails?.additionalEmails ??
- []) as string[]),
- ])
- .filter(isDefined);
+ const personEmails = personWithUpdatedEmails.flatMap((eventPayload) =>
+ getPersonEmails(eventPayload.properties.after.emails),
+ );
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
MessageParticipantMatchParticipantJob.name,
@@ -106,25 +105,21 @@ export class MessageParticipantPersonListener {
ObjectRecordDeleteEvent<PersonWorkspaceEntity>
>,
) {
- const personWithEmails = payload.events.filter(
- (eventPayload) =>
- isDefined(eventPayload.properties.before.emails?.primaryEmail) ||
- isDefined(eventPayload.properties.before.emails?.additionalEmails),
+ const personEmails = payload.events.flatMap((eventPayload) =>
+ getPersonEmails(eventPayload.properties.before.emails),
);
+ if (personEmails.length === 0) {
+ return;
+ }
+
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
MessageParticipantMatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
participantMatching: {
personIds: [],
- personEmails: personWithEmails
- .flatMap((eventPayload) => [
- eventPayload.properties.before.emails.primaryEmail,
- ...((eventPayload.properties.before.emails?.additionalEmails ??
- []) as string[]),
- ])
- .filter(isDefined),
+ personEmails,
workspaceMemberIds: [],
},
},