Skip to content

The open alternative to Salesforce, designed for AI.

989 B
Raw Blame
import { reportConnectionAuthFailure } from 'twenty-sdk/logic-function';

import { SLACK_CONNECTION_AUTH_FAILURE_REPORT_TIMEOUT_MS } from 'src/logic-functions/constants/slack-connection-auth-failure-report-timeout-ms';

export const reportSlackConnectionAuthFailure = async ({
  connectionId,
  reason,
}: {
  connectionId: string;
  reason: string;
}): Promise<void> => {
  let timeoutId: ReturnType<typeof setTimeout> | undefined;

  const reportPromise = reportConnectionAuthFailure({
    connectionId,
    reason,
  }).catch((error: unknown) => {
    console.warn(
      `[slack] reporting the connection auth failure failed: ${error instanceof Error ? error.message : String(error)}`,
    );
  });

  const timeoutPromise = new Promise<void>((resolve) => {
    timeoutId = setTimeout(
      resolve,
      SLACK_CONNECTION_AUTH_FAILURE_REPORT_TIMEOUT_MS,
    );
  });

  try {
    await Promise.race([reportPromise, timeoutPromise]);
  } finally {
    clearTimeout(timeoutId);
  }
};