Skip to content

The open alternative to Salesforce, designed for AI.

2.1 KB
Raw Blame
import { type MessageChannelVisibility } from '@/sdk/define/common/types/loose-shared-types.type';
import { APP_MESSAGE_CHANNEL_SELECTION } from '@/sdk/logic-function/messaging/message-channel-fields.constant';
import { type AppMessageChannel } from '@/sdk/logic-function/messaging/types/app-message-channel.type';
import { postGraphqlRequest } from '@/sdk/logic-function/utils/post-graphql-request.util';

const CREATE_APP_MESSAGE_CHANNEL_MUTATION = `
  mutation CreateAppMessageChannel($input: CreateAppMessageChannelInput!) {
    createAppMessageChannel(input: $input) {
      ${APP_MESSAGE_CHANNEL_SELECTION}
    }
  }
`;

export type CreateMessageChannelInput = {
  // An app connection id (from getConnection/listConnections); the channel speaks through it and inherits who may see it
  connectedAccountId: string;
  // The account's provider identity; inbound participants matching it are treated as the account itself
  handle: string;
  displayName?: string;
  // Required: decides whether one member's conversations are readable by the whole workspace, and only the app
  // knows how private its messages are ('METADATA' for personal inboxes, 'SHARE_EVERYTHING' for shared ones)
  visibility: MessageChannelVisibility;
  // Person field to match participant handles against (handles are then stored in its canonical form); set once
  participantHandleFieldUniversalIdentifier?: string;
  messagingProvider?: { universalIdentifier: string };
};

export const createMessageChannel = async ({
  messagingProvider,
  ...input
}: CreateMessageChannelInput): Promise<AppMessageChannel> => {
  const { createAppMessageChannel } = await postGraphqlRequest<
    {
      input: Omit<CreateMessageChannelInput, 'messagingProvider'> & {
        messagingProviderUniversalIdentifier?: string;
      };
    },
    { createAppMessageChannel: AppMessageChannel }
  >({
    query: CREATE_APP_MESSAGE_CHANNEL_MUTATION,
    variables: {
      input: {
        ...input,
        messagingProviderUniversalIdentifier:
          messagingProvider?.universalIdentifier,
      },
    },
    caller: 'createMessageChannel',
  });

  return createAppMessageChannel;
};