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

@ -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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; 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 { 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'; import { UpsertTimelineActivityFromInternalEvent } from 'src/modules/timeline/jobs/upsert-timeline-activity-from-internal-event.job';
@ -19,40 +20,46 @@ export class EntityEventsToDbListener {
) {} ) {}
@OnEvent('*.created') @OnEvent('*.created')
async handleCreate(payload: ObjectRecordCreateEvent<any>) { async handleCreate(
payload: WorkspaceEventBatch<ObjectRecordCreateEvent<any>>,
) {
return this.handle(payload); return this.handle(payload);
} }
@OnEvent('*.updated') @OnEvent('*.updated')
async handleUpdate(payload: ObjectRecordUpdateEvent<any>) { async handleUpdate(
payload.properties.diff = objectRecordChangedValues( payload: WorkspaceEventBatch<ObjectRecordUpdateEvent<any>>,
payload.properties.before, ) {
payload.properties.after, for (const eventPayload of payload.events) {
payload.properties.updatedFields, eventPayload.properties.diff = objectRecordChangedValues(
payload.objectMetadata, eventPayload.properties.before,
); eventPayload.properties.after,
eventPayload.properties.updatedFields,
eventPayload.objectMetadata,
);
}
return this.handle(payload); return this.handle(payload);
} }
@OnEvent('*.deleted') @OnEvent('*.deleted')
async handleDelete(payload: ObjectRecordUpdateEvent<any>) { async handleDelete(
payload: WorkspaceEventBatch<ObjectRecordUpdateEvent<any>>,
) {
return this.handle(payload); return this.handle(payload);
} }
private async handle(payload: ObjectRecordBaseEvent) { private async handle(payload: WorkspaceEventBatch<ObjectRecordBaseEvent>) {
if (!payload.objectMetadata?.isAuditLogged) { payload.events = payload.events.filter(
return; (event) => event.objectMetadata?.isAuditLogged,
}
this.messageQueueService.add<ObjectRecordBaseEvent>(
CreateAuditLogFromInternalEvent.name,
payload,
); );
this.messageQueueService.add<ObjectRecordBaseEvent>( await this.messageQueueService.add<
UpsertTimelineActivityFromInternalEvent.name, WorkspaceEventBatch<ObjectRecordBaseEvent>
payload, >(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 { AnalyticsService } from 'src/engine/core-modules/analytics/analytics.service';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service'; import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event'; 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() @Injectable()
export class TelemetryListener { export class TelemetryListener {
@ -13,36 +14,48 @@ export class TelemetryListener {
) {} ) {}
@OnEvent('*.created') @OnEvent('*.created')
async handleAllCreate(payload: ObjectRecordCreateEvent<any>) { async handleAllCreate(
await this.analyticsService.create( payload: WorkspaceEventBatch<ObjectRecordCreateEvent<any>>,
{ ) {
type: 'track', await Promise.all(
data: { payload.events.map((eventPayload) =>
eventName: payload.name, this.analyticsService.create(
}, {
}, type: 'track',
payload.userId, data: {
payload.workspaceId, eventName: payload.name,
'', // voluntarely not retrieving this },
'', // to avoid slowing down },
this.environmentService.get('SERVER_URL'), eventPayload.userId,
payload.workspaceId,
'', // voluntarily not retrieving this
'', // to avoid slowing down
this.environmentService.get('SERVER_URL'),
),
),
); );
} }
@OnEvent('user.signup') @OnEvent('user.signup')
async handleUserSignup(payload: ObjectRecordCreateEvent<any>) { async handleUserSignup(
await this.analyticsService.create( payload: WorkspaceEventBatch<ObjectRecordCreateEvent<any>>,
{ ) {
type: 'track', await Promise.all(
data: { payload.events.map((eventPayload) =>
eventName: 'user.signup', this.analyticsService.create(
}, {
}, type: 'track',
payload.userId, data: {
payload.workspaceId, eventName: 'user.signup',
'', },
'', },
this.environmentService.get('SERVER_URL'), eventPayload.userId,
payload.workspaceId,
'',
'',
this.environmentService.get('SERVER_URL'),
),
),
); );
} }
} }

View File

@ -1,5 +1,4 @@
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import isEmpty from 'lodash.isempty'; import isEmpty from 'lodash.isempty';
import { DataSource } from 'typeorm'; 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 { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
import { isQueryTimeoutError } from 'src/engine/utils/query-timeout.util'; import { isQueryTimeoutError } from 'src/engine/utils/query-timeout.util';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; 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 { import {
PGGraphQLMutation, PGGraphQLMutation,
@ -78,7 +79,7 @@ export class WorkspaceQueryRunnerService {
private readonly queryResultGettersFactory: QueryResultGettersFactory, private readonly queryResultGettersFactory: QueryResultGettersFactory,
@InjectMessageQueue(MessageQueue.webhookQueue) @InjectMessageQueue(MessageQueue.webhookQueue)
private readonly messageQueueService: MessageQueueService, private readonly messageQueueService: MessageQueueService,
private readonly eventEmitter: EventEmitter2, private readonly workspaceEventEmitter: WorkspaceEventEmitter,
private readonly workspaceQueryHookService: WorkspaceQueryHookService, private readonly workspaceQueryHookService: WorkspaceQueryHookService,
private readonly environmentService: EnvironmentService, private readonly environmentService: EnvironmentService,
private readonly duplicateService: DuplicateService, private readonly duplicateService: DuplicateService,
@ -304,18 +305,21 @@ export class WorkspaceQueryRunnerService {
options, options,
); );
parsedResults.forEach((record) => { this.workspaceEventEmitter.emit(
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.created`, { `${objectMetadataItem.nameSingular}.created`,
name: `${objectMetadataItem.nameSingular}.created`, parsedResults.map(
workspaceId: authContext.workspace.id, (record) =>
userId: authContext.user?.id, ({
recordId: record.id, userId: authContext.user?.id,
objectMetadata: objectMetadataItem, recordId: record.id,
properties: { objectMetadata: objectMetadataItem,
after: record, properties: {
}, after: record,
} satisfies ObjectRecordCreateEvent<any>); },
}); }) satisfies ObjectRecordCreateEvent<any>,
),
authContext.workspace.id,
);
return parsedResults; return parsedResults;
} }
@ -440,18 +444,22 @@ export class WorkspaceQueryRunnerService {
options, options,
); );
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.updated`, { this.workspaceEventEmitter.emit(
name: `${objectMetadataItem.nameSingular}.updated`, `${objectMetadataItem.nameSingular}.updated`,
workspaceId: authContext.workspace.id, [
userId: authContext.user?.id, {
recordId: existingRecord.id, userId: authContext.user?.id,
objectMetadata: objectMetadataItem, recordId: existingRecord.id,
properties: { objectMetadata: objectMetadataItem,
updatedFields: Object.keys(args.data), properties: {
before: this.removeNestedProperties(existingRecord as Record), updatedFields: Object.keys(args.data),
after: this.removeNestedProperties(parsedResults?.[0]), before: this.removeNestedProperties(existingRecord as Record),
}, after: this.removeNestedProperties(parsedResults?.[0]),
} satisfies ObjectRecordUpdateEvent<any>); },
} satisfies ObjectRecordUpdateEvent<any>,
],
authContext.workspace.id,
);
return parsedResults?.[0]; return parsedResults?.[0];
} }
@ -513,30 +521,36 @@ export class WorkspaceQueryRunnerService {
options, options,
); );
parsedResults.forEach((record) => { const eventsToEmit: ObjectRecordUpdateEvent<any>[] = parsedResults
const existingRecord = mappedRecords.get(record.id); .map((record) => {
const existingRecord = mappedRecords.get(record.id);
if (!existingRecord) { if (!existingRecord) {
this.logger.warn( this.logger.warn(
`Record with id ${record.id} not found in the database`, `Record with id ${record.id} not found in the database`,
); );
return; return;
} }
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.updated`, { return {
name: `${objectMetadataItem.nameSingular}.updated`, userId: authContext.user?.id,
workspaceId: authContext.workspace.id, recordId: existingRecord.id,
userId: authContext.user?.id, objectMetadata: objectMetadataItem,
recordId: existingRecord.id, properties: {
objectMetadata: objectMetadataItem, updatedFields: Object.keys(args.data),
properties: { before: this.removeNestedProperties(existingRecord as Record),
updatedFields: Object.keys(args.data), after: this.removeNestedProperties(record),
before: this.removeNestedProperties(existingRecord as Record), },
after: this.removeNestedProperties(record), };
}, })
} satisfies ObjectRecordUpdateEvent<any>); .filter(isDefined);
});
this.workspaceEventEmitter.emit(
`${objectMetadataItem.nameSingular}.updated`,
eventsToEmit,
authContext.workspace.id,
);
return parsedResults; return parsedResults;
} }
@ -602,18 +616,21 @@ export class WorkspaceQueryRunnerService {
options, options,
); );
parsedResults.forEach((record) => { this.workspaceEventEmitter.emit(
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.deleted`, { `${objectMetadataItem.nameSingular}.deleted`,
name: `${objectMetadataItem.nameSingular}.deleted`, parsedResults.map(
workspaceId: authContext.workspace.id, (record) =>
userId: authContext.user?.id, ({
recordId: record.id, userId: authContext.user?.id,
objectMetadata: objectMetadataItem, recordId: record.id,
properties: { objectMetadata: objectMetadataItem,
before: this.removeNestedProperties(record), properties: {
}, before: this.removeNestedProperties(record),
} satisfies ObjectRecordDeleteEvent<any>); },
}); }) satisfies ObjectRecordDeleteEvent<any>,
),
authContext.workspace.id,
);
return parsedResults; return parsedResults;
} }
@ -744,18 +761,21 @@ export class WorkspaceQueryRunnerService {
options, options,
); );
parsedResults.forEach((record) => { this.workspaceEventEmitter.emit(
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.created`, { `${objectMetadataItem.nameSingular}.created`,
name: `${objectMetadataItem.nameSingular}.created`, parsedResults.map(
workspaceId: authContext.workspace.id, (record) =>
userId: authContext.user?.id, ({
recordId: record.id, userId: authContext.user?.id,
objectMetadata: objectMetadataItem, recordId: record.id,
properties: { objectMetadata: objectMetadataItem,
after: this.removeNestedProperties(record), properties: {
}, after: this.removeNestedProperties(record),
} satisfies ObjectRecordCreateEvent<any>); },
}); }) satisfies ObjectRecordCreateEvent<any>,
),
authContext.workspace.id,
);
return parsedResults; return parsedResults;
} }
@ -821,19 +841,23 @@ export class WorkspaceQueryRunnerService {
options, options,
); );
this.eventEmitter.emit(`${objectMetadataItem.nameSingular}.deleted`, { this.workspaceEventEmitter.emit(
name: `${objectMetadataItem.nameSingular}.deleted`, `${objectMetadataItem.nameSingular}.deleted`,
workspaceId: authContext.workspace.id, [
userId: authContext.user?.id, {
recordId: args.id, userId: authContext.user?.id,
objectMetadata: objectMetadataItem, recordId: args.id,
properties: { objectMetadata: objectMetadataItem,
before: { properties: {
...(existingRecord ?? {}), before: {
...this.removeNestedProperties(parsedResults?.[0]), ...(existingRecord ?? {}),
}, ...this.removeNestedProperties(parsedResults?.[0]),
}, },
} satisfies ObjectRecordDeleteEvent<any>); },
} satisfies ObjectRecordDeleteEvent<any>,
],
authContext.workspace.id,
);
return parsedResults?.[0]; 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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; 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'; import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@Injectable() @Injectable()
@ -23,7 +24,9 @@ export class BillingWorkspaceMemberListener {
@OnEvent('workspaceMember.created') @OnEvent('workspaceMember.created')
@OnEvent('workspaceMember.deleted') @OnEvent('workspaceMember.deleted')
async handleCreateOrDeleteEvent( async handleCreateOrDeleteEvent(
payload: ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>
>,
) { ) {
if (!this.environmentService.get('IS_BILLING_ENABLED')) { if (!this.environmentService.get('IS_BILLING_ENABLED')) {
return; 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 { UserModule } from 'src/engine/core-modules/user/user.module';
import { WorkflowTriggerCoreModule } from 'src/engine/core-modules/workflow/core-workflow-trigger.module'; import { WorkflowTriggerCoreModule } from 'src/engine/core-modules/workflow/core-workflow-trigger.module';
import { WorkspaceModule } from 'src/engine/core-modules/workspace/workspace.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 { AnalyticsModule } from './analytics/analytics.module';
import { ClientConfigModule } from './client-config/client-config.module'; import { ClientConfigModule } from './client-config/client-config.module';
@ -36,6 +37,7 @@ import { FileModule } from './file/file.module';
AISQLQueryModule, AISQLQueryModule,
PostgresCredentialsModule, PostgresCredentialsModule,
WorkflowTriggerCoreModule, WorkflowTriggerCoreModule,
WorkspaceEventEmitterModule,
], ],
exports: [ exports: [
AnalyticsModule, AnalyticsModule,

View File

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

View File

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

View File

@ -1,4 +1,3 @@
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import assert from 'assert'; import assert from 'assert';
@ -16,6 +15,7 @@ import {
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event'; 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 { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; 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'; import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
// eslint-disable-next-line @nx/workspace-inject-workspace-repository // 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 userRepository: Repository<User>,
private readonly dataSourceService: DataSourceService, private readonly dataSourceService: DataSourceService,
private readonly typeORMService: TypeORMService, private readonly typeORMService: TypeORMService,
private readonly eventEmitter: EventEmitter2, private readonly workspaceEventEmitter: WorkspaceEventEmitter,
private readonly workspaceService: WorkspaceService, private readonly workspaceService: WorkspaceService,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager, private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) { ) {
@ -110,15 +110,16 @@ export class UserService extends TypeOrmQueryService<User> {
const payload = const payload =
new ObjectRecordDeleteEvent<WorkspaceMemberWorkspaceEntity>(); new ObjectRecordDeleteEvent<WorkspaceMemberWorkspaceEntity>();
payload.workspaceId = workspaceId;
payload.properties = { payload.properties = {
before: workspaceMember, before: workspaceMember,
}; };
payload.name = 'workspaceMember.deleted';
payload.recordId = workspaceMember.id; payload.recordId = workspaceMember.id;
payload.name = 'workspaceMember.deleted';
this.eventEmitter.emit('workspaceMember.deleted', payload); this.workspaceEventEmitter.emit(
'workspaceMember.deleted',
[payload],
workspaceId,
);
return user; 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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; 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'; import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@Injectable() @Injectable()
@ -23,39 +24,51 @@ export class WorkspaceWorkspaceMemberListener {
@OnEvent('workspaceMember.updated') @OnEvent('workspaceMember.updated')
async handleUpdateEvent( async handleUpdateEvent(
payload: ObjectRecordUpdateEvent<WorkspaceMemberWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<WorkspaceMemberWorkspaceEntity>
>,
) { ) {
const { firstName: firstNameAfter, lastName: lastNameAfter } = await Promise.all(
payload.properties.after.name; payload.events.map((eventPayload) => {
const { firstName: firstNameAfter, lastName: lastNameAfter } =
eventPayload.properties.after.name;
if (firstNameAfter === '' && lastNameAfter === '') { if (firstNameAfter === '' && lastNameAfter === '') {
return; return;
} }
if (!payload.userId) { if (!eventPayload.userId) {
return; return;
} }
await this.onboardingService.setOnboardingCreateProfilePending({ return this.onboardingService.setOnboardingCreateProfilePending({
userId: payload.userId, userId: eventPayload.userId,
workspaceId: payload.workspaceId, workspaceId: payload.workspaceId,
value: false, value: false,
}); });
}),
);
} }
@OnEvent('workspaceMember.deleted') @OnEvent('workspaceMember.deleted')
async handleDeleteEvent( 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) { if (!userId) {
return; return;
} }
await this.messageQueueService.add<HandleWorkspaceMemberDeletedJobData>( return this.messageQueueService.add<HandleWorkspaceMemberDeletedJobData>(
HandleWorkspaceMemberDeletedJob.name, HandleWorkspaceMemberDeletedJob.name,
{ workspaceId: payload.workspaceId, userId }, { 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'; import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
export class ObjectRecordBaseEvent { export class ObjectRecordBaseEvent {
name: string;
workspaceId: string;
recordId: string; recordId: string;
userId?: string; userId?: string;
workspaceMemberId?: string; workspaceMemberId?: string;
objectMetadata: ObjectMetadataInterface; objectMetadata: ObjectMetadataInterface;
properties: any; 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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; 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 { BlocklistWorkspaceEntity } from 'src/modules/blocklist/standard-objects/blocklist.workspace-entity';
import { import {
BlocklistItemDeleteCalendarEventsJob, BlocklistItemDeleteCalendarEventsJob,
@ -26,48 +27,74 @@ export class CalendarBlocklistListener {
@OnEvent('blocklist.created') @OnEvent('blocklist.created')
async handleCreatedEvent( async handleCreatedEvent(
payload: ObjectRecordCreateEvent<BlocklistWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<BlocklistWorkspaceEntity>
>,
) { ) {
await this.messageQueueService.add<BlocklistItemDeleteCalendarEventsJobData>( await Promise.all(
BlocklistItemDeleteCalendarEventsJob.name, payload.events.map((eventPayload) =>
{ this.messageQueueService.add<BlocklistItemDeleteCalendarEventsJobData>(
workspaceId: payload.workspaceId, BlocklistItemDeleteCalendarEventsJob.name,
blocklistItemId: payload.recordId, {
}, workspaceId: payload.workspaceId,
blocklistItemId: eventPayload.recordId,
},
),
),
); );
} }
@OnEvent('blocklist.deleted') @OnEvent('blocklist.deleted')
async handleDeletedEvent( async handleDeletedEvent(
payload: ObjectRecordDeleteEvent<BlocklistWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<BlocklistWorkspaceEntity>
>,
) { ) {
await this.messageQueueService.add<BlocklistReimportCalendarEventsJobData>( await Promise.all(
BlocklistReimportCalendarEventsJob.name, payload.events.map((eventPayload) =>
{ this.messageQueueService.add<BlocklistReimportCalendarEventsJobData>(
workspaceId: payload.workspaceId, BlocklistReimportCalendarEventsJob.name,
workspaceMemberId: payload.properties.before.workspaceMember.id, {
}, workspaceId: payload.workspaceId,
workspaceMemberId:
eventPayload.properties.before.workspaceMember.id,
},
),
),
); );
} }
@OnEvent('blocklist.updated') @OnEvent('blocklist.updated')
async handleUpdatedEvent( async handleUpdatedEvent(
payload: ObjectRecordUpdateEvent<BlocklistWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<BlocklistWorkspaceEntity>
>,
) { ) {
await this.messageQueueService.add<BlocklistItemDeleteCalendarEventsJobData>( await Promise.all(
BlocklistItemDeleteCalendarEventsJob.name, payload.events.reduce((acc: Promise<void>[], eventPayload) => {
{ acc.push(
workspaceId: payload.workspaceId, this.messageQueueService.add<BlocklistItemDeleteCalendarEventsJobData>(
blocklistItemId: payload.recordId, BlocklistItemDeleteCalendarEventsJob.name,
}, {
); workspaceId: payload.workspaceId,
blocklistItemId: eventPayload.recordId,
},
),
);
await this.messageQueueService.add<BlocklistReimportCalendarEventsJobData>( acc.push(
BlocklistReimportCalendarEventsJob.name, this.messageQueueService.add<BlocklistReimportCalendarEventsJobData>(
{ BlocklistReimportCalendarEventsJob.name,
workspaceId: payload.workspaceId, {
workspaceMemberId: payload.properties.after.workspaceMember.id, workspaceId: payload.workspaceId,
}, workspaceMemberId:
eventPayload.properties.after.workspaceMember.id,
},
),
);
return acc;
}, []),
); );
} }
} }

View File

@ -1,15 +1,15 @@
import { Module } from '@nestjs/common'; 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 { 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 { 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({ @Module({
imports: [TwentyORMModule.forFeature([CalendarEventWorkspaceEntity])], imports: [],
providers: [ providers: [
CalendarEventCleanerService, CalendarEventCleanerService,
DeleteConnectedAccountAssociatedCalendarDataJob, DeleteConnectedAccountAssociatedCalendarDataJob,
CalendarEventCleanerConnectedAccountListener,
], ],
exports: [CalendarEventCleanerService], 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 { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Any } from 'typeorm'; import { Any } from 'typeorm';
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator'; import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; 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 { 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 { 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 { 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'; import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';
@ -19,7 +20,6 @@ import {
CreateCompanyAndContactJob, CreateCompanyAndContactJob,
CreateCompanyAndContactJobData, CreateCompanyAndContactJobData,
} from 'src/modules/contact-creation-manager/jobs/create-company-and-contact.job'; } 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() @Injectable()
export class CalendarSaveEventsService { export class CalendarSaveEventsService {
@ -28,7 +28,7 @@ export class CalendarSaveEventsService {
private readonly calendarEventParticipantService: CalendarEventParticipantService, private readonly calendarEventParticipantService: CalendarEventParticipantService,
@InjectMessageQueue(MessageQueue.contactCreationQueue) @InjectMessageQueue(MessageQueue.contactCreationQueue)
private readonly messageQueueService: MessageQueueService, private readonly messageQueueService: MessageQueueService,
private readonly eventEmitter: EventEmitter2, private readonly workspaceEventEmitter: WorkspaceEventEmitter,
) {} ) {}
public async saveCalendarEventsAndEnqueueContactCreationJob( 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) { if (calendarChannel.isContactAutoCreationEnabled) {
await this.messageQueueService.add<CreateCompanyAndContactJobData>( await this.messageQueueService.add<CreateCompanyAndContactJobData>(
CreateCompanyAndContactJob.name, 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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { import {
CalendarEventParticipantMatchParticipantJobData,
CalendarEventParticipantMatchParticipantJob, CalendarEventParticipantMatchParticipantJob,
CalendarEventParticipantMatchParticipantJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-match-participant.job'; } from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-match-participant.job';
import { import {
CalendarEventParticipantUnmatchParticipantJobData,
CalendarEventParticipantUnmatchParticipantJob, CalendarEventParticipantUnmatchParticipantJob,
CalendarEventParticipantUnmatchParticipantJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-unmatch-participant.job'; } 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'; import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
@ -26,49 +27,59 @@ export class CalendarEventParticipantPersonListener {
@OnEvent('person.created') @OnEvent('person.created')
async handleCreatedEvent( async handleCreatedEvent(
payload: ObjectRecordCreateEvent<PersonWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<PersonWorkspaceEntity>
>,
) { ) {
if (payload.properties.after.email === null) { for (const eventPayload of payload.events) {
return; if (eventPayload.properties.after.email === null) {
} continue;
}
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,
},
);
// TODO: modify this job to take an array of participants to match
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>( await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
CalendarEventParticipantMatchParticipantJob.name, CalendarEventParticipantMatchParticipantJob.name,
{ {
workspaceId: payload.workspaceId, workspaceId: payload.workspaceId,
email: payload.properties.after.email, email: eventPayload.properties.after.email,
personId: payload.recordId, 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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { import {
CalendarEventParticipantMatchParticipantJob, CalendarEventParticipantMatchParticipantJob,
CalendarEventParticipantMatchParticipantJobData, CalendarEventParticipantMatchParticipantJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-match-participant.job'; } from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-match-participant.job';
import { import {
CalendarEventParticipantUnmatchParticipantJobData,
CalendarEventParticipantUnmatchParticipantJob, CalendarEventParticipantUnmatchParticipantJob,
CalendarEventParticipantUnmatchParticipantJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-unmatch-participant.job'; } 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'; import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@ -26,49 +27,57 @@ export class CalendarEventParticipantWorkspaceMemberListener {
@OnEvent('workspaceMember.created') @OnEvent('workspaceMember.created')
async handleCreatedEvent( async handleCreatedEvent(
payload: ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>
>,
) { ) {
if (payload.properties.after.userEmail === null) { for (const eventPayload of payload.events) {
return; if (eventPayload.properties.after.userEmail === null) {
} continue;
}
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,
},
);
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>( await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
CalendarEventParticipantMatchParticipantJob.name, CalendarEventParticipantMatchParticipantJob.name,
{ {
workspaceId: payload.workspaceId, workspaceId: payload.workspaceId,
email: payload.properties.after.userEmail, email: eventPayload.properties.after.userEmail,
workspaceMemberId: payload.recordId, 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 { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; 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 { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
import { TimelineActivityRepository } from 'src/modules/timeline/repositiories/timeline-activity.repository'; import { TimelineActivityRepository } from 'src/modules/timeline/repositiories/timeline-activity.repository';
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity'; import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
@ -22,49 +23,55 @@ export class CalendarEventParticipantListener {
) {} ) {}
@OnEvent('calendarEventParticipant.matched') @OnEvent('calendarEventParticipant.matched')
public async handleCalendarEventParticipantMatchedEvent(payload: { public async handleCalendarEventParticipantMatchedEvent(
workspaceId: string; payload: WorkspaceEventBatch<{
workspaceMemberId: string; workspaceMemberId: string;
participants: CalendarEventParticipantWorkspaceEntity[]; participants: CalendarEventParticipantWorkspaceEntity[];
}): Promise<void> { }>,
const calendarEventParticipants = payload.participants ?? []; ): 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( // TODO: move to a job?
payload.workspaceId,
);
const calendarEventObjectMetadata = const dataSourceSchema =
await this.objectMetadataRepository.findOneOrFail({ this.workspaceDataSourceService.getSchemaName(workspaceId);
where: {
nameSingular: 'calendarEvent',
workspaceId: payload.workspaceId,
},
});
const calendarEventParticipantsWithPersonId = const calendarEventObjectMetadata =
calendarEventParticipants.filter((participant) => participant.personId); await this.objectMetadataRepository.findOneOrFail({
where: {
nameSingular: 'calendarEvent',
workspaceId,
},
});
if (calendarEventParticipantsWithPersonId.length === 0) { const calendarEventParticipantsWithPersonId =
return; 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( await this.matchParticipantService.matchParticipants(
savedParticipants, savedParticipants,
'messageParticipant', 'calendarEventParticipant',
transactionManager, 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 { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; 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 { 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 { 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'; import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@ -16,27 +17,31 @@ export class ConnectedAccountListener {
@OnEvent('connectedAccount.deleted') @OnEvent('connectedAccount.deleted')
async handleDeletedEvent( async handleDeletedEvent(
payload: ObjectRecordDeleteEvent<ConnectedAccountWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<ConnectedAccountWorkspaceEntity>
>,
) { ) {
const workspaceMemberId = payload.properties.before.accountOwnerId; for (const eventPayload of payload.events) {
const workspaceId = payload.workspaceId; const workspaceMemberId = eventPayload.properties.before.accountOwnerId;
const workspaceMemberRepository = const workspaceId = payload.workspaceId;
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkspaceMemberWorkspaceEntity>( 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, 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 { 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'; 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 { 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 { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager'; 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'; import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
@WorkspaceQueryHook(`connectedAccount.deleteOne`) @WorkspaceQueryHook(`connectedAccount.deleteOne`)
@ -15,7 +14,7 @@ export class ConnectedAccountDeleteOnePreQueryHook
{ {
constructor( constructor(
private readonly twentyORMManager: TwentyORMManager, private readonly twentyORMManager: TwentyORMManager,
private eventEmitter: EventEmitter2, private readonly workspaceEventEmitter: WorkspaceEventEmitter,
) {} ) {}
async execute( async execute(
@ -34,16 +33,19 @@ export class ConnectedAccountDeleteOnePreQueryHook
connectedAccountId, connectedAccountId,
}); });
messageChannels.forEach((messageChannel) => { this.workspaceEventEmitter.emit(
this.eventEmitter.emit('messageChannel.deleted', { 'messageChannel.deleted',
workspaceId: authContext.workspace.id, messageChannels.map(
name: 'messageChannel.deleted', (messageChannel) =>
recordId: messageChannel.id, ({
} satisfies Pick< recordId: messageChannel.id,
ObjectRecordDeleteEvent<MessageChannelWorkspaceEntity>, }) satisfies Pick<
'workspaceId' | 'recordId' | 'name' ObjectRecordDeleteEvent<MessageChannelWorkspaceEntity>,
>); 'recordId'
}); >,
),
authContext.workspace.id,
);
return payload; return payload;
} }

View File

@ -1,11 +1,9 @@
import { Module } from '@nestjs/common'; 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 { 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({ @Module({
imports: [TwentyORMModule.forFeature([MessageChannelWorkspaceEntity])], imports: [],
providers: [ConnectedAccountDeleteOnePreQueryHook], providers: [ConnectedAccountDeleteOnePreQueryHook],
}) })
export class ConnectedAccountQueryHookModule {} 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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { import {
CalendarCreateCompanyAndContactAfterSyncJobData,
CalendarCreateCompanyAndContactAfterSyncJob, CalendarCreateCompanyAndContactAfterSyncJob,
CalendarCreateCompanyAndContactAfterSyncJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-create-company-and-contact-after-sync.job'; } 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'; import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
@ -21,22 +22,28 @@ export class AutoCompaniesAndContactsCreationCalendarChannelListener {
@OnEvent('calendarChannel.updated') @OnEvent('calendarChannel.updated')
async handleUpdatedEvent( async handleUpdatedEvent(
payload: ObjectRecordUpdateEvent<MessageChannelWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<MessageChannelWorkspaceEntity>
>,
) { ) {
if ( await Promise.all(
objectRecordChangedProperties( payload.events.map((eventPayload) => {
payload.properties.before, if (
payload.properties.after, objectRecordChangedProperties(
).includes('isContactAutoCreationEnabled') && eventPayload.properties.before,
payload.properties.after.isContactAutoCreationEnabled eventPayload.properties.after,
) { ).includes('isContactAutoCreationEnabled') &&
await this.messageQueueService.add<CalendarCreateCompanyAndContactAfterSyncJobData>( eventPayload.properties.after.isContactAutoCreationEnabled
CalendarCreateCompanyAndContactAfterSyncJob.name, ) {
{ return this.messageQueueService.add<CalendarCreateCompanyAndContactAfterSyncJobData>(
workspaceId: payload.workspaceId, CalendarCreateCompanyAndContactAfterSyncJob.name,
calendarChannelId: payload.recordId, {
}, 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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; 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 { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
import { import {
MessagingCreateCompanyAndContactAfterSyncJobData,
MessagingCreateCompanyAndContactAfterSyncJob, MessagingCreateCompanyAndContactAfterSyncJob,
MessagingCreateCompanyAndContactAfterSyncJobData,
} from 'src/modules/messaging/message-participant-manager/jobs/messaging-create-company-and-contact-after-sync.job'; } from 'src/modules/messaging/message-participant-manager/jobs/messaging-create-company-and-contact-after-sync.job';
@Injectable() @Injectable()
@ -21,22 +22,28 @@ export class AutoCompaniesAndContactsCreationMessageChannelListener {
@OnEvent('messageChannel.updated') @OnEvent('messageChannel.updated')
async handleUpdatedEvent( async handleUpdatedEvent(
payload: ObjectRecordUpdateEvent<MessageChannelWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<MessageChannelWorkspaceEntity>
>,
) { ) {
if ( await Promise.all(
objectRecordChangedProperties( payload.events.map((eventPayload) => {
payload.properties.before, if (
payload.properties.after, objectRecordChangedProperties(
).includes('isContactAutoCreationEnabled') && eventPayload.properties.before,
payload.properties.after.isContactAutoCreationEnabled eventPayload.properties.after,
) { ).includes('isContactAutoCreationEnabled') &&
await this.messageQueueService.add<MessagingCreateCompanyAndContactAfterSyncJobData>( eventPayload.properties.after.isContactAutoCreationEnabled
MessagingCreateCompanyAndContactAfterSyncJob.name, ) {
{ return this.messageQueueService.add<MessagingCreateCompanyAndContactAfterSyncJobData>(
workspaceId: payload.workspaceId, MessagingCreateCompanyAndContactAfterSyncJob.name,
messageChannelId: payload.recordId, {
}, workspaceId: payload.workspaceId,
); messageChannelId: eventPayload.recordId,
} },
);
}
}),
);
} }
} }

View File

@ -1,5 +1,4 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import chunk from 'lodash.chunk'; 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 { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; 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 { 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 { 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'; 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, private readonly createCompaniesService: CreateCompanyService,
@InjectObjectMetadataRepository(WorkspaceMemberWorkspaceEntity) @InjectObjectMetadataRepository(WorkspaceMemberWorkspaceEntity)
private readonly workspaceMemberRepository: WorkspaceMemberRepository, private readonly workspaceMemberRepository: WorkspaceMemberRepository,
private readonly eventEmitter: EventEmitter2, private readonly workspaceEventEmitter: WorkspaceEventEmitter,
@InjectRepository(ObjectMetadataEntity, 'metadata') @InjectRepository(ObjectMetadataEntity, 'metadata')
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>, private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager, private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
@ -191,18 +191,21 @@ export class CreateCompanyAndContactService {
source, source,
); );
for (const createdPerson of createdPeople) { this.workspaceEventEmitter.emit(
this.eventEmitter.emit('person.created', { 'person.created',
name: 'person.created', createdPeople.map(
workspaceId, (createdPerson) =>
// FixMe: TypeORM typing issue... id is always returned when using save ({
recordId: createdPerson.id as string, // FixMe: TypeORM typing issue... id is always returned when using save
objectMetadata, recordId: createdPerson.id as string,
properties: { objectMetadata,
after: createdPerson, properties: {
}, after: createdPerson,
} satisfies ObjectRecordCreateEvent<any>); },
} }) satisfies ObjectRecordCreateEvent<any>,
),
workspaceId,
);
} }
} }
} }

View File

@ -1,10 +1,10 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Any } from 'typeorm'; import { Any } from 'typeorm';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory'; import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager'; 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 { 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 { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity'; import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
@ -17,7 +17,7 @@ export class MatchParticipantService<
| MessageParticipantWorkspaceEntity, | MessageParticipantWorkspaceEntity,
> { > {
constructor( constructor(
private readonly eventEmitter: EventEmitter2, private readonly workspaceEventEmitter: WorkspaceEventEmitter,
private readonly twentyORMManager: TwentyORMManager, private readonly twentyORMManager: TwentyORMManager,
private readonly scopedWorkspaceContextFactory: ScopedWorkspaceContextFactory, private readonly scopedWorkspaceContextFactory: ScopedWorkspaceContextFactory,
) {} ) {}
@ -46,6 +46,10 @@ export class MatchParticipantService<
const workspaceId = this.scopedWorkspaceContextFactory.create().workspaceId; const workspaceId = this.scopedWorkspaceContextFactory.create().workspaceId;
if (!workspaceId) {
throw new Error('Workspace ID is required');
}
const participantIds = participants.map((participant) => participant.id); const participantIds = participants.map((participant) => participant.id);
const uniqueParticipantsHandles = [ const uniqueParticipantsHandles = [
...new Set(participants.map((participant) => participant.handle)), ...new Set(participants.map((participant) => participant.handle)),
@ -109,11 +113,16 @@ export class MatchParticipantService<
transactionManager, transactionManager,
); );
this.eventEmitter.emit(`${objectMetadataName}.matched`, { this.workspaceEventEmitter.emit(
`${objectMetadataName}.matched`,
[
{
workspaceMemberId: null,
participants: matchedParticipants,
},
],
workspaceId, workspaceId,
workspaceMemberId: null, );
participants: matchedParticipants,
});
} }
public async matchParticipantsAfterPersonOrWorkspaceMemberCreation( public async matchParticipantsAfterPersonOrWorkspaceMemberCreation(
@ -127,6 +136,10 @@ export class MatchParticipantService<
const workspaceId = this.scopedWorkspaceContextFactory.create().workspaceId; const workspaceId = this.scopedWorkspaceContextFactory.create().workspaceId;
if (!workspaceId) {
throw new Error('Workspace ID is required');
}
const participantsToUpdate = await participantRepository.find({ const participantsToUpdate = await participantRepository.find({
where: { where: {
handle, 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, workspaceId,
name: `${objectMetadataName}.matched`, );
workspaceMemberId: null,
participants: updatedParticipants,
});
} }
if (workspaceMemberId) { 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 { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager'; 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 { BlocklistWorkspaceEntity } from 'src/modules/blocklist/standard-objects/blocklist.workspace-entity';
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository'; import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity'; import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
@ -32,90 +33,109 @@ export class MessagingBlocklistListener {
@OnEvent('blocklist.created') @OnEvent('blocklist.created')
async handleCreatedEvent( async handleCreatedEvent(
payload: ObjectRecordCreateEvent<BlocklistWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<BlocklistWorkspaceEntity>
>,
) { ) {
await this.messageQueueService.add<BlocklistItemDeleteMessagesJobData>( await Promise.all(
BlocklistItemDeleteMessagesJob.name, payload.events.map((eventPayload) =>
{ // TODO: modify to pass an array of blocklist items
workspaceId: payload.workspaceId, this.messageQueueService.add<BlocklistItemDeleteMessagesJobData>(
blocklistItemId: payload.recordId, BlocklistItemDeleteMessagesJob.name,
}, {
workspaceId: payload.workspaceId,
blocklistItemId: eventPayload.recordId,
},
),
),
); );
} }
@OnEvent('blocklist.deleted') @OnEvent('blocklist.deleted')
async handleDeletedEvent( async handleDeletedEvent(
payload: ObjectRecordDeleteEvent<BlocklistWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<BlocklistWorkspaceEntity>
>,
) { ) {
const workspaceMemberId = payload.properties.before.workspaceMember.id;
const workspaceId = payload.workspaceId; const workspaceId = payload.workspaceId;
const connectedAccount = for (const eventPayload of payload.events) {
await this.connectedAccountRepository.getAllByWorkspaceMemberId( const workspaceMemberId =
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, 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') @OnEvent('blocklist.updated')
async handleUpdatedEvent( async handleUpdatedEvent(
payload: ObjectRecordUpdateEvent<BlocklistWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<BlocklistWorkspaceEntity>
>,
) { ) {
const workspaceMemberId = payload.properties.before.workspaceMember.id;
const workspaceId = payload.workspaceId; const workspaceId = payload.workspaceId;
await this.messageQueueService.add<BlocklistItemDeleteMessagesJobData>( for (const eventPayload of payload.events) {
BlocklistItemDeleteMessagesJob.name, const workspaceMemberId =
{ eventPayload.properties.before.workspaceMember.id;
workspaceId,
blocklistItemId: payload.recordId,
},
);
const connectedAccount = await this.messageQueueService.add<BlocklistItemDeleteMessagesJobData>(
await this.connectedAccountRepository.getAllByWorkspaceMemberId( BlocklistItemDeleteMessagesJob.name,
workspaceMemberId, {
workspaceId, workspaceId,
blocklistItemId: eventPayload.recordId,
},
); );
if (!connectedAccount || connectedAccount.length === 0) { const connectedAccount =
return; 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 { OnEvent } from '@nestjs/event-emitter';
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event'; 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 { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; 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 { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
import { import {
MessagingConnectedAccountDeletionCleanupJob, MessagingConnectedAccountDeletionCleanupJob,
MessagingConnectedAccountDeletionCleanupJobData, MessagingConnectedAccountDeletionCleanupJobData,
} from 'src/modules/messaging/message-cleaner/jobs/messaging-connected-account-deletion-cleanup.job'; } 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() @Injectable()
export class MessagingMessageCleanerConnectedAccountListener { export class MessagingMessageCleanerConnectedAccountListener {
constructor( constructor(
@InjectMessageQueue(MessageQueue.messagingQueue) @InjectMessageQueue(MessageQueue.messagingQueue)
private readonly messageQueueService: MessageQueueService, private readonly messageQueueService: MessageQueueService,
@InjectMessageQueue(MessageQueue.calendarQueue)
private readonly calendarQueueService: MessageQueueService,
) {} ) {}
@OnEvent('connectedAccount.deleted') @OnEvent('connectedAccount.deleted')
async handleDeletedEvent( async handleDeletedEvent(
payload: ObjectRecordDeleteEvent<ConnectedAccountWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<ConnectedAccountWorkspaceEntity>
>,
) { ) {
await this.messageQueueService.add<MessagingConnectedAccountDeletionCleanupJobData>( await Promise.all(
MessagingConnectedAccountDeletionCleanupJob.name, payload.events.map((eventPayload) =>
{ this.messageQueueService.add<MessagingConnectedAccountDeletionCleanupJobData>(
workspaceId: payload.workspaceId, MessagingConnectedAccountDeletionCleanupJob.name,
connectedAccountId: payload.recordId, {
}, workspaceId: payload.workspaceId,
); connectedAccountId: eventPayload.recordId,
},
await this.calendarQueueService.add<DeleteConnectedAccountAssociatedCalendarDataJobData>( ),
DeleteConnectedAccountAssociatedCalendarDataJob.name, ),
{
workspaceId: payload.workspaceId,
connectedAccountId: payload.recordId,
},
); );
} }
} }

View File

@ -1,19 +1,11 @@
import { Module } from '@nestjs/common'; 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 { 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 { 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'; import { MessagingMessageCleanerService } from 'src/modules/messaging/message-cleaner/services/messaging-message-cleaner.service';
@Module({ @Module({
imports: [ imports: [],
TwentyORMModule.forFeature([
MessageWorkspaceEntity,
MessageThreadWorkspaceEntity,
]),
],
providers: [ providers: [
MessagingMessageCleanerService, MessagingMessageCleanerService,
MessagingConnectedAccountDeletionCleanupJob, MessagingConnectedAccountDeletionCleanupJob,

View File

@ -2,9 +2,10 @@ import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter'; import { OnEvent } from '@nestjs/event-emitter';
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event'; 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 { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; 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 { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
import { import {
MessagingCleanCacheJob, MessagingCleanCacheJob,
@ -20,14 +21,20 @@ export class MessagingMessageImportManagerMessageChannelListener {
@OnEvent('messageChannel.deleted') @OnEvent('messageChannel.deleted')
async handleDeletedEvent( async handleDeletedEvent(
payload: ObjectRecordDeleteEvent<MessageChannelWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<MessageChannelWorkspaceEntity>
>,
) { ) {
await this.messageQueueService.add<MessagingCleanCacheJobData>( await Promise.all(
MessagingCleanCacheJob.name, payload.events.map((eventPayload) =>
{ this.messageQueueService.add<MessagingCleanCacheJobData>(
workspaceId: payload.workspaceId, MessagingCleanCacheJob.name,
messageChannelId: payload.recordId, {
}, 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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { import {
MessageParticipantMatchParticipantJobData,
MessageParticipantMatchParticipantJob, MessageParticipantMatchParticipantJob,
MessageParticipantMatchParticipantJobData,
} from 'src/modules/messaging/message-participant-manager/jobs/message-participant-match-participant.job'; } from 'src/modules/messaging/message-participant-manager/jobs/message-participant-match-participant.job';
import { import {
MessageParticipantUnmatchParticipantJobData,
MessageParticipantUnmatchParticipantJob, MessageParticipantUnmatchParticipantJob,
MessageParticipantUnmatchParticipantJobData,
} from 'src/modules/messaging/message-participant-manager/jobs/message-participant-unmatch-participant.job'; } from 'src/modules/messaging/message-participant-manager/jobs/message-participant-unmatch-participant.job';
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity'; import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
@ -26,49 +27,57 @@ export class MessageParticipantPersonListener {
@OnEvent('person.created') @OnEvent('person.created')
async handleCreatedEvent( async handleCreatedEvent(
payload: ObjectRecordCreateEvent<PersonWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<PersonWorkspaceEntity>
>,
) { ) {
if (payload.properties.after.email === null) { for (const eventPayload of payload.events) {
return; 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,
},
);
}
@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,
},
);
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>( await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
MessageParticipantMatchParticipantJob.name, MessageParticipantMatchParticipantJob.name,
{ {
workspaceId: payload.workspaceId, workspaceId: payload.workspaceId,
email: payload.properties.after.email, email: eventPayload.properties.after.email,
personId: payload.recordId, 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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
import { import {
MessageParticipantMatchParticipantJob, MessageParticipantMatchParticipantJob,
MessageParticipantMatchParticipantJobData, MessageParticipantMatchParticipantJobData,
@ -35,7 +36,9 @@ export class MessageParticipantWorkspaceMemberListener {
@OnEvent('workspaceMember.created') @OnEvent('workspaceMember.created')
async handleCreatedEvent( async handleCreatedEvent(
payload: ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>, payload: WorkspaceEventBatch<
ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>
>,
) { ) {
const workspace = await this.workspaceRepository.findOneBy({ const workspace = await this.workspaceRepository.findOneBy({
id: payload.workspaceId, id: payload.workspaceId,
@ -48,47 +51,53 @@ export class MessageParticipantWorkspaceMemberListener {
return; return;
} }
if (payload.properties.after.userEmail === null) { for (const eventPayload of payload.events) {
return; if (eventPayload.properties.after.userEmail === null) {
} continue;
}
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,
},
);
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>( await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
MessageParticipantMatchParticipantJob.name, MessageParticipantMatchParticipantJob.name,
{ {
workspaceId: payload.workspaceId, workspaceId: payload.workspaceId,
email: payload.properties.after.userEmail, email: eventPayload.properties.after.userEmail,
workspaceMemberId: payload.recordId, 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 { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; 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 { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
import { TimelineActivityRepository } from 'src/modules/timeline/repositiories/timeline-activity.repository'; import { TimelineActivityRepository } from 'src/modules/timeline/repositiories/timeline-activity.repository';
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity'; import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
@ -22,50 +23,54 @@ export class MessageParticipantListener {
) {} ) {}
@OnEvent('messageParticipant.matched') @OnEvent('messageParticipant.matched')
public async handleMessageParticipantMatched(payload: { public async handleMessageParticipantMatched(
workspaceId: string; payload: WorkspaceEventBatch<{
workspaceMemberId: string; workspaceMemberId: string;
participants: MessageParticipantWorkspaceEntity[]; participants: MessageParticipantWorkspaceEntity[];
}): Promise<void> { }>,
const messageParticipants = payload.participants ?? []; ): 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( const dataSourceSchema = this.workspaceDataSourceService.getSchemaName(
payload.workspaceId, payload.workspaceId,
); );
const messageObjectMetadata = const messageObjectMetadata =
await this.objectMetadataRepository.findOneOrFail({ await this.objectMetadataRepository.findOneOrFail({
where: { where: {
nameSingular: 'message', 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, workspaceId: payload.workspaceId,
}, linkedObjectMetadataId: messageObjectMetadata.id,
}); linkedRecordId: participant.messageId,
linkedRecordCachedName: '',
const messageParticipantsWithPersonId = messageParticipants.filter( })),
(participant) => participant.personId, payload.workspaceId,
); );
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: 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 { 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 { 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 { AuditLogRepository } from 'src/modules/timeline/repositiories/audit-log.repository';
import { AuditLogWorkspaceEntity } from 'src/modules/timeline/standard-objects/audit-log.workspace-entity'; import { AuditLogWorkspaceEntity } from 'src/modules/timeline/standard-objects/audit-log.workspace-entity';
import { WorkspaceMemberRepository } from 'src/modules/workspace-member/repositories/workspace-member.repository'; 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 { 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) @Processor(MessageQueue.entityEventsToDbQueue)
export class CreateAuditLogFromInternalEvent { export class CreateAuditLogFromInternalEvent {
@ -18,33 +19,37 @@ export class CreateAuditLogFromInternalEvent {
) {} ) {}
@Process(CreateAuditLogFromInternalEvent.name) @Process(CreateAuditLogFromInternalEvent.name)
async handle(data: ObjectRecordBaseEvent): Promise<void> { async handle(
let workspaceMemberId: string | null = null; data: WorkspaceEventBatch<ObjectRecordBaseEvent>,
): Promise<void> {
for (const eventData of data.events) {
let workspaceMemberId: string | null = null;
if (data.userId) { if (eventData.userId) {
const workspaceMember = await this.workspaceMemberService.getByIdOrFail( const workspaceMember = await this.workspaceMemberService.getByIdOrFail(
data.userId, 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, 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 { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; 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 { TimelineActivityService } from 'src/modules/timeline/services/timeline-activity.service';
import { WorkspaceMemberRepository } from 'src/modules/workspace-member/repositories/workspace-member.repository'; 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 { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@ -16,33 +17,41 @@ export class UpsertTimelineActivityFromInternalEvent {
) {} ) {}
@Process(UpsertTimelineActivityFromInternalEvent.name) @Process(UpsertTimelineActivityFromInternalEvent.name)
async handle(data: ObjectRecordBaseEvent): Promise<void> { async handle(
if (data.userId) { data: WorkspaceEventBatch<ObjectRecordBaseEvent>,
const workspaceMember = await this.workspaceMemberService.getByIdOrFail( ): Promise<void> {
data.userId, for (const eventData of data.events) {
data.workspaceId, 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 { 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 { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { TimelineActivityRepository } from 'src/modules/timeline/repositiories/timeline-activity.repository'; import { TimelineActivityRepository } from 'src/modules/timeline/repositiories/timeline-activity.repository';
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity'; import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
type TransformedEvent = ObjectRecordBaseEvent & { type TransformedEvent = ObjectRecordBaseEventWithNameAndWorkspaceId & {
objectName?: string; objectName?: string;
linkedRecordCachedName?: string; linkedRecordCachedName?: string;
linkedRecordId?: string; linkedRecordId?: string;
@ -26,7 +26,7 @@ export class TimelineActivityService {
task: 'taskTarget', task: 'taskTarget',
}; };
async upsertEvent(event: ObjectRecordBaseEvent) { async upsertEvent(event: ObjectRecordBaseEventWithNameAndWorkspaceId) {
const events = await this.transformEvent(event); const events = await this.transformEvent(event);
if (!events || events.length === 0) return; if (!events || events.length === 0) return;
@ -47,7 +47,7 @@ export class TimelineActivityService {
} }
private async transformEvent( private async transformEvent(
event: ObjectRecordBaseEvent, event: ObjectRecordBaseEventWithNameAndWorkspaceId,
): Promise<TransformedEvent[]> { ): Promise<TransformedEvent[]> {
if (['note', 'task'].includes(event.objectMetadata.nameSingular)) { if (['note', 'task'].includes(event.objectMetadata.nameSingular)) {
const linkedObjects = await this.handleLinkedObjects(event); const linkedObjects = await this.handleLinkedObjects(event);
@ -69,7 +69,9 @@ export class TimelineActivityService {
return [event]; return [event];
} }
private async handleLinkedObjects(event: ObjectRecordBaseEvent) { private async handleLinkedObjects(
event: ObjectRecordBaseEventWithNameAndWorkspaceId,
) {
const dataSourceSchema = this.workspaceDataSourceService.getSchemaName( const dataSourceSchema = this.workspaceDataSourceService.getSchemaName(
event.workspaceId, event.workspaceId,
); );
@ -92,7 +94,7 @@ export class TimelineActivityService {
} }
private async processActivity( private async processActivity(
event: ObjectRecordBaseEvent, event: ObjectRecordBaseEventWithNameAndWorkspaceId,
dataSourceSchema: string, dataSourceSchema: string,
activityType: string, activityType: string,
) { ) {
@ -145,7 +147,7 @@ export class TimelineActivityService {
} }
private async processActivityTarget( private async processActivityTarget(
event: ObjectRecordBaseEvent, event: ObjectRecordBaseEventWithNameAndWorkspaceId,
dataSourceSchema: string, dataSourceSchema: string,
activityType: 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 { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; 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 { WorkflowEventListenerWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-event-listener.workspace-entity';
import { import {
WorkflowEventTriggerJob, WorkflowEventTriggerJob,
@ -28,25 +29,32 @@ export class DatabaseEventTriggerListener {
) {} ) {}
@OnEvent('*.created') @OnEvent('*.created')
async handleObjectRecordCreateEvent(payload: ObjectRecordCreateEvent<any>) { async handleObjectRecordCreateEvent(
payload: WorkspaceEventBatch<ObjectRecordCreateEvent<any>>,
) {
await this.handleEvent(payload); await this.handleEvent(payload);
} }
@OnEvent('*.updated') @OnEvent('*.updated')
async handleObjectRecordUpdateEvent(payload: ObjectRecordUpdateEvent<any>) { async handleObjectRecordUpdateEvent(
payload: WorkspaceEventBatch<ObjectRecordUpdateEvent<any>>,
) {
await this.handleEvent(payload); await this.handleEvent(payload);
} }
@OnEvent('*.deleted') @OnEvent('*.deleted')
async handleObjectRecordDeleteEvent(payload: ObjectRecordDeleteEvent<any>) { async handleObjectRecordDeleteEvent(
payload: WorkspaceEventBatch<ObjectRecordDeleteEvent<any>>,
) {
await this.handleEvent(payload); await this.handleEvent(payload);
} }
private async handleEvent( private async handleEvent(
payload: payload: WorkspaceEventBatch<
| ObjectRecordCreateEvent<any> | ObjectRecordCreateEvent<any>
| ObjectRecordUpdateEvent<any> | ObjectRecordUpdateEvent<any>
| ObjectRecordDeleteEvent<any>, | ObjectRecordDeleteEvent<any>
>,
) { ) {
const workspaceId = payload.workspaceId; const workspaceId = payload.workspaceId;
const eventName = payload.name; const eventName = payload.name;
@ -84,15 +92,17 @@ export class DatabaseEventTriggerListener {
}); });
for (const eventListener of eventListeners) { for (const eventListener of eventListeners) {
this.messageQueueService.add<WorkflowEventTriggerJobData>( for (const eventPayload of payload.events) {
WorkflowEventTriggerJob.name, this.messageQueueService.add<WorkflowEventTriggerJobData>(
{ WorkflowEventTriggerJob.name,
workspaceId, {
workflowId: eventListener.workflowId, workspaceId,
payload, workflowId: eventListener.workflowId,
}, payload: eventPayload,
{ retryLimit: 3 }, },
); { retryLimit: 3 },
);
}
} }
} }
} }

View File

@ -1,15 +1,17 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { JobsModule } from 'src/engine/integrations/message-queue/jobs.module';
import { IntegrationsModule } from 'src/engine/integrations/integrations.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 { 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({ @Module({
imports: [ imports: [
TwentyORMModule.register({}), TwentyORMModule.register({}),
IntegrationsModule, IntegrationsModule,
MessageQueueModule.registerExplorer(), MessageQueueModule.registerExplorer(),
WorkspaceEventEmitterModule,
JobsModule, JobsModule,
], ],
}) })