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.5 KB
Raw Blame
import {
  AppConnectionValueForAuthProperty,
  createTrigger,
  TriggerStrategy,
} from '@activepieces/pieces-framework';
import {
  DedupeStrategy,
  HttpMethod,
  httpClient,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import { zendeskAuth } from '../auth';
import { getZendeskAuthentication, getZendeskBaseUrl } from '../common/client';
import { newGroupOutputSchema } from '../output-schemas';

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

type ZendeskAuthValue = AppConnectionValueForAuthProperty<typeof zendeskAuth>;

const polling: Polling<ZendeskAuthValue, Record<string, never>> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth }) => {
    let url: string | undefined = `${getZendeskBaseUrl(auth)}/groups?sort=-created_at`;
    const groups: ZendeskGroup[] = [];

    while (url) {
      const currentUrl: string = url;
      const response = await httpClient.sendRequest<{ groups: ZendeskGroup[]; next_page?: string }>({
        url: currentUrl,
        method: HttpMethod.GET,
        authentication: getZendeskAuthentication(auth),
      });

      groups.push(...response.body.groups);
      url = response.body.next_page;
    }

    return groups.map((group) => ({
      epochMilliSeconds: new Date(group.created_at).getTime(),
      data: group,
    }));
  },
};

export const newGroup = createTrigger({
  name: 'new_group',
  outputSchema: newGroupOutputSchema,
  classification: 'READ',
  displayName: 'New Group',
  description: 'Fires when a new group is created.',
  aiMetadata: {
    description: 'Fires when a new support group is created in Zendesk. Represents a newly added team or support department. Polls for newly created groups since Zendesk does not emit a webhook event for group creation.',
  },
  auth: zendeskAuth,
  props: {},
  type: TriggerStrategy.POLLING,
  sampleData: {
    id: 12345,
    url: 'https://example.zendesk.com/api/v2/groups/12345.json',
    name: 'Support Team',
    description: 'Main support team',
    is_public: true,
    created_at: '2023-03-25T02:39:41Z',
    updated_at: '2023-03-25T02:39:41Z',
  },
  async test(context) {
    return await pollingHelper.test(polling, context);
  },
  async onEnable(context) {
    await pollingHelper.onEnable(polling, context);
  },
  async onDisable(context) {
    await pollingHelper.onDisable(polling, context);
  },
  async run(context) {
    return await pollingHelper.poll(polling, context);
  },
});