Skip to content

The open alternative to Salesforce, designed for AI.

3.8 KB
Raw Blame
import { Logger, Scope } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

import { In, Repository } from 'typeorm';

import { MessageChannelSyncStage } from 'twenty-shared/types';
import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';
import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { MessageChannelEntity } from 'src/engine/metadata-modules/message-channel/entities/message-channel.entity';
import { WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';
import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';
import { MessageChannelSyncStatusService } from 'src/modules/messaging/common/services/message-channel-sync-status.service';
import { MESSAGING_ONGOING_STALE_SYNC_STAGES } from 'src/modules/messaging/message-import-manager/constants/messaging-ongoing-stale-sync-stages.constant';
import { isSyncStale } from 'src/modules/messaging/message-import-manager/utils/is-sync-stale.util';
import { toIsoStringOrNull } from 'src/utils/date/toIsoStringOrNull';

export type MessagingOngoingStaleJobData = {
  workspaceId: string;
};

@Processor({
  queueName: MessageQueue.messagingQueue,
  scope: Scope.REQUEST,
})
export class MessagingOngoingStaleJob {
  private readonly logger = new Logger(MessagingOngoingStaleJob.name);
  constructor(
    private readonly workspaceOrmManager: WorkspaceOrmManager,
    @InjectRepository(MessageChannelEntity)
    private readonly messageChannelRepository: Repository<MessageChannelEntity>,
    private readonly messageChannelSyncStatusService: MessageChannelSyncStatusService,
  ) {}

  @Process(MessagingOngoingStaleJob.name)
  async handle(data: MessagingOngoingStaleJobData): Promise<void> {
    const { workspaceId } = data;

    const authContext = buildSystemAuthContext(workspaceId);

    await this.workspaceOrmManager.executeInWorkspaceContext(
      async () => {
        const messageChannels = await this.messageChannelRepository.find({
          where: {
            syncStage: In(MESSAGING_ONGOING_STALE_SYNC_STAGES),
            workspaceId,
          },
        });

        for (const messageChannel of messageChannels) {
          if (
            isSyncStale(toIsoStringOrNull(messageChannel.syncStageStartedAt))
          ) {
            await this.messageChannelSyncStatusService.resetSyncStageStartedAt(
              [messageChannel.id],
              workspaceId,
            );

            switch (messageChannel.syncStage) {
              case MessageChannelSyncStage.MESSAGE_LIST_FETCH_ONGOING:
              case MessageChannelSyncStage.MESSAGE_LIST_FETCH_SCHEDULED:
                this.logger.log(
                  `Sync for message channel ${messageChannel.id} and workspace ${workspaceId} is stale. Setting sync stage to MESSAGE_LIST_FETCH_PENDING`,
                );
                await this.messageChannelSyncStatusService.markAsMessagesListFetchPending(
                  [messageChannel.id],
                  workspaceId,
                );
                break;
              case MessageChannelSyncStage.MESSAGES_IMPORT_ONGOING:
              case MessageChannelSyncStage.MESSAGES_IMPORT_SCHEDULED:
                this.logger.log(
                  `Sync for message channel ${messageChannel.id} and workspace ${workspaceId} is stale. Setting sync stage to MESSAGES_IMPORT_PENDING`,
                );
                await this.messageChannelSyncStatusService.markAsMessagesImportPending(
                  [messageChannel.id],
                  workspaceId,
                );
                break;
              default:
                break;
            }
          }
        }
      },
      authContext,
      { lite: true },
    );
  }
}