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,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,
);
}