Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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';

describe('isEmailBlocklisted', () => {
it('should return true if email is blocklisted', () => {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class CalendarSaveEventsService {
await this.calendarEventParticipantService.upsertAndDeleteCalendarEventParticipants(
participantsToSave,
participantsToUpdate,
workspaceId,
transactionManager,
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -41,7 +40,6 @@ import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/perso
CalendarEventParticipantListener,
CalendarEventParticipantPersonListener,
CalendarEventParticipantWorkspaceMemberListener,
AddPersonIdAndWorkspaceMemberIdService,
],
exports: [CalendarEventParticipantService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export class CalendarEventParticipantMatchParticipantJob {
return;
}

await this.calendarEventParticipantService.matchCalendarEventParticipants(
workspaceId,
await this.calendarEventParticipantService.matchCalendarEventParticipantsAfterPersonOrWorkspaceMemberCreation(
email,
workspaceId,
personId,
workspaceMemberId,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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';

export type CalendarEventParticipantUnmatchParticipantJobData = {
Expand All @@ -25,10 +25,9 @@ export class CalendarEventParticipantUnmatchParticipantJob {
async handle(
data: CalendarEventParticipantUnmatchParticipantJobData,
): Promise<void> {
const { workspaceId, email, personId, workspaceMemberId } = data;
const { email, personId, workspaceMemberId } = data;

await this.calendarEventParticipantService.unmatchCalendarEventParticipants(
workspaceId,
email,
personId,
workspaceMemberId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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 { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';

@Injectable()
export class CalendarEventParticipantService {
Expand All @@ -19,6 +21,7 @@ export class CalendarEventParticipantService {
public async upsertAndDeleteCalendarEventParticipants(
participantsToSave: CalendarEventParticipantWithCalendarEventId[],
participantsToUpdate: CalendarEventParticipantWithCalendarEventId[],
workspaceId: string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you have to pass the workspaceId here, you could get it from the scopedWorkspaceContextFactory (see twenty-orm.manager.ts). This would avoid the whole drill down and secure the behavior to avoid proposing where the workspace can be provided. That's being said I'm not 100% sure about this pattern of auto-resolving the workspaceId from the request

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

transactionManager?: any,
): Promise<void> {
const calendarEventParticipantRepository =
Expand Down Expand Up @@ -103,16 +106,107 @@ export class CalendarEventParticipantService {

participantsToSave.push(...newCalendarEventParticipants);

await calendarEventParticipantRepository.save(
const savedParticipants = await calendarEventParticipantRepository.save(
participantsToSave,
{},
transactionManager,
);

await this.matchCalendarEventParticipants(
savedParticipants,
workspaceId,
transactionManager,
);
}

private async matchCalendarEventParticipants(
calendarEventParticipants: CalendarEventParticipantWorkspaceEntity[],
workspaceId: string,
transactionManager?: any,
) {
const participantIds = calendarEventParticipants.map(
(participant) => participant.id,
);
const uniqueParticipantsHandles = [
...new Set(
calendarEventParticipants.map((participant) => participant.handle),
),
];

const calendarEventParticipantRepository =
await this.twentyORMManager.getRepository<CalendarEventParticipantWorkspaceEntity>(
'calendarEventParticipant',
);

const personRepository =
await this.twentyORMManager.getRepository<PersonWorkspaceEntity>(
'person',
);

const persons = await personRepository.find(
bosiraphael marked this conversation as resolved.
Show resolved Hide resolved
{
where: {
email: Any(uniqueParticipantsHandles),
},
},
transactionManager,
);

const workspaceMemberRepository =
await this.twentyORMManager.getRepository<WorkspaceMemberWorkspaceEntity>(
'workspaceMember',
);

const workspaceMembers = await workspaceMemberRepository.find(
{
where: {
userEmail: Any(uniqueParticipantsHandles),
},
},
transactionManager,
);

for (const handle of uniqueParticipantsHandles) {
const person = persons.find((person) => person.email === handle);

const workspaceMember = workspaceMembers.find(
(workspaceMember) => workspaceMember.userEmail === handle,
);

await calendarEventParticipantRepository.update(
{
id: Any(participantIds),
handle,
},
{
personId: person?.id,
workspaceMemberId: workspaceMember?.id,
},
transactionManager,
);
}

const matchedCalendarEventParticipants =
await calendarEventParticipantRepository.find(
{
where: {
id: Any(participantIds),
handle: Any(uniqueParticipantsHandles),
},
},
transactionManager,
);

this.eventEmitter.emit(`calendarEventParticipant.matched`, {
workspaceId,
workspaceMemberId: null,
messageParticipants: matchedCalendarEventParticipants,
});
}

public async matchCalendarEventParticipants(
public async matchCalendarEventParticipantsAfterPersonOrWorkspaceMemberCreation(
handle: string,
workspaceId: string,
email: string,
personId?: string,
workspaceMemberId?: string,
) {
Expand All @@ -124,7 +218,7 @@ export class CalendarEventParticipantService {
const calendarEventParticipantsToUpdate =
await calendarEventParticipantRepository.find({
where: {
handle: email,
handle,
},
});

Expand Down Expand Up @@ -156,6 +250,7 @@ export class CalendarEventParticipantService {
calendarEventParticipants: updatedCalendarEventParticipants,
});
}

if (workspaceMemberId) {
await calendarEventParticipantRepository.update(
{
Expand All @@ -171,7 +266,6 @@ export class CalendarEventParticipantService {
}

public async unmatchCalendarEventParticipants(
workspaceId: string,
handle: string,
personId?: string,
workspaceMemberId?: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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],
providers: [],
exports: [],
})
export class CalendarCommonModule {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need this one? looks like nothing is provided or exported

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MESSAGING_THROTTLE_DURATION } from 'src/modules/messaging/common/constants/messaging-throttle-duration';
import { MESSAGING_THROTTLE_DURATION } from 'src/modules/messaging/message-import-manager/constants/messaging-throttle-duration';

export const isThrottled = (
syncStageStartedAt: string | null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { AnalyticsModule } from 'src/engine/core-modules/analytics/analytics.module';
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.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 { MessagingChannelSyncStatusService } from 'src/modules/messaging/common/services/messaging-channel-sync-status.service';
import { MessagingErrorHandlingService } from 'src/modules/messaging/common/services/messaging-error-handling.service';
import { MessagingFetchByBatchesService } from 'src/modules/messaging/common/services/messaging-fetch-by-batch.service';
import { MessagingMessageThreadService } from 'src/modules/messaging/common/services/messaging-message-thread.service';
import { MessagingMessageService } from 'src/modules/messaging/common/services/messaging-message.service';
import { MessagingTelemetryService } from 'src/modules/messaging/common/services/messaging-telemetry.service';
import { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
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 { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';

@Module({
imports: [
HttpModule.register({
baseURL: 'https://www.googleapis.com/batch/gmail/v1',
}),
AnalyticsModule,
WorkspaceDataSourceModule,
ObjectMetadataRepositoryModule.forFeature([
PersonWorkspaceEntity,
Expand All @@ -33,22 +21,7 @@ import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/perso
]),
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
],
providers: [
MessagingMessageService,
MessagingMessageThreadService,
MessagingErrorHandlingService,
MessagingTelemetryService,
MessagingChannelSyncStatusService,
MessagingFetchByBatchesService,
AddPersonIdAndWorkspaceMemberIdService,
],
exports: [
MessagingMessageService,
MessagingMessageThreadService,
MessagingErrorHandlingService,
MessagingTelemetryService,
MessagingChannelSyncStatusService,
MessagingFetchByBatchesService,
],
providers: [MessagingChannelSyncStatusService],
exports: [MessagingChannelSyncStatusService],
})
export class MessagingCommonModule {}
Loading
Loading