6255 move services from messaging common module into the correct module and refactor them (#6409)

Closes #6255 

- Move files from `messaging/common` into the correct module
- Remove common module between calendar and messaging
`calendar-messaging-participant-manager`
- Update and fix massaging and calendar participant matching
- Create `MatchParticipantModule`

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
bosiraphael
2024-07-27 12:29:02 +02:00
committed by GitHub
parent 3060eb4e1e
commit d0db3b765f
42 changed files with 398 additions and 540 deletions

View File

@ -19,7 +19,6 @@ import { CalendarEventsImportService } from 'src/modules/calendar/calendar-event
import { CalendarGetCalendarEventsService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-get-events.service';
import { CalendarSaveEventsService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-save-events.service';
import { CalendarEventParticipantManagerModule } from 'src/modules/calendar/calendar-event-participant-manager/calendar-event-participant-manager.module';
import { CalendarCommonModule } from 'src/modules/calendar/common/calendar-common.module';
import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
@ -51,7 +50,6 @@ import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/sta
GoogleCalendarDriverModule,
BillingModule,
RefreshAccessTokenManagerModule,
CalendarCommonModule,
CalendarEventParticipantManagerModule,
],
providers: [

View File

@ -1,4 +1,4 @@
import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant-manager/utils/is-email-blocklisted.util';
import { isEmailBlocklisted } from 'src/modules/blocklist/utils/is-email-blocklisted.util';
import { CalendarEventWithParticipants } from 'src/modules/calendar/common/types/calendar-event';
export const filterOutBlocklistedEvents = (

View File

@ -7,7 +7,6 @@ import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadat
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant-manager/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
import { CalendarCreateCompanyAndContactAfterSyncJob } 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 { CalendarEventParticipantUnmatchParticipantJob } from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-unmatch-participant.job';
@ -15,9 +14,9 @@ import { CalendarEventParticipantPersonListener } from 'src/modules/calendar/cal
import { CalendarEventParticipantWorkspaceMemberListener } from 'src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant-workspace-member.listener';
import { CalendarEventParticipantListener } from 'src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant.listener';
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
import { CalendarCommonModule } from 'src/modules/calendar/common/calendar-common.module';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
import { ContactCreationManagerModule } from 'src/modules/contact-creation-manager/contact-creation-manager.module';
import { MatchParticipantModule } from 'src/modules/match-participant/match-participant.module';
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
@Module({
@ -31,7 +30,7 @@ import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/perso
'metadata',
),
ContactCreationManagerModule,
CalendarCommonModule,
MatchParticipantModule,
],
providers: [
CalendarEventParticipantService,
@ -41,7 +40,6 @@ import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/perso
CalendarEventParticipantListener,
CalendarEventParticipantPersonListener,
CalendarEventParticipantWorkspaceMemberListener,
AddPersonIdAndWorkspaceMemberIdService,
],
exports: [CalendarEventParticipantService],
})

View File

@ -4,7 +4,8 @@ import { WorkspaceService } from 'src/engine/core-modules/workspace/services/wor
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 { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
import { MatchParticipantService } from 'src/modules/match-participant/match-participant.service';
export type CalendarEventParticipantMatchParticipantJobData = {
workspaceId: string;
@ -19,8 +20,8 @@ export type CalendarEventParticipantMatchParticipantJobData = {
})
export class CalendarEventParticipantMatchParticipantJob {
constructor(
private readonly calendarEventParticipantService: CalendarEventParticipantService,
private readonly workspaceService: WorkspaceService,
private readonly matchParticipantService: MatchParticipantService<CalendarEventParticipantWorkspaceEntity>,
) {}
@Process(CalendarEventParticipantMatchParticipantJob.name)
@ -33,9 +34,9 @@ export class CalendarEventParticipantMatchParticipantJob {
return;
}
await this.calendarEventParticipantService.matchCalendarEventParticipants(
workspaceId,
await this.matchParticipantService.matchParticipantsAfterPersonOrWorkspaceMemberCreation(
email,
'calendarEventParticipant',
personId,
workspaceMemberId,
);

View File

@ -1,9 +1,10 @@
import { Scope } from '@nestjs/common';
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 { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
import { MatchParticipantService } from 'src/modules/match-participant/match-participant.service';
export type CalendarEventParticipantUnmatchParticipantJobData = {
workspaceId: string;
@ -18,18 +19,18 @@ export type CalendarEventParticipantUnmatchParticipantJobData = {
})
export class CalendarEventParticipantUnmatchParticipantJob {
constructor(
private readonly calendarEventParticipantService: CalendarEventParticipantService,
private readonly matchParticipantService: MatchParticipantService<CalendarEventParticipantWorkspaceEntity>,
) {}
@Process(CalendarEventParticipantUnmatchParticipantJob.name)
async handle(
data: CalendarEventParticipantUnmatchParticipantJobData,
): Promise<void> {
const { workspaceId, email, personId, workspaceMemberId } = data;
const { email, personId, workspaceMemberId } = data;
await this.calendarEventParticipantService.unmatchCalendarEventParticipants(
workspaceId,
await this.matchParticipantService.unmatchParticipants(
email,
'calendarEventParticipant',
personId,
workspaceMemberId,
);

View File

@ -7,9 +7,9 @@ import { Repository } from 'typeorm';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
import { TimelineActivityRepository } from 'src/modules/timeline/repositiories/timeline-activity.repository';
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
@Injectable()
export class CalendarEventParticipantListener {
@ -25,9 +25,9 @@ export class CalendarEventParticipantListener {
public async handleCalendarEventParticipantMatchedEvent(payload: {
workspaceId: string;
workspaceMemberId: string;
calendarEventParticipants: CalendarEventParticipantWorkspaceEntity[];
participants: CalendarEventParticipantWorkspaceEntity[];
}): Promise<void> {
const calendarEventParticipants = payload.calendarEventParticipants ?? [];
const calendarEventParticipants = payload.participants ?? [];
// TODO: move to a job?

View File

@ -1,5 +1,4 @@
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { isDefined } from 'class-validator';
import differenceWith from 'lodash.differencewith';
@ -8,12 +7,13 @@ import { Any } from 'typeorm';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
import { CalendarEventParticipantWithCalendarEventId } from 'src/modules/calendar/common/types/calendar-event';
import { MatchParticipantService } from 'src/modules/match-participant/match-participant.service';
@Injectable()
export class CalendarEventParticipantService {
constructor(
private readonly twentyORMManager: TwentyORMManager,
private readonly eventEmitter: EventEmitter2,
private readonly matchParticipantService: MatchParticipantService<CalendarEventParticipantWorkspaceEntity>,
) {}
public async upsertAndDeleteCalendarEventParticipants(
@ -103,103 +103,16 @@ export class CalendarEventParticipantService {
participantsToSave.push(...newCalendarEventParticipants);
await calendarEventParticipantRepository.save(
const savedParticipants = await calendarEventParticipantRepository.save(
participantsToSave,
{},
transactionManager,
);
}
public async matchCalendarEventParticipants(
workspaceId: string,
email: string,
personId?: string,
workspaceMemberId?: string,
) {
const calendarEventParticipantRepository =
await this.twentyORMManager.getRepository<CalendarEventParticipantWorkspaceEntity>(
'calendarEventParticipant',
);
const calendarEventParticipantsToUpdate =
await calendarEventParticipantRepository.find({
where: {
handle: email,
},
});
const calendarEventParticipantIdsToUpdate =
calendarEventParticipantsToUpdate.map((participant) => participant.id);
if (personId) {
await calendarEventParticipantRepository.update(
{
id: Any(calendarEventParticipantIdsToUpdate),
},
{
person: {
id: personId,
},
},
);
const updatedCalendarEventParticipants =
await calendarEventParticipantRepository.find({
where: {
id: Any(calendarEventParticipantIdsToUpdate),
},
});
this.eventEmitter.emit(`calendarEventParticipant.matched`, {
workspaceId,
workspaceMemberId: null,
calendarEventParticipants: updatedCalendarEventParticipants,
});
}
if (workspaceMemberId) {
await calendarEventParticipantRepository.update(
{
id: Any(calendarEventParticipantIdsToUpdate),
},
{
workspaceMember: {
id: workspaceMemberId,
},
},
);
}
}
public async unmatchCalendarEventParticipants(
workspaceId: string,
handle: string,
personId?: string,
workspaceMemberId?: string,
) {
const calendarEventParticipantRepository =
await this.twentyORMManager.getRepository<CalendarEventParticipantWorkspaceEntity>(
'calendarEventParticipant',
);
if (personId) {
await calendarEventParticipantRepository.update(
{
handle,
},
{
person: null,
},
);
}
if (workspaceMemberId) {
await calendarEventParticipantRepository.update(
{
handle,
},
{
workspaceMember: null,
},
);
}
await this.matchParticipantService.matchParticipants(
savedParticipants,
'messageParticipant',
transactionManager,
);
}
}

View File

@ -4,7 +4,6 @@ import { CalendarBlocklistManagerModule } from 'src/modules/calendar/blocklist-m
import { CalendarEventCleanerModule } from 'src/modules/calendar/calendar-event-cleaner/calendar-event-cleaner.module';
import { CalendarEventImportManagerModule } from 'src/modules/calendar/calendar-event-import-manager/calendar-event-import-manager.module';
import { CalendarEventParticipantManagerModule } from 'src/modules/calendar/calendar-event-participant-manager/calendar-event-participant-manager.module';
import { CalendarCommonModule } from 'src/modules/calendar/common/calendar-common.module';
@Module({
imports: [
@ -12,7 +11,6 @@ import { CalendarCommonModule } from 'src/modules/calendar/common/calendar-commo
CalendarEventCleanerModule,
CalendarEventImportManagerModule,
CalendarEventParticipantManagerModule,
CalendarCommonModule,
],
providers: [],
exports: [],

View File

@ -1,11 +0,0 @@
import { Module } from '@nestjs/common';
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant-manager/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
@Module({
imports: [WorkspaceDataSourceModule],
providers: [AddPersonIdAndWorkspaceMemberIdService],
exports: [],
})
export class CalendarCommonModule {}