Skip to content

The open alternative to Salesforce, designed for AI.

1.3 KB
Raw Blame
import { AppConnectionAuthFailedError } from '@/sdk/logic-function/connections/errors/app-connection-auth-failed.error';
import { type AppConnection } from '@/sdk/logic-function/connections/types/app-connection.type';
import {
  type AppConnectionWithoutVisibility,
  withLegacyConnectionVisibility,
} from '@/sdk/logic-function/connections/utils/with-legacy-connection-visibility.util';
import { postGraphqlRequest } from '@/sdk/logic-function/utils/post-graphql-request.util';

const GET_APP_CONNECTION_QUERY = `
  query GetAppConnection($id: ID!) {
    appConnection(id: $id) {
      id
      providerName
      name
      handle
      externalAccountId
      scope
      userWorkspaceId
      workspaceMemberId
      providerType
      accessToken
      scopes
      authFailedAt
      authFailedReason
    }
  }
`;

export const getConnection = async (id: string): Promise<AppConnection> => {
  const { appConnection } = await postGraphqlRequest<
    { id: string },
    { appConnection: AppConnectionWithoutVisibility }
  >({
    query: GET_APP_CONNECTION_QUERY,
    variables: { id },
    caller: 'getConnection',
  });

  if (appConnection.authFailedAt !== null) {
    throw new AppConnectionAuthFailedError(appConnection.id);
  }

  return withLegacyConnectionVisibility(appConnection);
};