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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
41 changed files with 1005 additions and 722 deletions

View File

@ -8,6 +8,7 @@ import { objectRecordChangedValues } from 'src/engine/integrations/event-emitter
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 { CreateAuditLogFromInternalEvent } from 'src/modules/timeline/jobs/create-audit-log-from-internal-event';
import { UpsertTimelineActivityFromInternalEvent } from 'src/modules/timeline/jobs/upsert-timeline-activity-from-internal-event.job';
@ -19,40 +20,46 @@ export class EntityEventsToDbListener {
) {}
@OnEvent('*.created')
async handleCreate(payload: ObjectRecordCreateEvent<any>) {
async handleCreate(
payload: WorkspaceEventBatch<ObjectRecordCreateEvent<any>>,
) {
return this.handle(payload);
}
@OnEvent('*.updated')
async handleUpdate(payload: ObjectRecordUpdateEvent<any>) {
payload.properties.diff = objectRecordChangedValues(
payload.properties.before,
payload.properties.after,
payload.properties.updatedFields,
payload.objectMetadata,
);
async handleUpdate(
payload: WorkspaceEventBatch<ObjectRecordUpdateEvent<any>>,
) {
for (const eventPayload of payload.events) {
eventPayload.properties.diff = objectRecordChangedValues(
eventPayload.properties.before,
eventPayload.properties.after,
eventPayload.properties.updatedFields,
eventPayload.objectMetadata,
);
}
return this.handle(payload);
}
@OnEvent('*.deleted')
async handleDelete(payload: ObjectRecordUpdateEvent<any>) {
async handleDelete(
payload: WorkspaceEventBatch<ObjectRecordUpdateEvent<any>>,
) {
return this.handle(payload);
}
private async handle(payload: ObjectRecordBaseEvent) {
if (!payload.objectMetadata?.isAuditLogged) {
return;
}
this.messageQueueService.add<ObjectRecordBaseEvent>(
CreateAuditLogFromInternalEvent.name,
payload,
private async handle(payload: WorkspaceEventBatch<ObjectRecordBaseEvent>) {
payload.events = payload.events.filter(
(event) => event.objectMetadata?.isAuditLogged,
);
this.messageQueueService.add<ObjectRecordBaseEvent>(
UpsertTimelineActivityFromInternalEvent.name,
payload,
);
await this.messageQueueService.add<
WorkspaceEventBatch<ObjectRecordBaseEvent>
>(CreateAuditLogFromInternalEvent.name, payload);
await this.messageQueueService.add<
WorkspaceEventBatch<ObjectRecordBaseEvent>
>(UpsertTimelineActivityFromInternalEvent.name, payload);
}
}

View File

@ -4,6 +4,7 @@ import { OnEvent } from '@nestjs/event-emitter';
import { AnalyticsService } from 'src/engine/core-modules/analytics/analytics.service';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
@Injectable()
export class TelemetryListener {
@ -13,36 +14,48 @@ export class TelemetryListener {
) {}
@OnEvent('*.created')
async handleAllCreate(payload: ObjectRecordCreateEvent<any>) {
await this.analyticsService.create(
{
type: 'track',
data: {
eventName: payload.name,
},
},
payload.userId,
payload.workspaceId,
'', // voluntarely not retrieving this
'', // to avoid slowing down
this.environmentService.get('SERVER_URL'),
async handleAllCreate(
payload: WorkspaceEventBatch<ObjectRecordCreateEvent<any>>,
) {
await Promise.all(
payload.events.map((eventPayload) =>
this.analyticsService.create(
{
type: 'track',
data: {
eventName: payload.name,
},
},
eventPayload.userId,
payload.workspaceId,
'', // voluntarily not retrieving this
'', // to avoid slowing down
this.environmentService.get('SERVER_URL'),
),
),
);
}
@OnEvent('user.signup')
async handleUserSignup(payload: ObjectRecordCreateEvent<any>) {
await this.analyticsService.create(
{
type: 'track',
data: {
eventName: 'user.signup',
},
},
payload.userId,
payload.workspaceId,
'',
'',
this.environmentService.get('SERVER_URL'),
async handleUserSignup(
payload: WorkspaceEventBatch<ObjectRecordCreateEvent<any>>,
) {
await Promise.all(
payload.events.map((eventPayload) =>
this.analyticsService.create(
{
type: 'track',
data: {
eventName: 'user.signup',
},
},
eventPayload.userId,
payload.workspaceId,
'',
'',
this.environmentService.get('SERVER_URL'),
),
),
);
}
}

View File

@ -1,5 +1,4 @@
import { Injectable, Logger } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import isEmpty from 'lodash.isempty';
import { DataSource } from 'typeorm';
@ -55,6 +54,8 @@ import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
import { isQueryTimeoutError } from 'src/engine/utils/query-timeout.util';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
import { isDefined } from 'src/utils/is-defined';
import {
PGGraphQLMutation,
@ -78,7 +79,7 @@ export class WorkspaceQueryRunnerService {
private readonly queryResultGettersFactory: QueryResultGettersFactory,
@InjectMessageQueue(MessageQueue.webhookQueue)
private readonly messageQueueService: MessageQueueService,
private readonly eventEmitter: EventEmitter2,
private readonly workspaceEventEmitter: WorkspaceEventEmitter,
private readonly workspaceQueryHookService: WorkspaceQueryHookService,
private readonly environmentService: EnvironmentService,
private readonly duplicateService: DuplicateService,
@ -304,18 +305,21 @@ export class WorkspaceQueryRunnerService {
options,
);
parsedResults.forEach((record) => {
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.created`, {
name: `${objectMetadataItem.nameSingular}.created`,
workspaceId: authContext.workspace.id,
userId: authContext.user?.id,
recordId: record.id,
objectMetadata: objectMetadataItem,
properties: {
after: record,
},
} satisfies ObjectRecordCreateEvent<any>);
});
this.workspaceEventEmitter.emit(
`${objectMetadataItem.nameSingular}.created`,
parsedResults.map(
(record) =>
({
userId: authContext.user?.id,
recordId: record.id,
objectMetadata: objectMetadataItem,
properties: {
after: record,
},
}) satisfies ObjectRecordCreateEvent<any>,
),
authContext.workspace.id,
);
return parsedResults;
}
@ -440,18 +444,22 @@ export class WorkspaceQueryRunnerService {
options,
);
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.updated`, {
name: `${objectMetadataItem.nameSingular}.updated`,
workspaceId: authContext.workspace.id,
userId: authContext.user?.id,
recordId: existingRecord.id,
objectMetadata: objectMetadataItem,
properties: {
updatedFields: Object.keys(args.data),
before: this.removeNestedProperties(existingRecord as Record),
after: this.removeNestedProperties(parsedResults?.[0]),
},
} satisfies ObjectRecordUpdateEvent<any>);
this.workspaceEventEmitter.emit(
`${objectMetadataItem.nameSingular}.updated`,
[
{
userId: authContext.user?.id,
recordId: existingRecord.id,
objectMetadata: objectMetadataItem,
properties: {
updatedFields: Object.keys(args.data),
before: this.removeNestedProperties(existingRecord as Record),
after: this.removeNestedProperties(parsedResults?.[0]),
},
} satisfies ObjectRecordUpdateEvent<any>,
],
authContext.workspace.id,
);
return parsedResults?.[0];
}
@ -513,30 +521,36 @@ export class WorkspaceQueryRunnerService {
options,
);
parsedResults.forEach((record) => {
const existingRecord = mappedRecords.get(record.id);
const eventsToEmit: ObjectRecordUpdateEvent<any>[] = parsedResults
.map((record) => {
const existingRecord = mappedRecords.get(record.id);
if (!existingRecord) {
this.logger.warn(
`Record with id ${record.id} not found in the database`,
);
if (!existingRecord) {
this.logger.warn(
`Record with id ${record.id} not found in the database`,
);
return;
}
return;
}
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.updated`, {
name: `${objectMetadataItem.nameSingular}.updated`,
workspaceId: authContext.workspace.id,
userId: authContext.user?.id,
recordId: existingRecord.id,
objectMetadata: objectMetadataItem,
properties: {
updatedFields: Object.keys(args.data),
before: this.removeNestedProperties(existingRecord as Record),
after: this.removeNestedProperties(record),
},
} satisfies ObjectRecordUpdateEvent<any>);
});
return {
userId: authContext.user?.id,
recordId: existingRecord.id,
objectMetadata: objectMetadataItem,
properties: {
updatedFields: Object.keys(args.data),
before: this.removeNestedProperties(existingRecord as Record),
after: this.removeNestedProperties(record),
},
};
})
.filter(isDefined);
this.workspaceEventEmitter.emit(
`${objectMetadataItem.nameSingular}.updated`,
eventsToEmit,
authContext.workspace.id,
);
return parsedResults;
}
@ -602,18 +616,21 @@ export class WorkspaceQueryRunnerService {
options,
);
parsedResults.forEach((record) => {
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.deleted`, {
name: `${objectMetadataItem.nameSingular}.deleted`,
workspaceId: authContext.workspace.id,
userId: authContext.user?.id,
recordId: record.id,
objectMetadata: objectMetadataItem,
properties: {
before: this.removeNestedProperties(record),
},
} satisfies ObjectRecordDeleteEvent<any>);
});
this.workspaceEventEmitter.emit(
`${objectMetadataItem.nameSingular}.deleted`,
parsedResults.map(
(record) =>
({
userId: authContext.user?.id,
recordId: record.id,
objectMetadata: objectMetadataItem,
properties: {
before: this.removeNestedProperties(record),
},
}) satisfies ObjectRecordDeleteEvent<any>,
),
authContext.workspace.id,
);
return parsedResults;
}
@ -744,18 +761,21 @@ export class WorkspaceQueryRunnerService {
options,
);
parsedResults.forEach((record) => {
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.created`, {
name: `${objectMetadataItem.nameSingular}.created`,
workspaceId: authContext.workspace.id,
userId: authContext.user?.id,
recordId: record.id,
objectMetadata: objectMetadataItem,
properties: {
after: this.removeNestedProperties(record),
},
} satisfies ObjectRecordCreateEvent<any>);
});
this.workspaceEventEmitter.emit(
`${objectMetadataItem.nameSingular}.created`,
parsedResults.map(
(record) =>
({
userId: authContext.user?.id,
recordId: record.id,
objectMetadata: objectMetadataItem,
properties: {
after: this.removeNestedProperties(record),
},
}) satisfies ObjectRecordCreateEvent<any>,
),
authContext.workspace.id,
);
return parsedResults;
}
@ -821,19 +841,23 @@ export class WorkspaceQueryRunnerService {
options,
);
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.deleted`, {
name: `${objectMetadataItem.nameSingular}.deleted`,
workspaceId: authContext.workspace.id,
userId: authContext.user?.id,
recordId: args.id,
objectMetadata: objectMetadataItem,
properties: {
before: {
...(existingRecord ?? {}),
...this.removeNestedProperties(parsedResults?.[0]),
},
},
} satisfies ObjectRecordDeleteEvent<any>);
this.workspaceEventEmitter.emit(
`${objectMetadataItem.nameSingular}.deleted`,
[
{
userId: authContext.user?.id,
recordId: args.id,
objectMetadata: objectMetadataItem,
properties: {
before: {
...(existingRecord ?? {}),
...this.removeNestedProperties(parsedResults?.[0]),
},
},
} satisfies ObjectRecordDeleteEvent<any>,
],
authContext.workspace.id,
);
return parsedResults?.[0];
}

View File

@ -10,6 +10,7 @@ import { ObjectRecordCreateEvent } 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 { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@Injectable()
@ -23,7 +24,9 @@ export class BillingWorkspaceMemberListener {
@OnEvent('workspaceMember.created')
@OnEvent('workspaceMember.deleted')
async handleCreateOrDeleteEvent(
payload: ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>
>,
) {
if (!this.environmentService.get('IS_BILLING_ENABLED')) {
return;

View File

@ -13,6 +13,7 @@ import { PostgresCredentialsModule } from 'src/engine/core-modules/postgres-cred
import { UserModule } from 'src/engine/core-modules/user/user.module';
import { WorkflowTriggerCoreModule } from 'src/engine/core-modules/workflow/core-workflow-trigger.module';
import { WorkspaceModule } from 'src/engine/core-modules/workspace/workspace.module';
import { WorkspaceEventEmitterModule } from 'src/engine/workspace-event-emitter/workspace-event-emitter.module';
import { AnalyticsModule } from './analytics/analytics.module';
import { ClientConfigModule } from './client-config/client-config.module';
@ -36,6 +37,7 @@ import { FileModule } from './file/file.module';
AISQLQueryModule,
PostgresCredentialsModule,
WorkflowTriggerCoreModule,
WorkspaceEventEmitterModule,
],
exports: [
AnalyticsModule,

View File

@ -1,5 +1,4 @@
/* eslint-disable @nx/workspace-inject-workspace-repository */
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectRepository } from '@nestjs/typeorm';
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
@ -11,6 +10,7 @@ import { User } from 'src/engine/core-modules/user/user.entity';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
import { assert } from 'src/utils/assert';
@ -22,7 +22,7 @@ export class UserWorkspaceService extends TypeOrmQueryService<UserWorkspace> {
private readonly userRepository: Repository<User>,
private readonly dataSourceService: DataSourceService,
private readonly typeORMService: TypeORMService,
private eventEmitter: EventEmitter2,
private workspaceEventEmitter: WorkspaceEventEmitter,
) {
super(userWorkspaceRepository);
}
@ -35,11 +35,9 @@ export class UserWorkspaceService extends TypeOrmQueryService<UserWorkspace> {
const payload = new ObjectRecordCreateEvent<UserWorkspace>();
payload.workspaceId = workspaceId;
payload.userId = userId;
payload.name = 'user.signup';
this.eventEmitter.emit('user.signup', payload);
this.workspaceEventEmitter.emit('user.signup', [payload], workspaceId);
return this.userWorkspaceRepository.save(userWorkspace);
}
@ -76,14 +74,16 @@ export class UserWorkspaceService extends TypeOrmQueryService<UserWorkspace> {
const payload =
new ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>();
payload.workspaceId = workspaceId;
payload.properties = {
after: workspaceMember[0],
};
payload.recordId = workspaceMember[0].id;
payload.name = 'workspaceMember.created';
this.eventEmitter.emit('workspaceMember.created', payload);
this.workspaceEventEmitter.emit(
'workspaceMember.created',
[payload],
workspaceId,
);
}
async addUserToWorkspace(user: User, workspace: Workspace) {

View File

@ -1,14 +1,14 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { UserService } from 'src/engine/core-modules/user/services/user.service';
import { User } from 'src/engine/core-modules/user/user.entity';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
import { UserWorkspace } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
import { UserService } from 'src/engine/core-modules/user/services/user.service';
import { User } from 'src/engine/core-modules/user/user.entity';
import { WorkspaceService } from 'src/engine/core-modules/workspace/services/workspace.service';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
describe('UserService', () => {
let service: UserService;
@ -34,7 +34,7 @@ describe('UserService', () => {
useValue: {},
},
{
provide: EventEmitter2,
provide: WorkspaceEventEmitter,
useValue: {},
},
{

View File

@ -1,4 +1,3 @@
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectRepository } from '@nestjs/typeorm';
import assert from 'assert';
@ -16,6 +15,7 @@ import {
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
// eslint-disable-next-line @nx/workspace-inject-workspace-repository
@ -25,7 +25,7 @@ export class UserService extends TypeOrmQueryService<User> {
private readonly userRepository: Repository<User>,
private readonly dataSourceService: DataSourceService,
private readonly typeORMService: TypeORMService,
private readonly eventEmitter: EventEmitter2,
private readonly workspaceEventEmitter: WorkspaceEventEmitter,
private readonly workspaceService: WorkspaceService,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {
@ -110,15 +110,16 @@ export class UserService extends TypeOrmQueryService<User> {
const payload =
new ObjectRecordDeleteEvent<WorkspaceMemberWorkspaceEntity>();
payload.workspaceId = workspaceId;
payload.properties = {
before: workspaceMember,
};
payload.name = 'workspaceMember.deleted';
payload.recordId = workspaceMember.id;
payload.name = 'workspaceMember.deleted';
this.eventEmitter.emit('workspaceMember.deleted', payload);
this.workspaceEventEmitter.emit(
'workspaceMember.deleted',
[payload],
workspaceId,
);
return user;
}

View File

@ -11,6 +11,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 { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@Injectable()
@ -23,39 +24,51 @@ export class WorkspaceWorkspaceMemberListener {
@OnEvent('workspaceMember.updated')
async handleUpdateEvent(
payload: ObjectRecordUpdateEvent<WorkspaceMemberWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<WorkspaceMemberWorkspaceEntity>
>,
) {
const { firstName: firstNameAfter, lastName: lastNameAfter } =
payload.properties.after.name;
await Promise.all(
payload.events.map((eventPayload) => {
const { firstName: firstNameAfter, lastName: lastNameAfter } =
eventPayload.properties.after.name;
if (firstNameAfter === '' && lastNameAfter === '') {
return;
}
if (firstNameAfter === '' && lastNameAfter === '') {
return;
}
if (!payload.userId) {
return;
}
if (!eventPayload.userId) {
return;
}
await this.onboardingService.setOnboardingCreateProfilePending({
userId: payload.userId,
workspaceId: payload.workspaceId,
value: false,
});
return this.onboardingService.setOnboardingCreateProfilePending({
userId: eventPayload.userId,
workspaceId: payload.workspaceId,
value: false,
});
}),
);
}
@OnEvent('workspaceMember.deleted')
async handleDeleteEvent(
payload: ObjectRecordDeleteEvent<WorkspaceMemberWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<WorkspaceMemberWorkspaceEntity>
>,
) {
const userId = payload.properties.before.userId;
await Promise.all(
payload.events.map((eventPayload) => {
const userId = eventPayload.properties.before.userId;
if (!userId) {
return;
}
if (!userId) {
return;
}
await this.messageQueueService.add<HandleWorkspaceMemberDeletedJobData>(
HandleWorkspaceMemberDeletedJob.name,
{ workspaceId: payload.workspaceId, userId },
return this.messageQueueService.add<HandleWorkspaceMemberDeletedJobData>(
HandleWorkspaceMemberDeletedJob.name,
{ workspaceId: payload.workspaceId, userId },
);
}),
);
}
}

View File

@ -1,11 +0,0 @@
import { ObjectRecordBaseEvent } from 'src/engine/integrations/event-emitter/types/object-record.base.event';
export class ObjectRecordJobData extends ObjectRecordBaseEvent {
getOperation() {
return this.name.split('.')[1];
}
getObjectName() {
return this.name.split('.')[0];
}
}

View File

@ -1,11 +1,14 @@
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
export class ObjectRecordBaseEvent {
name: string;
workspaceId: string;
recordId: string;
userId?: string;
workspaceMemberId?: string;
objectMetadata: ObjectMetadataInterface;
properties: any;
}
export class ObjectRecordBaseEventWithNameAndWorkspaceId extends ObjectRecordBaseEvent {
name: string;
workspaceId: string;
}

View File

@ -0,0 +1,11 @@
import { Global, Module } from '@nestjs/common';
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
@Global()
@Module({
imports: [],
providers: [WorkspaceEventEmitter],
exports: [WorkspaceEventEmitter],
})
export class WorkspaceEventEmitterModule {}

View File

@ -0,0 +1,21 @@
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
@Injectable()
export class WorkspaceEventEmitter {
constructor(private readonly eventEmitter: EventEmitter2) {}
public emit(eventName: string, events: any[], workspaceId: string) {
if (!events.length) {
return;
}
return this.eventEmitter.emit(eventName, {
name: eventName,
workspaceId,
events,
} satisfies WorkspaceEventBatch<any>);
}
}

View File

@ -0,0 +1,5 @@
export type WorkspaceEventBatch<WorkspaceEvent> = {
name: string;
workspaceId: string;
events: WorkspaceEvent[];
};

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

View File

@ -3,6 +3,7 @@ import { OnEvent } from '@nestjs/event-emitter';
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { AccountsToReconnectService } from 'src/modules/connected-account/services/accounts-to-reconnect.service';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@ -16,27 +17,31 @@ export class ConnectedAccountListener {
@OnEvent('connectedAccount.deleted')
async handleDeletedEvent(
payload: ObjectRecordDeleteEvent<ConnectedAccountWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<ConnectedAccountWorkspaceEntity>
>,
) {
const workspaceMemberId = payload.properties.before.accountOwnerId;
const workspaceId = payload.workspaceId;
const workspaceMemberRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkspaceMemberWorkspaceEntity>(
for (const eventPayload of payload.events) {
const workspaceMemberId = eventPayload.properties.before.accountOwnerId;
const workspaceId = payload.workspaceId;
const workspaceMemberRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkspaceMemberWorkspaceEntity>(
workspaceId,
'workspaceMember',
);
const workspaceMember = await workspaceMemberRepository.findOneOrFail({
where: { id: workspaceMemberId },
});
const userId = workspaceMember.userId;
const connectedAccountId = eventPayload.properties.before.id;
await this.accountsToReconnectService.removeAccountToReconnect(
userId,
workspaceId,
'workspaceMember',
connectedAccountId,
);
const workspaceMember = await workspaceMemberRepository.findOneOrFail({
where: { id: workspaceMemberId },
});
const userId = workspaceMember.userId;
const connectedAccountId = payload.properties.before.id;
await this.accountsToReconnectService.removeAccountToReconnect(
userId,
workspaceId,
connectedAccountId,
);
}
}
}

View File

@ -1,5 +1,3 @@
import { EventEmitter2 } from '@nestjs/event-emitter';
import { WorkspaceQueryHookInstance } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/interfaces/workspace-query-hook.interface';
import { DeleteOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';
@ -7,6 +5,7 @@ import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runne
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
@WorkspaceQueryHook(`connectedAccount.deleteOne`)
@ -15,7 +14,7 @@ export class ConnectedAccountDeleteOnePreQueryHook
{
constructor(
private readonly twentyORMManager: TwentyORMManager,
private eventEmitter: EventEmitter2,
private readonly workspaceEventEmitter: WorkspaceEventEmitter,
) {}
async execute(
@ -34,16 +33,19 @@ export class ConnectedAccountDeleteOnePreQueryHook
connectedAccountId,
});
messageChannels.forEach((messageChannel) => {
this.eventEmitter.emit('messageChannel.deleted', {
workspaceId: authContext.workspace.id,
name: 'messageChannel.deleted',
recordId: messageChannel.id,
} satisfies Pick<
ObjectRecordDeleteEvent<MessageChannelWorkspaceEntity>,
'workspaceId' | 'recordId' | 'name'
>);
});
this.workspaceEventEmitter.emit(
'messageChannel.deleted',
messageChannels.map(
(messageChannel) =>
({
recordId: messageChannel.id,
}) satisfies Pick<
ObjectRecordDeleteEvent<MessageChannelWorkspaceEntity>,
'recordId'
>,
),
authContext.workspace.id,
);
return payload;
}

View File

@ -1,11 +1,9 @@
import { Module } from '@nestjs/common';
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
import { ConnectedAccountDeleteOnePreQueryHook } from 'src/modules/connected-account/query-hooks/connected-account-delete-one.pre-query.hook';
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
@Module({
imports: [TwentyORMModule.forFeature([MessageChannelWorkspaceEntity])],
imports: [],
providers: [ConnectedAccountDeleteOnePreQueryHook],
})
export class ConnectedAccountQueryHookModule {}

View File

@ -6,9 +6,10 @@ import { objectRecordChangedProperties } from 'src/engine/integrations/event-emi
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 {
CalendarCreateCompanyAndContactAfterSyncJobData,
CalendarCreateCompanyAndContactAfterSyncJob,
CalendarCreateCompanyAndContactAfterSyncJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-create-company-and-contact-after-sync.job';
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
@ -21,22 +22,28 @@ export class AutoCompaniesAndContactsCreationCalendarChannelListener {
@OnEvent('calendarChannel.updated')
async handleUpdatedEvent(
payload: ObjectRecordUpdateEvent<MessageChannelWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<MessageChannelWorkspaceEntity>
>,
) {
if (
objectRecordChangedProperties(
payload.properties.before,
payload.properties.after,
).includes('isContactAutoCreationEnabled') &&
payload.properties.after.isContactAutoCreationEnabled
) {
await this.messageQueueService.add<CalendarCreateCompanyAndContactAfterSyncJobData>(
CalendarCreateCompanyAndContactAfterSyncJob.name,
{
workspaceId: payload.workspaceId,
calendarChannelId: payload.recordId,
},
);
}
await Promise.all(
payload.events.map((eventPayload) => {
if (
objectRecordChangedProperties(
eventPayload.properties.before,
eventPayload.properties.after,
).includes('isContactAutoCreationEnabled') &&
eventPayload.properties.after.isContactAutoCreationEnabled
) {
return this.messageQueueService.add<CalendarCreateCompanyAndContactAfterSyncJobData>(
CalendarCreateCompanyAndContactAfterSyncJob.name,
{
workspaceId: payload.workspaceId,
calendarChannelId: eventPayload.recordId,
},
);
}
}),
);
}
}

View File

@ -6,10 +6,11 @@ import { objectRecordChangedProperties } from 'src/engine/integrations/event-emi
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 { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
import {
MessagingCreateCompanyAndContactAfterSyncJobData,
MessagingCreateCompanyAndContactAfterSyncJob,
MessagingCreateCompanyAndContactAfterSyncJobData,
} from 'src/modules/messaging/message-participant-manager/jobs/messaging-create-company-and-contact-after-sync.job';
@Injectable()
@ -21,22 +22,28 @@ export class AutoCompaniesAndContactsCreationMessageChannelListener {
@OnEvent('messageChannel.updated')
async handleUpdatedEvent(
payload: ObjectRecordUpdateEvent<MessageChannelWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<MessageChannelWorkspaceEntity>
>,
) {
if (
objectRecordChangedProperties(
payload.properties.before,
payload.properties.after,
).includes('isContactAutoCreationEnabled') &&
payload.properties.after.isContactAutoCreationEnabled
) {
await this.messageQueueService.add<MessagingCreateCompanyAndContactAfterSyncJobData>(
MessagingCreateCompanyAndContactAfterSyncJob.name,
{
workspaceId: payload.workspaceId,
messageChannelId: payload.recordId,
},
);
}
await Promise.all(
payload.events.map((eventPayload) => {
if (
objectRecordChangedProperties(
eventPayload.properties.before,
eventPayload.properties.after,
).includes('isContactAutoCreationEnabled') &&
eventPayload.properties.after.isContactAutoCreationEnabled
) {
return this.messageQueueService.add<MessagingCreateCompanyAndContactAfterSyncJobData>(
MessagingCreateCompanyAndContactAfterSyncJob.name,
{
workspaceId: payload.workspaceId,
messageChannelId: eventPayload.recordId,
},
);
}
}),
);
}
}

View File

@ -1,5 +1,4 @@
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectRepository } from '@nestjs/typeorm';
import chunk from 'lodash.chunk';
@ -11,6 +10,7 @@ import { FieldActorSource } from 'src/engine/metadata-modules/field-metadata/com
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 { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-ids';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
import { CONTACTS_CREATION_BATCH_SIZE } from 'src/modules/contact-creation-manager/constants/contacts-creation-batch-size.constant';
@ -32,7 +32,7 @@ export class CreateCompanyAndContactService {
private readonly createCompaniesService: CreateCompanyService,
@InjectObjectMetadataRepository(WorkspaceMemberWorkspaceEntity)
private readonly workspaceMemberRepository: WorkspaceMemberRepository,
private readonly eventEmitter: EventEmitter2,
private readonly workspaceEventEmitter: WorkspaceEventEmitter,
@InjectRepository(ObjectMetadataEntity, 'metadata')
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
@ -191,18 +191,21 @@ export class CreateCompanyAndContactService {
source,
);
for (const createdPerson of createdPeople) {
this.eventEmitter.emit('person.created', {
name: 'person.created',
workspaceId,
// FixMe: TypeORM typing issue... id is always returned when using save
recordId: createdPerson.id as string,
objectMetadata,
properties: {
after: createdPerson,
},
} satisfies ObjectRecordCreateEvent<any>);
}
this.workspaceEventEmitter.emit(
'person.created',
createdPeople.map(
(createdPerson) =>
({
// FixMe: TypeORM typing issue... id is always returned when using save
recordId: createdPerson.id as string,
objectMetadata,
properties: {
after: createdPerson,
},
}) satisfies ObjectRecordCreateEvent<any>,
),
workspaceId,
);
}
}
}

View File

@ -1,10 +1,10 @@
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Any } from 'typeorm';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
import { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
@ -17,7 +17,7 @@ export class MatchParticipantService<
| MessageParticipantWorkspaceEntity,
> {
constructor(
private readonly eventEmitter: EventEmitter2,
private readonly workspaceEventEmitter: WorkspaceEventEmitter,
private readonly twentyORMManager: TwentyORMManager,
private readonly scopedWorkspaceContextFactory: ScopedWorkspaceContextFactory,
) {}
@ -46,6 +46,10 @@ export class MatchParticipantService<
const workspaceId = this.scopedWorkspaceContextFactory.create().workspaceId;
if (!workspaceId) {
throw new Error('Workspace ID is required');
}
const participantIds = participants.map((participant) => participant.id);
const uniqueParticipantsHandles = [
...new Set(participants.map((participant) => participant.handle)),
@ -109,11 +113,16 @@ export class MatchParticipantService<
transactionManager,
);
this.eventEmitter.emit(`${objectMetadataName}.matched`, {
this.workspaceEventEmitter.emit(
`${objectMetadataName}.matched`,
[
{
workspaceMemberId: null,
participants: matchedParticipants,
},
],
workspaceId,
workspaceMemberId: null,
participants: matchedParticipants,
});
);
}
public async matchParticipantsAfterPersonOrWorkspaceMemberCreation(
@ -127,6 +136,10 @@ export class MatchParticipantService<
const workspaceId = this.scopedWorkspaceContextFactory.create().workspaceId;
if (!workspaceId) {
throw new Error('Workspace ID is required');
}
const participantsToUpdate = await participantRepository.find({
where: {
handle,
@ -155,12 +168,18 @@ export class MatchParticipantService<
},
});
this.eventEmitter.emit(`${objectMetadataName}.matched`, {
this.workspaceEventEmitter.emit(
`${objectMetadataName}.matched`,
[
{
workspaceId,
name: `${objectMetadataName}.matched`,
workspaceMemberId: null,
participants: updatedParticipants,
},
],
workspaceId,
name: `${objectMetadataName}.matched`,
workspaceMemberId: null,
participants: updatedParticipants,
});
);
}
if (workspaceMemberId) {

View File

@ -9,6 +9,7 @@ import { MessageQueue } from 'src/engine/integrations/message-queue/message-queu
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { BlocklistWorkspaceEntity } from 'src/modules/blocklist/standard-objects/blocklist.workspace-entity';
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
@ -32,90 +33,109 @@ export class MessagingBlocklistListener {
@OnEvent('blocklist.created')
async handleCreatedEvent(
payload: ObjectRecordCreateEvent<BlocklistWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<BlocklistWorkspaceEntity>
>,
) {
await this.messageQueueService.add<BlocklistItemDeleteMessagesJobData>(
BlocklistItemDeleteMessagesJob.name,
{
workspaceId: payload.workspaceId,
blocklistItemId: payload.recordId,
},
await Promise.all(
payload.events.map((eventPayload) =>
// TODO: modify to pass an array of blocklist items
this.messageQueueService.add<BlocklistItemDeleteMessagesJobData>(
BlocklistItemDeleteMessagesJob.name,
{
workspaceId: payload.workspaceId,
blocklistItemId: eventPayload.recordId,
},
),
),
);
}
@OnEvent('blocklist.deleted')
async handleDeletedEvent(
payload: ObjectRecordDeleteEvent<BlocklistWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<BlocklistWorkspaceEntity>
>,
) {
const workspaceMemberId = payload.properties.before.workspaceMember.id;
const workspaceId = payload.workspaceId;
const connectedAccount =
await this.connectedAccountRepository.getAllByWorkspaceMemberId(
workspaceMemberId,
for (const eventPayload of payload.events) {
const workspaceMemberId =
eventPayload.properties.before.workspaceMember.id;
const connectedAccount =
await this.connectedAccountRepository.getAllByWorkspaceMemberId(
workspaceMemberId,
workspaceId,
);
if (!connectedAccount || connectedAccount.length === 0) {
return;
}
const messageChannelRepository =
await this.twentyORMManager.getRepository<MessageChannelWorkspaceEntity>(
'messageChannel',
);
const messageChannel = await messageChannelRepository.findOneOrFail({
where: {
connectedAccountId: connectedAccount[0].id,
},
});
await this.messagingChannelSyncStatusService.resetAndScheduleFullMessageListFetch(
messageChannel.id,
workspaceId,
);
if (!connectedAccount || connectedAccount.length === 0) {
return;
}
const messageChannelRepository =
await this.twentyORMManager.getRepository<MessageChannelWorkspaceEntity>(
'messageChannel',
);
const messageChannel = await messageChannelRepository.findOneOrFail({
where: {
connectedAccountId: connectedAccount[0].id,
},
});
await this.messagingChannelSyncStatusService.resetAndScheduleFullMessageListFetch(
messageChannel.id,
workspaceId,
);
}
@OnEvent('blocklist.updated')
async handleUpdatedEvent(
payload: ObjectRecordUpdateEvent<BlocklistWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<BlocklistWorkspaceEntity>
>,
) {
const workspaceMemberId = payload.properties.before.workspaceMember.id;
const workspaceId = payload.workspaceId;
await this.messageQueueService.add<BlocklistItemDeleteMessagesJobData>(
BlocklistItemDeleteMessagesJob.name,
{
workspaceId,
blocklistItemId: payload.recordId,
},
);
for (const eventPayload of payload.events) {
const workspaceMemberId =
eventPayload.properties.before.workspaceMember.id;
const connectedAccount =
await this.connectedAccountRepository.getAllByWorkspaceMemberId(
workspaceMemberId,
workspaceId,
await this.messageQueueService.add<BlocklistItemDeleteMessagesJobData>(
BlocklistItemDeleteMessagesJob.name,
{
workspaceId,
blocklistItemId: eventPayload.recordId,
},
);
if (!connectedAccount || connectedAccount.length === 0) {
return;
const connectedAccount =
await this.connectedAccountRepository.getAllByWorkspaceMemberId(
workspaceMemberId,
workspaceId,
);
if (!connectedAccount || connectedAccount.length === 0) {
continue;
}
const messageChannelRepository =
await this.twentyORMManager.getRepository<MessageChannelWorkspaceEntity>(
'messageChannel',
);
const messageChannel = await messageChannelRepository.findOneOrFail({
where: {
connectedAccountId: connectedAccount[0].id,
},
});
await this.messagingChannelSyncStatusService.resetAndScheduleFullMessageListFetch(
messageChannel.id,
workspaceId,
);
}
const messageChannelRepository =
await this.twentyORMManager.getRepository<MessageChannelWorkspaceEntity>(
'messageChannel',
);
const messageChannel = await messageChannelRepository.findOneOrFail({
where: {
connectedAccountId: connectedAccount[0].id,
},
});
await this.messagingChannelSyncStatusService.resetAndScheduleFullMessageListFetch(
messageChannel.id,
workspaceId,
);
}
}

View File

@ -2,46 +2,39 @@ 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 { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
import {
MessagingConnectedAccountDeletionCleanupJob,
MessagingConnectedAccountDeletionCleanupJobData,
} from 'src/modules/messaging/message-cleaner/jobs/messaging-connected-account-deletion-cleanup.job';
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import {
DeleteConnectedAccountAssociatedCalendarDataJobData,
DeleteConnectedAccountAssociatedCalendarDataJob,
} from 'src/modules/calendar/calendar-event-cleaner/jobs/delete-connected-account-associated-calendar-data.job';
@Injectable()
export class MessagingMessageCleanerConnectedAccountListener {
constructor(
@InjectMessageQueue(MessageQueue.messagingQueue)
private readonly messageQueueService: MessageQueueService,
@InjectMessageQueue(MessageQueue.calendarQueue)
private readonly calendarQueueService: MessageQueueService,
) {}
@OnEvent('connectedAccount.deleted')
async handleDeletedEvent(
payload: ObjectRecordDeleteEvent<ConnectedAccountWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<ConnectedAccountWorkspaceEntity>
>,
) {
await this.messageQueueService.add<MessagingConnectedAccountDeletionCleanupJobData>(
MessagingConnectedAccountDeletionCleanupJob.name,
{
workspaceId: payload.workspaceId,
connectedAccountId: payload.recordId,
},
);
await this.calendarQueueService.add<DeleteConnectedAccountAssociatedCalendarDataJobData>(
DeleteConnectedAccountAssociatedCalendarDataJob.name,
{
workspaceId: payload.workspaceId,
connectedAccountId: payload.recordId,
},
await Promise.all(
payload.events.map((eventPayload) =>
this.messageQueueService.add<MessagingConnectedAccountDeletionCleanupJobData>(
MessagingConnectedAccountDeletionCleanupJob.name,
{
workspaceId: payload.workspaceId,
connectedAccountId: eventPayload.recordId,
},
),
),
);
}
}

View File

@ -1,19 +1,11 @@
import { Module } from '@nestjs/common';
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
import { MessageThreadWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-thread.workspace-entity';
import { MessageWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message.workspace-entity';
import { MessagingConnectedAccountDeletionCleanupJob } from 'src/modules/messaging/message-cleaner/jobs/messaging-connected-account-deletion-cleanup.job';
import { MessagingMessageCleanerConnectedAccountListener } from 'src/modules/messaging/message-cleaner/listeners/messaging-message-cleaner-connected-account.listener';
import { MessagingMessageCleanerService } from 'src/modules/messaging/message-cleaner/services/messaging-message-cleaner.service';
@Module({
imports: [
TwentyORMModule.forFeature([
MessageWorkspaceEntity,
MessageThreadWorkspaceEntity,
]),
],
imports: [],
providers: [
MessagingMessageCleanerService,
MessagingConnectedAccountDeletionCleanupJob,

View File

@ -2,9 +2,10 @@ 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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
import {
MessagingCleanCacheJob,
@ -20,14 +21,20 @@ export class MessagingMessageImportManagerMessageChannelListener {
@OnEvent('messageChannel.deleted')
async handleDeletedEvent(
payload: ObjectRecordDeleteEvent<MessageChannelWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<MessageChannelWorkspaceEntity>
>,
) {
await this.messageQueueService.add<MessagingCleanCacheJobData>(
MessagingCleanCacheJob.name,
{
workspaceId: payload.workspaceId,
messageChannelId: payload.recordId,
},
await Promise.all(
payload.events.map((eventPayload) =>
this.messageQueueService.add<MessagingCleanCacheJobData>(
MessagingCleanCacheJob.name,
{
workspaceId: payload.workspaceId,
messageChannelId: 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 {
MessageParticipantMatchParticipantJobData,
MessageParticipantMatchParticipantJob,
MessageParticipantMatchParticipantJobData,
} from 'src/modules/messaging/message-participant-manager/jobs/message-participant-match-participant.job';
import {
MessageParticipantUnmatchParticipantJobData,
MessageParticipantUnmatchParticipantJob,
MessageParticipantUnmatchParticipantJobData,
} from 'src/modules/messaging/message-participant-manager/jobs/message-participant-unmatch-participant.job';
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
@ -26,49 +27,57 @@ export class MessageParticipantPersonListener {
@OnEvent('person.created')
async handleCreatedEvent(
payload: ObjectRecordCreateEvent<PersonWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<PersonWorkspaceEntity>
>,
) {
if (payload.properties.after.email === null) {
return;
}
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
MessageParticipantMatchParticipantJob.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<MessageParticipantUnmatchParticipantJobData>(
MessageParticipantUnmatchParticipantJob.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;
}
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
MessageParticipantMatchParticipantJob.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')
) {
await this.messageQueueService.add<MessageParticipantUnmatchParticipantJobData>(
MessageParticipantUnmatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: eventPayload.properties.before.email,
personId: eventPayload.recordId,
},
);
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
MessageParticipantMatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: eventPayload.properties.after.email,
personId: eventPayload.recordId,
},
);
}
}
}
}

View File

@ -14,6 +14,7 @@ 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 {
MessageParticipantMatchParticipantJob,
MessageParticipantMatchParticipantJobData,
@ -35,7 +36,9 @@ export class MessageParticipantWorkspaceMemberListener {
@OnEvent('workspaceMember.created')
async handleCreatedEvent(
payload: ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>,
payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>
>,
) {
const workspace = await this.workspaceRepository.findOneBy({
id: payload.workspaceId,
@ -48,47 +51,53 @@ export class MessageParticipantWorkspaceMemberListener {
return;
}
if (payload.properties.after.userEmail === null) {
return;
}
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
MessageParticipantMatchParticipantJob.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<MessageParticipantUnmatchParticipantJobData>(
MessageParticipantUnmatchParticipantJob.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<MessageParticipantMatchParticipantJobData>(
MessageParticipantMatchParticipantJob.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<MessageParticipantUnmatchParticipantJobData>(
MessageParticipantUnmatchParticipantJob.name,
{
workspaceId: payload.workspaceId,
email: eventPayload.properties.before.userEmail,
personId: eventPayload.recordId,
},
);
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
MessageParticipantMatchParticipantJob.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 { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-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,50 +23,54 @@ export class MessageParticipantListener {
) {}
@OnEvent('messageParticipant.matched')
public async handleMessageParticipantMatched(payload: {
workspaceId: string;
workspaceMemberId: string;
participants: MessageParticipantWorkspaceEntity[];
}): Promise<void> {
const messageParticipants = payload.participants ?? [];
public async handleMessageParticipantMatched(
payload: WorkspaceEventBatch<{
workspaceMemberId: string;
participants: MessageParticipantWorkspaceEntity[];
}>,
): Promise<void> {
// TODO: Refactor to insertTimelineActivitiesForObject once
for (const eventPayload of payload.events) {
const messageParticipants = eventPayload.participants ?? [];
// TODO: move to a job?
// TODO: move to a job?
const dataSourceSchema = this.workspaceDataSourceService.getSchemaName(
payload.workspaceId,
);
const dataSourceSchema = this.workspaceDataSourceService.getSchemaName(
payload.workspaceId,
);
const messageObjectMetadata =
await this.objectMetadataRepository.findOneOrFail({
where: {
nameSingular: 'message',
const messageObjectMetadata =
await this.objectMetadataRepository.findOneOrFail({
where: {
nameSingular: 'message',
workspaceId: payload.workspaceId,
},
});
const messageParticipantsWithPersonId = messageParticipants.filter(
(participant) => participant.personId,
);
if (messageParticipantsWithPersonId.length === 0) {
return;
}
await this.timelineActivityRepository.insertTimelineActivitiesForObject(
'person',
messageParticipantsWithPersonId.map((participant) => ({
dataSourceSchema,
name: 'message.linked',
properties: null,
objectName: 'message',
recordId: participant.personId,
workspaceMemberId: eventPayload.workspaceMemberId,
workspaceId: payload.workspaceId,
},
});
const messageParticipantsWithPersonId = messageParticipants.filter(
(participant) => participant.personId,
);
if (messageParticipantsWithPersonId.length === 0) {
return;
linkedObjectMetadataId: messageObjectMetadata.id,
linkedRecordId: participant.messageId,
linkedRecordCachedName: '',
})),
payload.workspaceId,
);
}
await this.timelineActivityRepository.insertTimelineActivitiesForObject(
'person',
messageParticipantsWithPersonId.map((participant) => ({
dataSourceSchema,
name: 'message.linked',
properties: null,
objectName: 'message',
recordId: participant.personId,
workspaceMemberId: payload.workspaceMemberId,
workspaceId: payload.workspaceId,
linkedObjectMetadataId: messageObjectMetadata.id,
linkedRecordId: participant.messageId,
linkedRecordCachedName: '',
})),
payload.workspaceId,
);
}
}

View File

@ -1,12 +1,13 @@
import { ObjectRecordBaseEvent } from 'src/engine/integrations/event-emitter/types/object-record.base.event';
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { AuditLogRepository } from 'src/modules/timeline/repositiories/audit-log.repository';
import { AuditLogWorkspaceEntity } from 'src/modules/timeline/standard-objects/audit-log.workspace-entity';
import { WorkspaceMemberRepository } from 'src/modules/workspace-member/repositories/workspace-member.repository';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
@Processor(MessageQueue.entityEventsToDbQueue)
export class CreateAuditLogFromInternalEvent {
@ -18,33 +19,37 @@ export class CreateAuditLogFromInternalEvent {
) {}
@Process(CreateAuditLogFromInternalEvent.name)
async handle(data: ObjectRecordBaseEvent): Promise<void> {
let workspaceMemberId: string | null = null;
async handle(
data: WorkspaceEventBatch<ObjectRecordBaseEvent>,
): Promise<void> {
for (const eventData of data.events) {
let workspaceMemberId: string | null = null;
if (data.userId) {
const workspaceMember = await this.workspaceMemberService.getByIdOrFail(
data.userId,
if (eventData.userId) {
const workspaceMember = await this.workspaceMemberService.getByIdOrFail(
eventData.userId,
data.workspaceId,
);
workspaceMemberId = workspaceMember.id;
}
if (eventData.properties.diff) {
// we remove "before" and "after" property for a cleaner/slimmer event payload
eventData.properties = {
diff: eventData.properties.diff,
};
}
await this.auditLogRepository.insert(
data.name,
eventData.properties,
workspaceMemberId,
data.name.split('.')[0],
eventData.objectMetadata.id,
eventData.recordId,
data.workspaceId,
);
workspaceMemberId = workspaceMember.id;
}
if (data.properties.diff) {
// we remove "before" and "after" property for a cleaner/slimmer event payload
data.properties = {
diff: data.properties.diff,
};
}
await this.auditLogRepository.insert(
data.name,
data.properties,
workspaceMemberId,
data.name.split('.')[0],
data.objectMetadata.id,
data.recordId,
data.workspaceId,
);
}
}

View File

@ -3,6 +3,7 @@ import { Process } from 'src/engine/integrations/message-queue/decorators/proces
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { TimelineActivityService } from 'src/modules/timeline/services/timeline-activity.service';
import { WorkspaceMemberRepository } from 'src/modules/workspace-member/repositories/workspace-member.repository';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@ -16,33 +17,41 @@ export class UpsertTimelineActivityFromInternalEvent {
) {}
@Process(UpsertTimelineActivityFromInternalEvent.name)
async handle(data: ObjectRecordBaseEvent): Promise<void> {
if (data.userId) {
const workspaceMember = await this.workspaceMemberService.getByIdOrFail(
data.userId,
data.workspaceId,
);
async handle(
data: WorkspaceEventBatch<ObjectRecordBaseEvent>,
): Promise<void> {
for (const eventData of data.events) {
if (eventData.userId) {
const workspaceMember = await this.workspaceMemberService.getByIdOrFail(
eventData.userId,
data.workspaceId,
);
data.workspaceMemberId = workspaceMember.id;
eventData.workspaceMemberId = workspaceMember.id;
}
if (eventData.properties.diff) {
// we remove "before" and "after" property for a cleaner/slimmer event payload
eventData.properties = {
diff: eventData.properties.diff,
};
}
// Temporary
// We ignore every that is not a LinkedObject or a Business Object
if (
eventData.objectMetadata.isSystem &&
eventData.objectMetadata.nameSingular !== 'noteTarget' &&
eventData.objectMetadata.nameSingular !== 'taskTarget'
) {
continue;
}
await this.timelineActivityService.upsertEvent({
...eventData,
workspaceId: data.workspaceId,
name: data.name,
});
}
if (data.properties.diff) {
// we remove "before" and "after" property for a cleaner/slimmer event payload
data.properties = {
diff: data.properties.diff,
};
}
// Temporary
// We ignore every that is not a LinkedObject or a Business Object
if (
data.objectMetadata.isSystem &&
data.objectMetadata.nameSingular !== 'noteTarget' &&
data.objectMetadata.nameSingular !== 'taskTarget'
) {
return;
}
await this.timelineActivityService.upsertEvent(data);
}
}

View File

@ -1,12 +1,12 @@
import { Injectable } from '@nestjs/common';
import { ObjectRecordBaseEvent } from 'src/engine/integrations/event-emitter/types/object-record.base.event';
import { ObjectRecordBaseEventWithNameAndWorkspaceId } from 'src/engine/integrations/event-emitter/types/object-record.base.event';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { TimelineActivityRepository } from 'src/modules/timeline/repositiories/timeline-activity.repository';
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
type TransformedEvent = ObjectRecordBaseEvent & {
type TransformedEvent = ObjectRecordBaseEventWithNameAndWorkspaceId & {
objectName?: string;
linkedRecordCachedName?: string;
linkedRecordId?: string;
@ -26,7 +26,7 @@ export class TimelineActivityService {
task: 'taskTarget',
};
async upsertEvent(event: ObjectRecordBaseEvent) {
async upsertEvent(event: ObjectRecordBaseEventWithNameAndWorkspaceId) {
const events = await this.transformEvent(event);
if (!events || events.length === 0) return;
@ -47,7 +47,7 @@ export class TimelineActivityService {
}
private async transformEvent(
event: ObjectRecordBaseEvent,
event: ObjectRecordBaseEventWithNameAndWorkspaceId,
): Promise<TransformedEvent[]> {
if (['note', 'task'].includes(event.objectMetadata.nameSingular)) {
const linkedObjects = await this.handleLinkedObjects(event);
@ -69,7 +69,9 @@ export class TimelineActivityService {
return [event];
}
private async handleLinkedObjects(event: ObjectRecordBaseEvent) {
private async handleLinkedObjects(
event: ObjectRecordBaseEventWithNameAndWorkspaceId,
) {
const dataSourceSchema = this.workspaceDataSourceService.getSchemaName(
event.workspaceId,
);
@ -92,7 +94,7 @@ export class TimelineActivityService {
}
private async processActivity(
event: ObjectRecordBaseEvent,
event: ObjectRecordBaseEventWithNameAndWorkspaceId,
dataSourceSchema: string,
activityType: string,
) {
@ -145,7 +147,7 @@ export class TimelineActivityService {
}
private async processActivityTarget(
event: ObjectRecordBaseEvent,
event: ObjectRecordBaseEventWithNameAndWorkspaceId,
dataSourceSchema: string,
activityType: string,
) {

View File

@ -10,6 +10,7 @@ import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decora
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { WorkflowEventListenerWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-event-listener.workspace-entity';
import {
WorkflowEventTriggerJob,
@ -28,25 +29,32 @@ export class DatabaseEventTriggerListener {
) {}
@OnEvent('*.created')
async handleObjectRecordCreateEvent(payload: ObjectRecordCreateEvent<any>) {
async handleObjectRecordCreateEvent(
payload: WorkspaceEventBatch<ObjectRecordCreateEvent<any>>,
) {
await this.handleEvent(payload);
}
@OnEvent('*.updated')
async handleObjectRecordUpdateEvent(payload: ObjectRecordUpdateEvent<any>) {
async handleObjectRecordUpdateEvent(
payload: WorkspaceEventBatch<ObjectRecordUpdateEvent<any>>,
) {
await this.handleEvent(payload);
}
@OnEvent('*.deleted')
async handleObjectRecordDeleteEvent(payload: ObjectRecordDeleteEvent<any>) {
async handleObjectRecordDeleteEvent(
payload: WorkspaceEventBatch<ObjectRecordDeleteEvent<any>>,
) {
await this.handleEvent(payload);
}
private async handleEvent(
payload:
payload: WorkspaceEventBatch<
| ObjectRecordCreateEvent<any>
| ObjectRecordUpdateEvent<any>
| ObjectRecordDeleteEvent<any>,
| ObjectRecordDeleteEvent<any>
>,
) {
const workspaceId = payload.workspaceId;
const eventName = payload.name;
@ -84,15 +92,17 @@ export class DatabaseEventTriggerListener {
});
for (const eventListener of eventListeners) {
this.messageQueueService.add<WorkflowEventTriggerJobData>(
WorkflowEventTriggerJob.name,
{
workspaceId,
workflowId: eventListener.workflowId,
payload,
},
{ retryLimit: 3 },
);
for (const eventPayload of payload.events) {
this.messageQueueService.add<WorkflowEventTriggerJobData>(
WorkflowEventTriggerJob.name,
{
workspaceId,
workflowId: eventListener.workflowId,
payload: eventPayload,
},
{ retryLimit: 3 },
);
}
}
}
}

View File

@ -1,15 +1,17 @@
import { Module } from '@nestjs/common';
import { JobsModule } from 'src/engine/integrations/message-queue/jobs.module';
import { IntegrationsModule } from 'src/engine/integrations/integrations.module';
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
import { JobsModule } from 'src/engine/integrations/message-queue/jobs.module';
import { MessageQueueModule } from 'src/engine/integrations/message-queue/message-queue.module';
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
import { WorkspaceEventEmitterModule } from 'src/engine/workspace-event-emitter/workspace-event-emitter.module';
@Module({
imports: [
TwentyORMModule.register({}),
IntegrationsModule,
MessageQueueModule.registerExplorer(),
WorkspaceEventEmitterModule,
JobsModule,
],
})