Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.9 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import {
  HttpMethod,
  httpClient,
} from '@activepieces/pieces-common';
import { zendeskAuth } from '../auth';
import { getZendeskAuthentication, getZendeskBaseUrl } from '../common/client';
import { findGroupOutputSchema } from '../output-schemas';

interface ZendeskGroup {
  id: number;
  name: string;
  is_public: boolean;
  created_at: string;
  updated_at: string;
}

interface ZendeskGroupsResponse {
  groups: ZendeskGroup[];
}

export const findGroupAction = createAction({
  auth: zendeskAuth,
  name: 'find-group',
  outputSchema: findGroupOutputSchema,
  classification: 'READ',
  displayName: 'Find a Group',
  description: 'Find a group by name.',
  audience: 'human',
  aiMetadata: { description: 'Finds a support group in Zendesk by name. Returns the first matching group with its profile details. Useful for assigning tickets or routing work to specific support teams.', idempotent: true },
  props: {
    group_name: Property.ShortText({
      displayName: 'Group Name',
      description: 'The name of the group to find',
      required: true,
    }),
  },
  async run({ propsValue, auth }) {
    const authentication = auth;
    const { group_name } = propsValue;

    if (!group_name || group_name.trim() === '') {
      throw new Error('Group name is required');
    }

    try {
      let url: string | undefined = `${getZendeskBaseUrl(authentication)}/groups.json?per_page=100`;
      let matchedGroup: ZendeskGroup | undefined;

      while (url && !matchedGroup) {
        const currentUrl: string = url;
        const response = await httpClient.sendRequest<ZendeskGroupsResponse & { next_page?: string }>({
          url: currentUrl,
          method: HttpMethod.GET,
          authentication: getZendeskAuthentication(authentication),
        });

        const groups = response.body.groups || [];

        matchedGroup = groups.find(
          (group) => group.name && group.name.toLowerCase() === group_name.toLowerCase()
        );

        url = response.body.next_page;
      }

      if (!matchedGroup) {
        throw new Error(`No group found with name "${group_name}"`);
      }

      return {
        success: true,
        group: matchedGroup,
        id: matchedGroup.id,
        name: matchedGroup.name,
        is_public: matchedGroup.is_public,
        created_at: matchedGroup.created_at,
        updated_at: matchedGroup.updated_at,
      };
    } catch (error) {
      const errorMessage = (error as Error).message;
      if (errorMessage.includes('401') || errorMessage.includes('403')) {
        throw new Error(
          'Authentication failed. Please check your API credentials and permissions.'
        );
      }

      if (errorMessage.includes('429')) {
        throw new Error(
          'Rate limit exceeded. Please wait a moment before trying again.'
        );
      }

      throw error;
    }
  },
});