Skip to content

The open alternative to Salesforce, designed for AI.

3.9 KB
Raw Blame
import { isNonEmptyString } from '@sniptt/guards';
import { CoreApiClient } from 'twenty-client-sdk/core';
import { defineLogicFunction } from 'twenty-sdk/define';
import {
  type InputJsonSchema,
  type LogicFunctionExecutionContext,
  jsonSchemaToInputSchema,
} from 'twenty-sdk/logic-function';

import { TEAMS_SYNC_TRANSCRIPT_UNIVERSAL_IDENTIFIER } from 'src/features/transcripts/constants/universal-identifiers';
import { type CallRecordingSyncFields } from 'src/features/transcripts/logic-functions/types/call-recording-sync-fields.type';
import { getTeamsConnectionForRequestOrThrow } from 'src/features/transcripts/logic-functions/utils/get-teams-connection-for-request-or-throw';
import { syncTeamsTranscriptToCallRecordingOrThrow } from 'src/features/transcripts/logic-functions/utils/sync-teams-transcript-to-call-recording-or-throw';
import { getConnectionAccessTokenOrThrow } from 'src/utils/get-connection-access-token-or-throw';
import { toErrorMessage } from 'src/utils/to-error-message';
import { isTeamsTranscriptImportEnabled } from 'src/features/transcripts/logic-functions/utils/is-teams-transcript-import-enabled';

const teamsSyncTranscriptInputSchema: InputJsonSchema = {
  type: 'object',
  properties: {
    meetingId: {
      type: 'string',
      label: 'Meeting ID',
      description: 'The meetingId returned by List My Teams Transcripts.',
    },
    transcriptId: {
      type: 'string',
      label: 'Transcript ID',
      description: 'The transcriptId returned by List My Teams Transcripts.',
    },
  },
  required: ['meetingId', 'transcriptId'],
  additionalProperties: false,
};

type TeamsSyncTranscriptResult =
  | {
      success: true;
      callRecordingId: string;
      calendarEventId?: string;
      status: CallRecordingSyncFields['status'];
      created: boolean;
      skipped: boolean;
    }
  | { success: false; error: string };

export const teamsSyncTranscriptHandler = async (
  parameters: {
    meetingId?: unknown;
    transcriptId?: unknown;
  },
  context: Pick<LogicFunctionExecutionContext, 'userWorkspaceId'>,
): Promise<TeamsSyncTranscriptResult> => {
  if (!isTeamsTranscriptImportEnabled()) {
    return { success: false, error: 'Teams transcripts are not enabled.' };
  }

  const { meetingId, transcriptId } = parameters;

  if (!isNonEmptyString(meetingId) || !isNonEmptyString(transcriptId)) {
    return {
      success: false,
      error: 'meetingId and transcriptId are required.',
    };
  }

  try {
    const connection = await getTeamsConnectionForRequestOrThrow(context);
    const result = await syncTeamsTranscriptToCallRecordingOrThrow({
      accessToken: getConnectionAccessTokenOrThrow(connection),
      coreApiClient: new CoreApiClient({ runAs: 'application' }),
      meetingId,
      transcriptId,
    });

    return { success: true, ...result };
  } catch (error) {
    return { success: false, error: toErrorMessage(error) };
  }
};

export default defineLogicFunction({
  universalIdentifier: TEAMS_SYNC_TRANSCRIPT_UNIVERSAL_IDENTIFIER,
  name: 'teams-sync-transcript',
  description:
    'Sync one Teams meeting transcript from your connected Microsoft account into a Call Recording. Takes the meetingId and transcriptId returned by List My Teams Transcripts, links the recording to the matching calendar event, and leaves recordings deleted in Twenty deleted.',
  timeoutSeconds: 300,
  handler: teamsSyncTranscriptHandler,
  toolTriggerSettings: { inputSchema: teamsSyncTranscriptInputSchema },
  workflowActionTriggerSettings: {
    label: 'Sync Teams Transcript',
    inputSchema: jsonSchemaToInputSchema(teamsSyncTranscriptInputSchema),
    outputSchema: [
      {
        type: 'object',
        properties: {
          success: { type: 'boolean' },
          error: { type: 'string' },
          callRecordingId: { type: 'string' },
          calendarEventId: { type: 'string' },
          status: { type: 'string' },
          created: { type: 'boolean' },
          skipped: { type: 'boolean' },
        },
      },
    ],
  },
});