4736 add listener on calendarchannel isautocontactcreationenabled (#4913)

Closes #4736
This commit is contained in:
bosiraphael
2024-04-11 17:57:19 +02:00
committed by GitHub
parent 7211730570
commit 8853408264
11 changed files with 186 additions and 62 deletions

View File

@ -1,8 +1,10 @@
import { Module } from '@nestjs/common';
import { CalendarChannelListener } from 'src/modules/calendar/listeners/calendar-channel.listener';
@Module({
imports: [],
providers: [],
providers: [CalendarChannelListener],
exports: [],
})
export class CalendarModule {}

View File

@ -0,0 +1,73 @@
import { Injectable, Logger } from '@nestjs/common';
import { MessageQueueJob } from 'src/engine/integrations/message-queue/interfaces/message-queue-job.interface';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository';
import { CalendarEventParticipantRepository } from 'src/modules/calendar/repositories/calendar-event-participant.repository';
import { CalendarChannelObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel.object-metadata';
import { CalendarEventParticipantObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-participant.object-metadata';
import { CreateCompanyAndContactService } from 'src/modules/connected-account/auto-companies-and-contacts-creation/services/create-company-and-contact.service';
export type CalendarCreateCompanyAndContactAfterSyncJobData = {
workspaceId: string;
calendarChannelId: string;
};
@Injectable()
export class CalendarCreateCompanyAndContactAfterSyncJob
implements MessageQueueJob<CalendarCreateCompanyAndContactAfterSyncJobData>
{
private readonly logger = new Logger(
CalendarCreateCompanyAndContactAfterSyncJob.name,
);
constructor(
private readonly createCompanyAndContactService: CreateCompanyAndContactService,
@InjectObjectMetadataRepository(CalendarChannelObjectMetadata)
private readonly calendarChannelService: CalendarChannelRepository,
@InjectObjectMetadataRepository(CalendarEventParticipantObjectMetadata)
private readonly calendarEventParticipantRepository: CalendarEventParticipantRepository,
) {}
async handle(
data: CalendarCreateCompanyAndContactAfterSyncJobData,
): Promise<void> {
this.logger.log(
`create contacts and companies after sync for workspace ${data.workspaceId} and calendarChannel ${data.calendarChannelId}`,
);
const { workspaceId, calendarChannelId } = data;
const calendarChannels = await this.calendarChannelService.getByIds(
[calendarChannelId],
workspaceId,
);
if (calendarChannels.length === 0) {
throw new Error(
`Calendar channel with id ${calendarChannelId} not found in workspace ${workspaceId}`,
);
}
const { handle, isContactAutoCreationEnabled } = calendarChannels[0];
if (!isContactAutoCreationEnabled || !handle) {
return;
}
const calendarEventParticipantsWithoutPersonIdAndWorkspaceMemberId =
await this.calendarEventParticipantRepository.getByCalendarChannelIdWithoutPersonIdAndWorkspaceMemberId(
calendarChannelId,
workspaceId,
);
await this.createCompanyAndContactService.createCompaniesAndContactsAndUpdateParticipants(
handle,
calendarEventParticipantsWithoutPersonIdAndWorkspaceMemberId,
workspaceId,
);
this.logger.log(
`create contacts and companies after sync for workspace ${data.workspaceId} and calendarChannel ${data.calendarChannelId} done`,
);
}
}

View File

@ -0,0 +1,41 @@
import { Inject, Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { ObjectRecordUpdateEvent } from 'src/engine/integrations/event-emitter/types/object-record-update.event';
import { objectRecordChangedProperties } from 'src/engine/integrations/event-emitter/utils/object-record-changed-properties.util';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import {
CalendarCreateCompanyAndContactAfterSyncJobData,
CalendarCreateCompanyAndContactAfterSyncJob,
} from 'src/modules/calendar/jobs/calendar-create-company-and-contact-after-sync.job';
import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel.object-metadata';
@Injectable()
export class CalendarChannelListener {
constructor(
@Inject(MessageQueue.messagingQueue)
private readonly messageQueueService: MessageQueueService,
) {}
@OnEvent('calendarChannel.updated')
async handleUpdatedEvent(
payload: ObjectRecordUpdateEvent<MessageChannelObjectMetadata>,
) {
if (
objectRecordChangedProperties(
payload.details.before,
payload.details.after,
).includes('isContactAutoCreationEnabled') &&
payload.details.after.isContactAutoCreationEnabled
) {
await this.messageQueueService.add<CalendarCreateCompanyAndContactAfterSyncJobData>(
CalendarCreateCompanyAndContactAfterSyncJob.name,
{
workspaceId: payload.workspaceId,
calendarChannelId: payload.recordId,
},
);
}
}
}

View File

@ -263,4 +263,33 @@ export class CalendarEventParticipantRepository {
return calendarEventParticipants;
}
public async getByCalendarChannelIdWithoutPersonIdAndWorkspaceMemberId(
calendarChannelId: string,
workspaceId: string,
transactionManager?: EntityManager,
): Promise<CalendarEventParticipantWithId[]> {
if (!workspaceId) {
throw new Error('WorkspaceId is required');
}
const dataSourceSchema =
this.workspaceDataSourceService.getSchemaName(workspaceId);
const calendarEventParticipants: CalendarEventParticipantWithId[] =
await this.workspaceDataSourceService.executeRawQuery(
`SELECT "calendarEventParticipant".*
FROM ${dataSourceSchema}."calendarEventParticipant" AS "calendarEventParticipant"
LEFT JOIN ${dataSourceSchema}."calendarEvent" AS "calendarEvent" ON "calendarEventParticipant"."calendarEventId" = "calendarEvent"."id"
LEFT JOIN ${dataSourceSchema}."calendarChannelEventAssociation" AS "calendarChannelEventAssociation" ON "calendarEvent"."id" = "calendarChannelEventAssociation"."calendarEventId"
WHERE "calendarChannelEventAssociation"."calendarChannelId" = $1
AND "calendarEventParticipant"."personId" IS NULL
AND "calendarEventParticipant"."workspaceMemberId" IS NULL`,
[calendarChannelId],
workspaceId,
transactionManager,
);
return calendarEventParticipants;
}
}

View File

@ -7,10 +7,7 @@ import { PersonRepository } from 'src/modules/person/repositories/person.reposit
import { PersonObjectMetadata } from 'src/modules/person/standard-objects/person.object-metadata';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/getFlattenedValuesAndValuesStringForBatchRawQuery.util';
import {
CalendarEventParticipant,
CalendarEventParticipantWithId,
} from 'src/modules/calendar/types/calendar-event';
import { CalendarEventParticipant } from 'src/modules/calendar/types/calendar-event';
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/connected-account/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
import { CalendarEventParticipantRepository } from 'src/modules/calendar/repositories/calendar-event-participant.repository';
import { CalendarEventParticipantObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-participant.object-metadata';
@ -26,11 +23,15 @@ export class CalendarEventParticipantService {
private readonly addPersonIdAndWorkspaceMemberIdService: AddPersonIdAndWorkspaceMemberIdService,
) {}
public async updateCalendarEventParticipantsAfterContactCreation(
participants: CalendarEventParticipantWithId[],
public async updateCalendarEventParticipantsAfterPeopleCreation(
workspaceId: string,
transactionManager?: EntityManager,
): Promise<void> {
const participants =
await this.calendarEventParticipantRepository.getWithoutPersonIdAndWorkspaceMemberId(
workspaceId,
);
if (!participants) return;
const dataSourceSchema =