6619 modify event emitter to emit an array of events (#6625)

Closes #6619

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Raphaël Bosi
2024-08-20 19:44:29 +02:00
committed by GitHub
parent 17a1760afd
commit 091c0f83be
41 changed files with 1005 additions and 722 deletions

View File

@ -7,6 +7,7 @@ import { ObjectRecordUpdateEvent } from 'src/engine/integrations/event-emitter/t
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { BlocklistWorkspaceEntity } from 'src/modules/blocklist/standard-objects/blocklist.workspace-entity';
import {
BlocklistItemDeleteCalendarEventsJob,
@ -26,48 +27,74 @@ export class CalendarBlocklistListener {
@OnEvent('blocklist.created')
async handleCreatedEvent(
payload: ObjectRecordCreateEvent<BlocklistWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<BlocklistWorkspaceEntity>
>,
) {
await this.messageQueueService.add<BlocklistItemDeleteCalendarEventsJobData>(
BlocklistItemDeleteCalendarEventsJob.name,
{
workspaceId: payload.workspaceId,
blocklistItemId: payload.recordId,
},
await Promise.all(
payload.events.map((eventPayload) =>
this.messageQueueService.add<BlocklistItemDeleteCalendarEventsJobData>(
BlocklistItemDeleteCalendarEventsJob.name,
{
workspaceId: payload.workspaceId,
blocklistItemId: eventPayload.recordId,
},
),
),
);
}
@OnEvent('blocklist.deleted')
async handleDeletedEvent(
payload: ObjectRecordDeleteEvent<BlocklistWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<BlocklistWorkspaceEntity>
>,
) {
await this.messageQueueService.add<BlocklistReimportCalendarEventsJobData>(
BlocklistReimportCalendarEventsJob.name,
{
workspaceId: payload.workspaceId,
workspaceMemberId: payload.properties.before.workspaceMember.id,
},
await Promise.all(
payload.events.map((eventPayload) =>
this.messageQueueService.add<BlocklistReimportCalendarEventsJobData>(
BlocklistReimportCalendarEventsJob.name,
{
workspaceId: payload.workspaceId,
workspaceMemberId:
eventPayload.properties.before.workspaceMember.id,
},
),
),
);
}
@OnEvent('blocklist.updated')
async handleUpdatedEvent(
payload: ObjectRecordUpdateEvent<BlocklistWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<BlocklistWorkspaceEntity>
>,
) {
await this.messageQueueService.add<BlocklistItemDeleteCalendarEventsJobData>(
BlocklistItemDeleteCalendarEventsJob.name,
{
workspaceId: payload.workspaceId,
blocklistItemId: payload.recordId,
},
);
await Promise.all(
payload.events.reduce((acc: Promise<void>[], eventPayload) => {
acc.push(
this.messageQueueService.add<BlocklistItemDeleteCalendarEventsJobData>(
BlocklistItemDeleteCalendarEventsJob.name,
{
workspaceId: payload.workspaceId,
blocklistItemId: eventPayload.recordId,
},
),
);
await this.messageQueueService.add<BlocklistReimportCalendarEventsJobData>(
BlocklistReimportCalendarEventsJob.name,
{
workspaceId: payload.workspaceId,
workspaceMemberId: payload.properties.after.workspaceMember.id,
},
acc.push(
this.messageQueueService.add<BlocklistReimportCalendarEventsJobData>(
BlocklistReimportCalendarEventsJob.name,
{
workspaceId: payload.workspaceId,
workspaceMemberId:
eventPayload.properties.after.workspaceMember.id,
},
),
);
return acc;
}, []),
);
}
}

View File

@ -1,15 +1,15 @@
import { Module } from '@nestjs/common';
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
import { DeleteConnectedAccountAssociatedCalendarDataJob } from 'src/modules/calendar/calendar-event-cleaner/jobs/delete-connected-account-associated-calendar-data.job';
import { CalendarEventCleanerConnectedAccountListener } from 'src/modules/calendar/calendar-event-cleaner/listeners/calendar-event-cleaner-connected-account.listener';
import { CalendarEventCleanerService } from 'src/modules/calendar/calendar-event-cleaner/services/calendar-event-cleaner.service';
import { CalendarEventWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event.workspace-entity';
@Module({
imports: [TwentyORMModule.forFeature([CalendarEventWorkspaceEntity])],
imports: [],
providers: [
CalendarEventCleanerService,
DeleteConnectedAccountAssociatedCalendarDataJob,
CalendarEventCleanerConnectedAccountListener,
],
exports: [CalendarEventCleanerService],
})

View File

@ -0,0 +1,40 @@
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event';
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import {
DeleteConnectedAccountAssociatedCalendarDataJob,
DeleteConnectedAccountAssociatedCalendarDataJobData,
} from 'src/modules/calendar/calendar-event-cleaner/jobs/delete-connected-account-associated-calendar-data.job';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
@Injectable()
export class CalendarEventCleanerConnectedAccountListener {
constructor(
@InjectMessageQueue(MessageQueue.calendarQueue)
private readonly calendarQueueService: MessageQueueService,
) {}
@OnEvent('connectedAccount.deleted')
async handleDeletedEvent(
payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<ConnectedAccountWorkspaceEntity>
>,
) {
await Promise.all(
payload.events.map((eventPayload) =>
this.calendarQueueService.add<DeleteConnectedAccountAssociatedCalendarDataJobData>(
DeleteConnectedAccountAssociatedCalendarDataJob.name,
{
workspaceId: payload.workspaceId,
connectedAccountId: eventPayload.recordId,
},
),
),
);
}
}

View File

@ -1,12 +1,13 @@
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Any } from 'typeorm';
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { FieldActorSource } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
import { injectIdsInCalendarEvents } from 'src/modules/calendar/calendar-event-import-manager/utils/inject-ids-in-calendar-events.util';
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';
@ -19,7 +20,6 @@ import {
CreateCompanyAndContactJob,
CreateCompanyAndContactJobData,
} from 'src/modules/contact-creation-manager/jobs/create-company-and-contact.job';
import { FieldActorSource } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type';
@Injectable()
export class CalendarSaveEventsService {
@ -28,7 +28,7 @@ export class CalendarSaveEventsService {
private readonly calendarEventParticipantService: CalendarEventParticipantService,
@InjectMessageQueue(MessageQueue.contactCreationQueue)
private readonly messageQueueService: MessageQueueService,
private readonly eventEmitter: EventEmitter2,
private readonly workspaceEventEmitter: WorkspaceEventEmitter,
) {}
public async saveCalendarEventsAndEnqueueContactCreationJob(
@ -140,13 +140,6 @@ export class CalendarSaveEventsService {
);
});
this.eventEmitter.emit(`calendarEventParticipant.matched`, {
workspaceId,
name: 'calendarEventParticipant.matched',
workspaceMemberId: connectedAccount.accountOwnerId,
calendarEventParticipants: savedCalendarEventParticipantsToEmit,
});
if (calendarChannel.isContactAutoCreationEnabled) {
await this.messageQueueService.add<CreateCompanyAndContactJobData>(
CreateCompanyAndContactJob.name,

View File

@ -7,13 +7,14 @@ import { objectRecordChangedProperties as objectRecordUpdateEventChangedProperti
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import {
CalendarEventParticipantMatchParticipantJobData,
CalendarEventParticipantMatchParticipantJob,
CalendarEventParticipantMatchParticipantJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-match-participant.job';
import {
CalendarEventParticipantUnmatchParticipantJobData,
CalendarEventParticipantUnmatchParticipantJob,
CalendarEventParticipantUnmatchParticipantJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-unmatch-participant.job';
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
@ -26,49 +27,59 @@ export class CalendarEventParticipantPersonListener {
@OnEvent('person.created')
async handleCreatedEvent(
payload: ObjectRecordCreateEvent<PersonWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<PersonWorkspaceEntity>
>,
) {
if (payload.properties.after.email === null) {
return;
}
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
CalendarEventParticipantMatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: payload.properties.after.email,
personId: payload.recordId,
},
);
}
@OnEvent('person.updated')
async handleUpdatedEvent(
payload: ObjectRecordUpdateEvent<PersonWorkspaceEntity>,
) {
if (
objectRecordUpdateEventChangedProperties(
payload.properties.before,
payload.properties.after,
).includes('email')
) {
await this.messageQueueService.add<CalendarEventParticipantUnmatchParticipantJobData>(
CalendarEventParticipantUnmatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: payload.properties.before.email,
personId: payload.recordId,
},
);
for (const eventPayload of payload.events) {
if (eventPayload.properties.after.email === null) {
continue;
}
// TODO: modify this job to take an array of participants to match
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
CalendarEventParticipantMatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: payload.properties.after.email,
personId: payload.recordId,
email: eventPayload.properties.after.email,
personId: eventPayload.recordId,
},
);
}
}
@OnEvent('person.updated')
async handleUpdatedEvent(
payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<PersonWorkspaceEntity>
>,
) {
for (const eventPayload of payload.events) {
if (
objectRecordUpdateEventChangedProperties(
eventPayload.properties.before,
eventPayload.properties.after,
).includes('email')
) {
// TODO: modify this job to take an array of participants to match
await this.messageQueueService.add<CalendarEventParticipantUnmatchParticipantJobData>(
CalendarEventParticipantUnmatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: eventPayload.properties.before.email,
personId: eventPayload.recordId,
},
);
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
CalendarEventParticipantMatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: eventPayload.properties.after.email,
personId: eventPayload.recordId,
},
);
}
}
}
}

View File

@ -7,13 +7,14 @@ import { objectRecordChangedProperties as objectRecordUpdateEventChangedProperti
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import {
CalendarEventParticipantMatchParticipantJob,
CalendarEventParticipantMatchParticipantJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-match-participant.job';
import {
CalendarEventParticipantUnmatchParticipantJobData,
CalendarEventParticipantUnmatchParticipantJob,
CalendarEventParticipantUnmatchParticipantJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-unmatch-participant.job';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@ -26,49 +27,57 @@ export class CalendarEventParticipantWorkspaceMemberListener {
@OnEvent('workspaceMember.created')
async handleCreatedEvent(
payload: ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>
>,
) {
if (payload.properties.after.userEmail === null) {
return;
}
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
CalendarEventParticipantMatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: payload.properties.after.userEmail,
workspaceMemberId: payload.properties.after.id,
},
);
}
@OnEvent('workspaceMember.updated')
async handleUpdatedEvent(
payload: ObjectRecordUpdateEvent<WorkspaceMemberWorkspaceEntity>,
) {
if (
objectRecordUpdateEventChangedProperties<WorkspaceMemberWorkspaceEntity>(
payload.properties.before,
payload.properties.after,
).includes('userEmail')
) {
await this.messageQueueService.add<CalendarEventParticipantUnmatchParticipantJobData>(
CalendarEventParticipantUnmatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: payload.properties.before.userEmail,
personId: payload.recordId,
},
);
for (const eventPayload of payload.events) {
if (eventPayload.properties.after.userEmail === null) {
continue;
}
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
CalendarEventParticipantMatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: payload.properties.after.userEmail,
workspaceMemberId: payload.recordId,
email: eventPayload.properties.after.userEmail,
workspaceMemberId: eventPayload.recordId,
},
);
}
}
@OnEvent('workspaceMember.updated')
async handleUpdatedEvent(
payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<WorkspaceMemberWorkspaceEntity>
>,
) {
for (const eventPayload of payload.events) {
if (
objectRecordUpdateEventChangedProperties<WorkspaceMemberWorkspaceEntity>(
eventPayload.properties.before,
eventPayload.properties.after,
).includes('userEmail')
) {
await this.messageQueueService.add<CalendarEventParticipantUnmatchParticipantJobData>(
CalendarEventParticipantUnmatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: eventPayload.properties.before.userEmail,
personId: eventPayload.recordId,
},
);
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
CalendarEventParticipantMatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: eventPayload.properties.after.userEmail,
workspaceMemberId: eventPayload.recordId,
},
);
}
}
}
}

View File

@ -7,6 +7,7 @@ import { Repository } from 'typeorm';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
import { TimelineActivityRepository } from 'src/modules/timeline/repositiories/timeline-activity.repository';
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
@ -22,49 +23,55 @@ export class CalendarEventParticipantListener {
) {}
@OnEvent('calendarEventParticipant.matched')
public async handleCalendarEventParticipantMatchedEvent(payload: {
workspaceId: string;
workspaceMemberId: string;
participants: CalendarEventParticipantWorkspaceEntity[];
}): Promise<void> {
const calendarEventParticipants = payload.participants ?? [];
public async handleCalendarEventParticipantMatchedEvent(
payload: WorkspaceEventBatch<{
workspaceMemberId: string;
participants: CalendarEventParticipantWorkspaceEntity[];
}>,
): Promise<void> {
const workspaceId = payload.workspaceId;
// TODO: move to a job?
// TODO: Refactor to insertTimelineActivitiesForObject once
for (const eventPayload of payload.events) {
const calendarEventParticipants = eventPayload.participants;
const workspaceMemberId = eventPayload.workspaceMemberId;
const dataSourceSchema = this.workspaceDataSourceService.getSchemaName(
payload.workspaceId,
);
// TODO: move to a job?
const calendarEventObjectMetadata =
await this.objectMetadataRepository.findOneOrFail({
where: {
nameSingular: 'calendarEvent',
workspaceId: payload.workspaceId,
},
});
const dataSourceSchema =
this.workspaceDataSourceService.getSchemaName(workspaceId);
const calendarEventParticipantsWithPersonId =
calendarEventParticipants.filter((participant) => participant.personId);
const calendarEventObjectMetadata =
await this.objectMetadataRepository.findOneOrFail({
where: {
nameSingular: 'calendarEvent',
workspaceId,
},
});
if (calendarEventParticipantsWithPersonId.length === 0) {
return;
const calendarEventParticipantsWithPersonId =
calendarEventParticipants.filter((participant) => participant.personId);
if (calendarEventParticipantsWithPersonId.length === 0) {
continue;
}
await this.timelineActivityRepository.insertTimelineActivitiesForObject(
'person',
calendarEventParticipantsWithPersonId.map((participant) => ({
dataSourceSchema,
name: 'calendarEvent.linked',
properties: null,
objectName: 'calendarEvent',
recordId: participant.personId,
workspaceMemberId,
workspaceId,
linkedObjectMetadataId: calendarEventObjectMetadata.id,
linkedRecordId: participant.calendarEventId,
linkedRecordCachedName: '',
})),
workspaceId,
);
}
await this.timelineActivityRepository.insertTimelineActivitiesForObject(
'person',
calendarEventParticipantsWithPersonId.map((participant) => ({
dataSourceSchema,
name: 'calendarEvent.linked',
properties: null,
objectName: 'calendarEvent',
recordId: participant.personId,
workspaceMemberId: payload.workspaceMemberId,
workspaceId: payload.workspaceId,
linkedObjectMetadataId: calendarEventObjectMetadata.id,
linkedRecordId: participant.calendarEventId,
linkedRecordCachedName: '',
})),
payload.workspaceId,
);
}
}

View File

@ -111,7 +111,7 @@ export class CalendarEventParticipantService {
await this.matchParticipantService.matchParticipants(
savedParticipants,
'messageParticipant',
'calendarEventParticipant',
transactionManager,
);
}