@ -0,0 +1,59 @@
|
||||
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 { GoogleCalendarSyncService } from 'src/modules/calendar/services/google-calendar-sync/google-calendar-sync.service';
|
||||
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
|
||||
import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata';
|
||||
|
||||
export type BlocklistReimportCalendarEventsJobData = {
|
||||
workspaceId: string;
|
||||
workspaceMemberId: string;
|
||||
handle: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class BlocklistReimportCalendarEventsJob
|
||||
implements MessageQueueJob<BlocklistReimportCalendarEventsJobData>
|
||||
{
|
||||
private readonly logger = new Logger(BlocklistReimportCalendarEventsJob.name);
|
||||
|
||||
constructor(
|
||||
@InjectObjectMetadataRepository(ConnectedAccountObjectMetadata)
|
||||
private readonly connectedAccountRepository: ConnectedAccountRepository,
|
||||
private readonly googleCalendarSyncService: GoogleCalendarSyncService,
|
||||
) {}
|
||||
|
||||
async handle(data: BlocklistReimportCalendarEventsJobData): Promise<void> {
|
||||
const { workspaceId, workspaceMemberId, handle } = data;
|
||||
|
||||
this.logger.log(
|
||||
`Reimporting calendar events from handle ${handle} in workspace ${workspaceId} for workspace member ${workspaceMemberId}`,
|
||||
);
|
||||
|
||||
const connectedAccount =
|
||||
await this.connectedAccountRepository.getAllByWorkspaceMemberId(
|
||||
workspaceMemberId,
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
if (!connectedAccount || connectedAccount.length === 0) {
|
||||
this.logger.error(
|
||||
`No connected account found for workspace member ${workspaceMemberId} in workspace ${workspaceId}`,
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await this.googleCalendarSyncService.startGoogleCalendarSync(
|
||||
workspaceId,
|
||||
connectedAccount[0].id,
|
||||
handle,
|
||||
);
|
||||
|
||||
this.logger.log(
|
||||
`Reimporting calendar events from ${handle} in workspace ${workspaceId} for workspace member ${workspaceMemberId} done`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -2,12 +2,17 @@ import { Inject, Injectable } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
|
||||
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event';
|
||||
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event';
|
||||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
|
||||
import {
|
||||
BlocklistItemDeleteCalendarEventsJobData,
|
||||
BlocklistItemDeleteCalendarEventsJob,
|
||||
} from 'src/modules/calendar/jobs/blocklist-item-delete-calendar-events.job';
|
||||
import {
|
||||
BlocklistReimportCalendarEventsJobData,
|
||||
BlocklistReimportCalendarEventsJob,
|
||||
} from 'src/modules/calendar/jobs/blocklist-reimport-calendar-events.job';
|
||||
import { BlocklistObjectMetadata } from 'src/modules/connected-account/standard-objects/blocklist.object-metadata';
|
||||
|
||||
@Injectable()
|
||||
@ -29,4 +34,18 @@ export class CalendarBlocklistListener {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@OnEvent('blocklist.deleted')
|
||||
async handleDeletedEvent(
|
||||
payload: ObjectRecordDeleteEvent<BlocklistObjectMetadata>,
|
||||
) {
|
||||
await this.messageQueueService.add<BlocklistReimportCalendarEventsJobData>(
|
||||
BlocklistReimportCalendarEventsJob.name,
|
||||
{
|
||||
workspaceId: payload.workspaceId,
|
||||
workspaceMemberId: payload.properties.before.workspaceMember.id,
|
||||
handle: payload.properties.before.handle,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,6 +68,7 @@ export class GoogleCalendarSyncService {
|
||||
public async startGoogleCalendarSync(
|
||||
workspaceId: string,
|
||||
connectedAccountId: string,
|
||||
emailOrDomainToReimport?: string,
|
||||
): Promise<void> {
|
||||
const connectedAccount = await this.connectedAccountRepository.getById(
|
||||
connectedAccountId,
|
||||
@ -137,8 +138,9 @@ export class GoogleCalendarSyncService {
|
||||
const googleCalendarEvents = await googleCalendarClient.events.list({
|
||||
calendarId: 'primary',
|
||||
maxResults: 500,
|
||||
syncToken,
|
||||
syncToken: emailOrDomainToReimport ? undefined : syncToken,
|
||||
pageToken: nextPageToken,
|
||||
q: emailOrDomainToReimport,
|
||||
showDeleted: true,
|
||||
});
|
||||
|
||||
@ -174,10 +176,19 @@ export class GoogleCalendarSyncService {
|
||||
return;
|
||||
}
|
||||
|
||||
const filteredEvents = filterOutBlocklistedEvents(
|
||||
events,
|
||||
blocklistedEmails,
|
||||
);
|
||||
let filteredEvents = filterOutBlocklistedEvents(events, blocklistedEmails);
|
||||
|
||||
if (emailOrDomainToReimport) {
|
||||
// We still need to filter the events to only keep the ones that have the email or domain we want to reimport
|
||||
// because the q parameter in the list method also filters the events that have the email or domain in their summary, description ...
|
||||
// The q parameter allows us to narrow down the events
|
||||
filteredEvents = filteredEvents.filter(
|
||||
(event) =>
|
||||
event.attendees?.some(
|
||||
(attendee) => attendee.email?.endsWith(emailOrDomainToReimport),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const eventExternalIds = filteredEvents.map((event) => event.id as string);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user