|
| 1 | +import { Logger, Scope } from '@nestjs/common'; |
| 2 | + |
| 3 | +import { In } from 'typeorm'; |
| 4 | + |
| 5 | +import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator'; |
| 6 | +import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator'; |
| 7 | +import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; |
| 8 | +import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager'; |
| 9 | +import { CalendarChannelSyncStatusService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-channel-sync-status.service'; |
| 10 | +import { isSyncStale } from 'src/modules/calendar/calendar-event-import-manager/utils/is-sync-stale.util'; |
| 11 | +import { |
| 12 | + CalendarChannelSyncStage, |
| 13 | + CalendarChannelWorkspaceEntity, |
| 14 | +} from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity'; |
| 15 | + |
| 16 | +export type CalendarOngoingStaleJobData = { |
| 17 | + workspaceId: string; |
| 18 | +}; |
| 19 | + |
| 20 | +@Processor({ |
| 21 | + queueName: MessageQueue.messagingQueue, |
| 22 | + scope: Scope.REQUEST, |
| 23 | +}) |
| 24 | +export class CalendarOngoingStaleJob { |
| 25 | + private readonly logger = new Logger(CalendarOngoingStaleJob.name); |
| 26 | + constructor( |
| 27 | + private readonly twentyORMManager: TwentyORMManager, |
| 28 | + private readonly calendarChannelSyncStatusService: CalendarChannelSyncStatusService, |
| 29 | + ) {} |
| 30 | + |
| 31 | + @Process(CalendarOngoingStaleJob.name) |
| 32 | + async handle(data: CalendarOngoingStaleJobData): Promise<void> { |
| 33 | + const { workspaceId } = data; |
| 34 | + |
| 35 | + const calendarChannelRepository = |
| 36 | + await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>( |
| 37 | + 'calendarChannel', |
| 38 | + ); |
| 39 | + |
| 40 | + const calendarChannels = await calendarChannelRepository.find({ |
| 41 | + where: { |
| 42 | + syncStage: In([ |
| 43 | + CalendarChannelSyncStage.CALENDAR_EVENTS_IMPORT_ONGOING, |
| 44 | + CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_ONGOING, |
| 45 | + ]), |
| 46 | + }, |
| 47 | + }); |
| 48 | + |
| 49 | + for (const calendarChannel of calendarChannels) { |
| 50 | + if ( |
| 51 | + calendarChannel.syncStageStartedAt && |
| 52 | + isSyncStale(calendarChannel.syncStageStartedAt) |
| 53 | + ) { |
| 54 | + this.logger.log( |
| 55 | + `Sync for calendar channel ${calendarChannel.id} and workspace ${workspaceId} is stale. Setting sync stage to pending`, |
| 56 | + ); |
| 57 | + await this.calendarChannelSyncStatusService.resetSyncStageStartedAt( |
| 58 | + calendarChannel.id, |
| 59 | + ); |
| 60 | + |
| 61 | + switch (calendarChannel.syncStage) { |
| 62 | + case CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_ONGOING: |
| 63 | + await this.calendarChannelSyncStatusService.schedulePartialCalendarEventListFetch( |
| 64 | + calendarChannel.id, |
| 65 | + ); |
| 66 | + break; |
| 67 | + case CalendarChannelSyncStage.CALENDAR_EVENTS_IMPORT_ONGOING: |
| 68 | + await this.calendarChannelSyncStatusService.scheduleCalendarEventsImport( |
| 69 | + calendarChannel.id, |
| 70 | + ); |
| 71 | + break; |
| 72 | + default: |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments