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

16.0 KB
Raw Blame
import { DynamicPropsValue, Property } from '@activepieces/pieces-framework';
import { HttpMethod, httpClient } from '@activepieces/pieces-common';
import { zendeskAuth } from '../auth';
import { getZendeskAuthentication, getZendeskBaseUrl } from './client';

interface ZendeskTicket {
  id: number;
  subject: string;
  status: string;
  type?: string;
  requester_id: number;
  assignee_id?: number;
  created_at: string;
  updated_at: string;
}

interface ZendeskTicketsResponse {
  tickets: ZendeskTicket[];
}

interface ZendeskOrganization {
  id: number;
  name: string;
  details?: string;
  created_at: string;
  updated_at: string;
}

interface ZendeskOrganizationsResponse {
  organizations: ZendeskOrganization[];
}

interface ZendeskUser {
  id: number;
  name: string;
  email?: string;
  role?: string;
  created_at: string;
  updated_at: string;
}

interface ZendeskUsersResponse {
  users: ZendeskUser[];
}

interface ZendeskBrand {
  id: number;
  name: string;
  subdomain: string;
  active: boolean;
  default: boolean;
  created_at: string;
  updated_at: string;
}

interface ZendeskBrandsResponse {
  brands: ZendeskBrand[];
}

interface ZendeskCustomRole {
  id: number;
  name: string;
  description: string;
  role_type: number;
  team_member_count: number;
  created_at: string;
  updated_at: string;
}

interface ZendeskCustomRolesResponse {
  custom_roles: ZendeskCustomRole[];
}

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

interface ZendeskGroupsResponse {
  groups: ZendeskGroup[];
}

export const ticketIdDropdown =  Property.Dropdown({
  auth: zendeskAuth,
  displayName: 'Ticket',
  description: 'Select the ticket to work with',
  required: true,
  refreshers: ['auth'],
  options: async (propsValue) => {
    const auth = propsValue.auth;
    if (!auth) {
      return {
        disabled: true,
        options: [],
        placeholder: 'Connect your Zendesk account first',
      };
    }

    try {
      const response = await httpClient.sendRequest<ZendeskTicketsResponse>({
        url: `${getZendeskBaseUrl(auth)}/tickets.json?per_page=100`,
        method: HttpMethod.GET,
        authentication: getZendeskAuthentication(auth),
      });

      const tickets = response.body.tickets;

      return {
        disabled: false,
        options: tickets.map((ticket) => ({
          label: `#${ticket.id} - ${ticket.subject} (${ticket.status})`,
          value: ticket.id.toString(),
        })),
        placeholder:
          tickets.length === 0 ? 'No tickets available' : 'Select a ticket',
      };
    } catch (error: unknown) {
      const errorMessage =
        error instanceof Error ? error.message : String(error);
      return {
        disabled: true,
        options: [],
        placeholder: `Error loading tickets: ${errorMessage}`,
      };
    }
  },
});

export const organizationIdDropdown =  Property.Dropdown({
auth: zendeskAuth,

  displayName: 'Organization',
  description: 'Select the organization to work with',
  required: true,
  refreshers: ['auth'],
  options: async (propsValue) => {
    const auth = propsValue.auth;
    if (!auth) {
      return {
        disabled: true,
        options: [],
        placeholder: 'Connect your Zendesk account first',
      };
    }

    try {
      const response =
        await httpClient.sendRequest<ZendeskOrganizationsResponse>({
          url: `${getZendeskBaseUrl(auth)}/organizations.json?per_page=100`,
          method: HttpMethod.GET,
          authentication: getZendeskAuthentication(auth),
        });

      const organizations = response.body.organizations;

      return {
        disabled: false,
        options: organizations.map((org) => ({
          label: `${org.name} (ID: ${org.id})`,
          value: org.id.toString(),
        })),
        placeholder:
          organizations.length === 0
            ? 'No organizations available'
            : 'Select an organization',
      };
    } catch (error: unknown) {
      const errorMessage =
        error instanceof Error ? error.message : String(error);
      return {
        disabled: true,
        options: [],
        placeholder: `Error loading organizations: ${errorMessage}`,
      };
    }
  },
});

export const userIdDropdown =  Property.Dropdown({
auth: zendeskAuth,

  displayName: 'User',
  description: 'Select the user to work with',
  required: true,
  refreshers: ['auth'],
  options: async (propsValue) => {
    const auth = propsValue.auth;
    if (!auth) {
      return {
        disabled: true,
        options: [],
        placeholder: 'Connect your Zendesk account first',
      };
    }

    try {
      const response = await httpClient.sendRequest<ZendeskUsersResponse>({
        url: `${getZendeskBaseUrl(auth)}/users.json?per_page=100`,
        method: HttpMethod.GET,
        authentication: getZendeskAuthentication(auth),
      });

      const users = response.body.users;

      return {
        disabled: false,
        options: users.map((user) => ({
          label: `${user.name}${user.email ? ` (${user.email})` : ''} - ID: ${
            user.id
          }${user.role ? ` [${user.role}]` : ''}`,
          value: user.id.toString(),
        })),
        placeholder:
          users.length === 0 ? 'No users available' : 'Select a user',
      };
    } catch (error: unknown) {
      const errorMessage =
        error instanceof Error ? error.message : String(error);
      return {
        disabled: true,
        options: [],
        placeholder: `Error loading users: ${errorMessage}`,
      };
    }
  },
});

export const brandIdDropdown =  Property.Dropdown({
auth: zendeskAuth,

  displayName: 'Brand',
  description: 'Select the brand to work with',
  required: false,
  refreshers: ['auth'],
  options: async (propsValue) => {
    const auth = propsValue.auth;
    if (!auth) {
      return {
        disabled: true,
        options: [],
        placeholder: 'Connect your Zendesk account first',
      };
    }

    try {
      const response = await httpClient.sendRequest<ZendeskBrandsResponse>({
        url: `${getZendeskBaseUrl(auth)}/brands.json`,
        method: HttpMethod.GET,
        authentication: getZendeskAuthentication(auth),
      });

      const brands = response.body.brands;

      return {
        disabled: false,
        options: brands
          .filter((brand) => brand.active)
          .map((brand) => ({
            label: `${brand.name}${brand.default ? ' (Default)' : ''} - ${
              brand.subdomain
            }`,
            value: brand.id.toString(),
          })),
        placeholder:
          brands.length === 0 ? 'No brands available' : 'Select a brand',
      };
    } catch (error: unknown) {
      const errorMessage =
        error instanceof Error ? error.message : String(error);
      return {
        disabled: true,
        options: [],
        placeholder: `Error loading brands: ${errorMessage}`,
      };
    }
  },
});

export const problemTicketIdDropdown =  Property.Dropdown({
auth: zendeskAuth,

  displayName: 'Problem Ticket',
  description: 'Select the problem ticket this ticket is an incident of',
  required: false,
  refreshers: ['auth'],
  options: async (propsValue) => {
    const auth = propsValue.auth;
    if (!auth) {
      return {
        disabled: true,
        options: [],
        placeholder: 'Connect your Zendesk account first',
      };
    }

    try {
      const response = await httpClient.sendRequest<ZendeskTicketsResponse>({
        url: `${getZendeskBaseUrl(auth)}/tickets.json?per_page=100`,
        method: HttpMethod.GET,
        authentication: getZendeskAuthentication(auth),
      });

      const tickets = response.body.tickets;

      const problemTickets = tickets.filter(
        (ticket) => ticket.type === 'problem'
      );

      return {
        disabled: false,
        options: problemTickets.map((ticket) => ({
          label: `#${ticket.id} - ${ticket.subject} (${ticket.status})`,
          value: ticket.id.toString(),
        })),
        placeholder:
          problemTickets.length === 0
            ? 'No problem tickets available'
            : 'Select a problem ticket',
      };
    } catch (error: unknown) {
      const errorMessage =
        error instanceof Error ? error.message : String(error);
      return {
        disabled: true,
        options: [],
        placeholder: `Error loading problem tickets: ${errorMessage}`,
      };
    }
  },
});

export const customRoleIdDropdown =  Property.Dropdown({
auth: zendeskAuth,

  displayName: 'Custom Role',
  description: 'Select the custom role for the agent',
  required: false,
  refreshers: ['auth'],
  options: async (propsValue) => {
    const auth = propsValue.auth;
    if (!auth) {
      return {
        disabled: true,
        options: [],
        placeholder: 'Connect your Zendesk account first',
      };
    }

    try {
      const response = await httpClient.sendRequest<ZendeskCustomRolesResponse>({
        url: `${getZendeskBaseUrl(auth)}/custom_roles.json`,
        method: HttpMethod.GET,
        authentication: getZendeskAuthentication(auth),
      });

      const customRoles = response.body.custom_roles;

      return {
        disabled: false,
        options: customRoles.map((role) => ({
          label: `${role.name} - ${role.description} (${role.team_member_count} members)`,
          value: role.id.toString(),
        })),
        placeholder:
          customRoles.length === 0 ? 'No custom roles available' : 'Select a custom role',
      };
    } catch (error: unknown) {
      const errorMessage =
        error instanceof Error ? error.message : String(error);
      return {
        disabled: true,
        options: [],
        placeholder: `Error loading custom roles: ${errorMessage}`,
      };
    }
  },
});

export const agentBrandIdDropdown = Property.MultiSelectDropdown({
  auth: zendeskAuth,
  displayName: 'Agent Brand Access',
  description: 'Select the brands that the agent can access (for agents only)',
  required: false,
  refreshers: ['auth'],
  options: async (propsValue) => {
    const auth = propsValue.auth;
    if (!auth) {
      return {
        disabled: true,
        options: [],
        placeholder: 'Connect your Zendesk account first',
      };
    }

    try {
      const response = await httpClient.sendRequest<ZendeskBrandsResponse>({
        url: `${getZendeskBaseUrl(auth)}/brands.json`,
        method: HttpMethod.GET,
        authentication: getZendeskAuthentication(auth),
      });

      const brands = response.body.brands;

      return {
        disabled: false,
        options: brands
          .filter((brand) => brand.active) // Only show active brands
          .map((brand) => ({
            label: `${brand.name}${brand.default ? ' (Default)' : ''} - ${brand.subdomain}`,
            value: brand.id.toString(),
          })),
        placeholder:
          brands.length === 0 ? 'No brands available' : 'Select brands for agent access',
      };
    } catch (error: unknown) {
      const errorMessage =
        error instanceof Error ? error.message : String(error);
      return {
        disabled: true,
        options: [],
        placeholder: `Error loading brands: ${errorMessage}`,
      };
    }
  },
});

export const userFieldsDynamicProp = Property.DynamicProperties({
  auth: zendeskAuth,
  displayName: 'User Fields',
  description: 'Custom user field values.',
  required: false,
  refreshers: ['auth'],
  props: async ({ auth }) => {
    if (!auth) return {};

    try {
      const response = await httpClient.sendRequest({
        url: `${getZendeskBaseUrl(auth)}/user_fields.json`,
        method: HttpMethod.GET,
        authentication: getZendeskAuthentication(auth),
      });

      const fields = (response.body as {
        user_fields: Array<{
          key: string;
          title: string;
          description?: string;
          type: string;
          active: boolean;
          custom_field_options?: Array<{ name: string; value: string }>;
          regexp_for_validation?: string;
        }>;
      }).user_fields;

      const dynamicProps: DynamicPropsValue = {};

      for (const field of fields) {
        if (!field.active) continue;

        const fieldKey = `field_${field.key}`;
        const displayName = field.title;
        const description = field.description ?? `Custom ${field.type} field`;

        switch (field.type) {
          case 'tagger':
          case 'dropdown':
            if (field.custom_field_options && field.custom_field_options.length > 0) {
              dynamicProps[fieldKey] = Property.StaticDropdown({
                displayName,
                description,
                required: false,
                options: {
                  disabled: false,
                  placeholder: `Select ${displayName}`,
                  options: field.custom_field_options.map((option) => ({
                    label: option.name,
                    value: option.value,
                  })),
                },
              });
            }
            break;
          case 'multiselect':
            if (field.custom_field_options && field.custom_field_options.length > 0) {
              dynamicProps[fieldKey] = Property.StaticMultiSelectDropdown({
                displayName,
                description,
                required: false,
                options: {
                  options: field.custom_field_options.map((option) => ({
                    label: option.name,
                    value: option.value,
                  })),
                },
              });
            }
            break;
          case 'integer':
          case 'decimal':
            dynamicProps[fieldKey] = Property.Number({
              displayName,
              description,
              required: false,
            });
            break;
          case 'date':
            dynamicProps[fieldKey] = Property.DateTime({
              displayName,
              description,
              required: false,
            });
            break;
          case 'checkbox':
            dynamicProps[fieldKey] = Property.Checkbox({
              displayName,
              description,
              required: false,
            });
            break;
          case 'textarea':
            dynamicProps[fieldKey] = Property.LongText({
              displayName,
              description,
              required: false,
            });
            break;
          case 'regexp':
            dynamicProps[fieldKey] = Property.ShortText({
              displayName,
              description: `${description}${field.regexp_for_validation ? ` (Pattern: ${field.regexp_for_validation})` : ''}`,
              required: false,
            });
            break;
          default:
            dynamicProps[fieldKey] = Property.ShortText({
              displayName,
              description,
              required: false,
            });
        }
      }

      return dynamicProps;
    } catch {
      return {};
    }
  },
});

export const groupIdDropdown =  Property.Dropdown({
auth: zendeskAuth,

  displayName: 'Group',
  description: 'Select the group to assign',
  required: false,
  refreshers: ['auth'],
  options: async (propsValue) => {
    const auth = propsValue.auth;
    if (!auth) {
      return {
        disabled: true,
        options: [],
        placeholder: 'Connect your Zendesk account first',
      };
    }

    try {
      const response = await httpClient.sendRequest<ZendeskGroupsResponse>({
        url: `${getZendeskBaseUrl(auth)}/groups.json?per_page=100`,
        method: HttpMethod.GET,
        authentication: getZendeskAuthentication(auth),
      });

      const groups = response.body.groups;

      return {
        disabled: false,
        options: groups.map((group) => ({
          label: `${group.name}${group.is_public ? ' (Public)' : ' (Private)'} - ID: ${group.id}`,
          value: group.id.toString(),
        })),
        placeholder:
          groups.length === 0 ? 'No groups available' : 'Select a group',
      };
    } catch (error: unknown) {
      const errorMessage =
        error instanceof Error ? error.message : String(error);
      return {
        disabled: true,
        options: [],
        placeholder: `Error loading groups: ${errorMessage}`,
      };
    }
  },
});