message channel change 5 (#15964)

This commit is contained in:
neo773
2025-11-21 15:35:12 +05:30
committed by GitHub
parent 31ca2a46c5
commit dc3e30b115
7 changed files with 0 additions and 319 deletions
@@ -8,7 +8,6 @@ import { WorkspaceModule } from 'src/engine/core-modules/workspace/workspace.mod
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
import { CalendarCreateCompanyAndPersonAfterSyncJob } from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-create-company-and-contact-after-sync.job';
import { CalendarEventParticipantMatchParticipantJob } from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-match-participant.job';
import { CalendarEventParticipantPersonListener } from 'src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant-person.listener';
import { CalendarEventParticipantWorkspaceMemberListener } from 'src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant-workspace-member.listener';
@@ -28,7 +27,6 @@ import { MatchParticipantModule } from 'src/modules/match-participant/match-part
],
providers: [
CalendarEventParticipantService,
CalendarCreateCompanyAndPersonAfterSyncJob,
CalendarEventParticipantMatchParticipantJob,
CalendarEventParticipantListener,
CalendarEventParticipantPersonListener,
@@ -1,97 +0,0 @@
import { Scope } from '@nestjs/common';
import { IsNull } from 'typeorm';
import { FieldActorSource } from 'twenty-shared/types';
import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';
import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { type CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
import { type CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
import { CreateCompanyAndPersonService } from 'src/modules/contact-creation-manager/services/create-company-and-contact.service';
export type CalendarCreateCompanyAndPersonAfterSyncJobData = {
workspaceId: string;
calendarChannelId: string;
};
@Processor({
queueName: MessageQueue.calendarQueue,
scope: Scope.REQUEST,
})
export class CalendarCreateCompanyAndPersonAfterSyncJob {
constructor(
private readonly twentyORMManager: TwentyORMManager,
private readonly createCompanyAndPersonService: CreateCompanyAndPersonService,
) {}
@Process(CalendarCreateCompanyAndPersonAfterSyncJob.name)
async handle(
data: CalendarCreateCompanyAndPersonAfterSyncJobData,
): Promise<void> {
const { workspaceId, calendarChannelId } = data;
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
const calendarChannel = await calendarChannelRepository.findOne({
where: {
id: calendarChannelId,
},
relations: ['connectedAccount.accountOwner'],
});
if (!calendarChannel) {
throw new Error(
`Calendar channel with id ${calendarChannelId} not found in workspace ${workspaceId}`,
);
}
const { handle, isContactAutoCreationEnabled, connectedAccount } =
calendarChannel;
if (!isContactAutoCreationEnabled || !handle) {
return;
}
if (!connectedAccount) {
throw new Error(
`Connected account not found in workspace ${workspaceId}`,
);
}
const calendarEventParticipantRepository =
await this.twentyORMManager.getRepository<CalendarEventParticipantWorkspaceEntity>(
'calendarEventParticipant',
);
const calendarEventParticipantsWithoutPersonIdAndWorkspaceMemberId =
await calendarEventParticipantRepository.find({
where: {
calendarEvent: {
calendarChannelEventAssociations: {
calendarChannelId,
},
calendarEventParticipants: {
person: IsNull(),
workspaceMember: IsNull(),
},
},
},
relations: [
'calendarEvent.calendarChannelEventAssociations',
'calendarEvent.calendarEventParticipants',
],
});
await this.createCompanyAndPersonService.createCompaniesAndPeopleAndUpdateParticipants(
connectedAccount,
calendarEventParticipantsWithoutPersonIdAndWorkspaceMemberId,
workspaceId,
FieldActorSource.CALENDAR,
);
}
}
@@ -5,8 +5,6 @@ import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
import { AutoCompaniesAndContactsCreationCalendarChannelListener } from 'src/modules/contact-creation-manager/listeners/auto-companies-and-contacts-creation-calendar-channel.listener';
import { AutoCompaniesAndContactsCreationMessageChannelListener } from 'src/modules/contact-creation-manager/listeners/auto-companies-and-contacts-creation-message-channel.listener';
import { CreateCompanyAndPersonService } from 'src/modules/contact-creation-manager/services/create-company-and-contact.service';
import { CreateCompanyService } from 'src/modules/contact-creation-manager/services/create-company.service';
import { CreatePersonService } from 'src/modules/contact-creation-manager/services/create-person.service';
@@ -21,8 +19,6 @@ import { CreatePersonService } from 'src/modules/contact-creation-manager/servic
CreateCompanyService,
CreatePersonService,
CreateCompanyAndPersonService,
AutoCompaniesAndContactsCreationMessageChannelListener,
AutoCompaniesAndContactsCreationCalendarChannelListener,
],
exports: [CreateCompanyAndPersonService],
})
@@ -1,50 +0,0 @@
import { Injectable } from '@nestjs/common';
import { OnDatabaseBatchEvent } from 'src/engine/api/graphql/graphql-query-runner/decorators/on-database-batch-event.decorator';
import { DatabaseEventAction } from 'src/engine/api/graphql/graphql-query-runner/enums/database-event-action';
import { type ObjectRecordUpdateEvent } from 'src/engine/core-modules/event-emitter/types/object-record-update.event';
import { objectRecordChangedProperties } from 'src/engine/core-modules/event-emitter/utils/object-record-changed-properties.util';
import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/types/workspace-event-batch.type';
import {
CalendarCreateCompanyAndPersonAfterSyncJob,
CalendarCreateCompanyAndPersonAfterSyncJobData,
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-create-company-and-contact-after-sync.job';
import { type MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
@Injectable()
export class AutoCompaniesAndContactsCreationCalendarChannelListener {
constructor(
@InjectMessageQueue(MessageQueue.calendarQueue)
private readonly messageQueueService: MessageQueueService,
) {}
@OnDatabaseBatchEvent('calendarChannel', DatabaseEventAction.UPDATED)
async handleUpdatedEvent(
payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<MessageChannelWorkspaceEntity>
>,
) {
await Promise.all(
payload.events.map((eventPayload) => {
if (
objectRecordChangedProperties(
eventPayload.properties.before,
eventPayload.properties.after,
).includes('isContactAutoCreationEnabled') &&
eventPayload.properties.after.isContactAutoCreationEnabled
) {
return this.messageQueueService.add<CalendarCreateCompanyAndPersonAfterSyncJobData>(
CalendarCreateCompanyAndPersonAfterSyncJob.name,
{
workspaceId: payload.workspaceId,
calendarChannelId: eventPayload.recordId,
},
);
}
}),
);
}
}
@@ -1,50 +0,0 @@
import { Injectable } from '@nestjs/common';
import { type ObjectRecordUpdateEvent } from 'src/engine/core-modules/event-emitter/types/object-record-update.event';
import { objectRecordChangedProperties } from 'src/engine/core-modules/event-emitter/utils/object-record-changed-properties.util';
import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/types/workspace-event-batch.type';
import { type MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
import {
MessagingCreateCompanyAndContactAfterSyncJob,
type MessagingCreateCompanyAndContactAfterSyncJobData,
} from 'src/modules/messaging/message-participant-manager/jobs/messaging-create-company-and-contact-after-sync.job';
import { OnDatabaseBatchEvent } from 'src/engine/api/graphql/graphql-query-runner/decorators/on-database-batch-event.decorator';
import { DatabaseEventAction } from 'src/engine/api/graphql/graphql-query-runner/enums/database-event-action';
@Injectable()
export class AutoCompaniesAndContactsCreationMessageChannelListener {
constructor(
@InjectMessageQueue(MessageQueue.contactCreationQueue)
private readonly messageQueueService: MessageQueueService,
) {}
@OnDatabaseBatchEvent('messageChannel', DatabaseEventAction.UPDATED)
async handleUpdatedEvent(
payload: WorkspaceEventBatch<
ObjectRecordUpdateEvent<MessageChannelWorkspaceEntity>
>,
) {
await Promise.all(
payload.events.map((eventPayload) => {
if (
objectRecordChangedProperties(
eventPayload.properties.before,
eventPayload.properties.after,
).includes('isContactAutoCreationEnabled') &&
eventPayload.properties.after.isContactAutoCreationEnabled
) {
return this.messageQueueService.add<MessagingCreateCompanyAndContactAfterSyncJobData>(
MessagingCreateCompanyAndContactAfterSyncJob.name,
{
workspaceId: payload.workspaceId,
messageChannelId: eventPayload.recordId,
},
);
}
}),
);
}
}
@@ -1,114 +0,0 @@
import { Logger } from '@nestjs/common';
import { Any, IsNull } from 'typeorm';
import { FieldActorSource } from 'twenty-shared/types';
import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';
import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { type ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
import { CreateCompanyAndPersonService } from 'src/modules/contact-creation-manager/services/create-company-and-contact.service';
import { MessageDirection } from 'src/modules/messaging/common/enums/message-direction.enum';
import {
MessageChannelContactAutoCreationPolicy,
type MessageChannelWorkspaceEntity,
} from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
import { type MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
export type MessagingCreateCompanyAndContactAfterSyncJobData = {
workspaceId: string;
messageChannelId: string;
};
@Processor(MessageQueue.messagingQueue)
export class MessagingCreateCompanyAndContactAfterSyncJob {
private readonly logger = new Logger(
MessagingCreateCompanyAndContactAfterSyncJob.name,
);
constructor(
private readonly createCompanyAndPersonService: CreateCompanyAndPersonService,
private readonly twentyORMManager: TwentyORMManager,
) {}
@Process(MessagingCreateCompanyAndContactAfterSyncJob.name)
async handle(
data: MessagingCreateCompanyAndContactAfterSyncJobData,
): Promise<void> {
this.logger.log(
`create people and companies after sync for workspace ${data.workspaceId} and messageChannel ${data.messageChannelId}`,
);
const { workspaceId, messageChannelId } = data;
const messageChannelRepository =
await this.twentyORMManager.getRepository<MessageChannelWorkspaceEntity>(
'messageChannel',
);
const messageChannel = await messageChannelRepository.findOneOrFail({
where: {
id: messageChannelId,
},
});
const { contactAutoCreationPolicy, connectedAccountId } = messageChannel;
if (
contactAutoCreationPolicy === MessageChannelContactAutoCreationPolicy.NONE
) {
return;
}
const connectedAccountRepository =
await this.twentyORMManager.getRepository<ConnectedAccountWorkspaceEntity>(
'connectedAccount',
);
const connectedAccount = await connectedAccountRepository.findOne({
where: {
id: connectedAccountId,
},
});
if (!connectedAccount) {
throw new Error(
`Connected account with id ${connectedAccountId} not found in workspace ${workspaceId}`,
);
}
const messageParticipantRepository =
await this.twentyORMManager.getRepository<MessageParticipantWorkspaceEntity>(
'messageParticipant',
);
const directionFilter =
contactAutoCreationPolicy ===
MessageChannelContactAutoCreationPolicy.SENT_AND_RECEIVED
? Any([MessageDirection.INCOMING, MessageDirection.OUTGOING])
: MessageDirection.OUTGOING;
const contactsToCreate = await messageParticipantRepository.find({
where: {
message: {
messageChannelMessageAssociations: {
messageChannelId,
direction: directionFilter,
},
},
personId: IsNull(),
workspaceMemberId: IsNull(),
},
});
await this.createCompanyAndPersonService.createCompaniesAndPeopleAndUpdateParticipants(
connectedAccount,
contactsToCreate,
workspaceId,
FieldActorSource.EMAIL,
);
this.logger.log(
`create contacts and companies after sync for workspace ${data.workspaceId} and messageChannel ${data.messageChannelId} done`,
);
}
}
@@ -11,7 +11,6 @@ import { ContactCreationManagerModule } from 'src/modules/contact-creation-manag
import { MatchParticipantModule } from 'src/modules/match-participant/match-participant.module';
import { MessagingCommonModule } from 'src/modules/messaging/common/messaging-common.module';
import { MessageParticipantMatchParticipantJob } from 'src/modules/messaging/message-participant-manager/jobs/message-participant-match-participant.job';
import { MessagingCreateCompanyAndContactAfterSyncJob } from 'src/modules/messaging/message-participant-manager/jobs/messaging-create-company-and-contact-after-sync.job';
import { MessageParticipantPersonListener } from 'src/modules/messaging/message-participant-manager/listeners/message-participant-person.listener';
import { MessageParticipantWorkspaceMemberListener } from 'src/modules/messaging/message-participant-manager/listeners/message-participant-workspace-member.listener';
import { MessageParticipantListener } from 'src/modules/messaging/message-participant-manager/listeners/message-participant.listener';
@@ -37,7 +36,6 @@ import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-o
providers: [
MessagingMessageParticipantService,
MessageParticipantMatchParticipantJob,
MessagingCreateCompanyAndContactAfterSyncJob,
MessageParticipantListener,
MessageParticipantPersonListener,
MessageParticipantWorkspaceMemberListener,