Skip to content

The open alternative to Salesforce, designed for AI.

6.2 KB
Raw Blame
import { timingSafeEqual } from 'crypto';

import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

import { type LifecycleEventType } from '@microsoft/microsoft-graph-types';
import { isNonEmptyString } from '@sniptt/guards';
import { type AssertUnreachable } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import { In, Repository } from 'typeorm';

import { ExceptionHandlerService } from 'src/engine/core-modules/exception-handler/exception-handler.service';
import { MetricsService } from 'src/engine/core-modules/metrics/metrics.service';
import { MetricsKeys } from 'src/engine/core-modules/metrics/types/metrics-keys.type';
import { MessageChannelEntity } from 'src/engine/metadata-modules/message-channel/entities/message-channel.entity';
import { MessagingWebhookSubscriptionService } from 'src/modules/connected-account/webhook-subscription-manager/services/messaging-webhook-subscription.service';
import { WebhookSyncTriggerService } from 'src/modules/connected-account/webhook-subscription-manager/services/webhook-sync-trigger.service';
import { type MicrosoftGraphNotification } from 'src/modules/connected-account-sync-webhooks/types/microsoft-graph-notification.type';
import { type WebhookNotificationHandler } from 'src/modules/connected-account-sync-webhooks/types/webhook-notification-handler.type';

@Injectable()
export class MicrosoftMessagingNotificationHandler implements WebhookNotificationHandler<
  MicrosoftGraphNotification[]
> {
  private readonly logger = new Logger(
    MicrosoftMessagingNotificationHandler.name,
  );

  constructor(
    @InjectRepository(MessageChannelEntity)
    private readonly messageChannelRepository: Repository<MessageChannelEntity>,
    private readonly messagingWebhookSubscriptionService: MessagingWebhookSubscriptionService,
    private readonly webhookSyncTriggerService: WebhookSyncTriggerService,
    private readonly metricsService: MetricsService,
    private readonly exceptionHandlerService: ExceptionHandlerService,
  ) {}

  async handle(notifications: MicrosoftGraphNotification[]): Promise<void> {
    if (notifications.length > 0) {
      this.metricsService.incrementCounterBy({
        key: MetricsKeys.ConnectedAccountSyncWebhookReceivedMessaging,
        amount: notifications.length,
      });
    }

    const subscriptionIds = notifications
      .map((notification) => notification.subscriptionId)
      .filter(isNonEmptyString);

    if (subscriptionIds.length === 0) {
      return;
    }

    const messageChannels = await this.messageChannelRepository.find({
      where: { webhookSubscriptionExternalId: In(subscriptionIds) },
    });

    const messageChannelByExternalId = new Map(
      messageChannels.map((messageChannel) => [
        messageChannel.webhookSubscriptionExternalId,
        messageChannel,
      ]),
    );

    for (const notification of notifications) {
      const messageChannel = messageChannelByExternalId.get(
        notification.subscriptionId,
      );

      if (!isDefined(messageChannel)) {
        this.logger.warn(
          `No messaging subscription found for ${notification.subscriptionId}`,
        );
        continue;
      }

      const clientState = notification.clientState;
      const expectedClientState = messageChannel.webhookSubscriptionClientState;
      const clientStateBuffer = isNonEmptyString(clientState)
        ? Buffer.from(clientState)
        : null;
      const expectedClientStateBuffer = isNonEmptyString(expectedClientState)
        ? Buffer.from(expectedClientState)
        : null;

      if (
        !isDefined(clientStateBuffer) ||
        !isDefined(expectedClientStateBuffer) ||
        clientStateBuffer.length !== expectedClientStateBuffer.length ||
        !timingSafeEqual(clientStateBuffer, expectedClientStateBuffer)
      ) {
        this.logger.warn(
          `Client state mismatch for subscription ${notification.subscriptionId}`,
        );
        continue;
      }

      if (isNonEmptyString(notification.lifecycleEvent)) {
        await this.handleLifecycleEvent({
          lifecycleEvent: notification.lifecycleEvent,
          removedSubscriptionId: notification.subscriptionId,
          messageChannel,
        }).catch((error) => {
          this.logger.error(
            `Failed to handle ${notification.lifecycleEvent} lifecycle event for message channel ${messageChannel.id}`,
            error,
          );
          this.exceptionHandlerService.captureExceptions([error], {
            workspace: { id: messageChannel.workspaceId },
          });
        });
        continue;
      }

      await this.webhookSyncTriggerService.triggerMessagingSync(
        messageChannel.id,
        messageChannel.workspaceId,
      );

      this.logger.log(
        `Triggered messaging sync for message channel ${messageChannel.id} from Microsoft notification`,
      );
    }
  }

  private async handleLifecycleEvent({
    lifecycleEvent,
    removedSubscriptionId,
    messageChannel,
  }: {
    lifecycleEvent: LifecycleEventType;
    removedSubscriptionId: string;
    messageChannel: MessageChannelEntity;
  }): Promise<void> {
    switch (lifecycleEvent) {
      case 'subscriptionRemoved':
        await this.messagingWebhookSubscriptionService.recreateSubscription({
          messageChannelId: messageChannel.id,
          workspaceId: messageChannel.workspaceId,
          removedSubscriptionId,
        });
        await this.webhookSyncTriggerService.triggerMessagingSync(
          messageChannel.id,
          messageChannel.workspaceId,
        );
        break;
      case 'reauthorizationRequired':
        await this.messagingWebhookSubscriptionService.renewSubscription({
          messageChannelId: messageChannel.id,
          workspaceId: messageChannel.workspaceId,
        });
        break;
      case 'missed':
        await this.webhookSyncTriggerService.triggerMessagingSync(
          messageChannel.id,
          messageChannel.workspaceId,
        );
        break;
      default: {
        const unhandledLifecycleEvent: AssertUnreachable<
          typeof lifecycleEvent
        > = lifecycleEvent;

        this.logger.warn(
          `Ignored unrecognized lifecycle event ${unhandledLifecycleEvent} for message channel ${messageChannel.id}`,
        );
      }
    }
  }
}