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

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

const WEBHOOK_TRIGGER_KEY = 'zendesk_new_user_webhook';

interface ZendeskUser {
  id: number;
  name: string;
  email: string;
  created_at: string;
  updated_at: string;
  time_zone: string;
  phone?: string;
  shared_phone_number?: string;
  photo?: {
    url: string;
    id: number;
    file_name: string;
    content_url: string;
    content_type: string;
    size: number;
    width: number;
    height: number;
  };
  locale_id: number;
  locale: string;
  organization_id?: number;
  role: string;
  verified: boolean;
  external_id?: string;
  tags: string[];
  alias?: string;
  active: boolean;
  shared: boolean;
  shared_agent: boolean;
  last_login_at?: string;
  two_factor_auth_enabled: boolean;
  signature?: string;
  details?: string;
  notes?: string;
  role_type?: number;
  custom_role_id?: number;
  moderator: boolean;
  ticket_restriction: string;
  only_private_comments: boolean;
  restricted_agent: boolean;
  suspended: boolean;
  report_csv: boolean;
  user_fields: Record<string, unknown>;
}

export const newUser = createTrigger({
  name: 'new_user',
  outputSchema: newUserOutputSchema,
  classification: 'READ',
  displayName: 'New User',
  description: 'Fires when a new user is created. Uses Zendesk event webhook (no Trigger needed).',
  aiMetadata: {
    description: 'Fires when a new user is created in Zendesk. Represents a newly added account; can optionally be scoped to a single role (end-user, agent, or admin), otherwise fires for all roles. Uses a Zendesk event-type webhook registered automatically, so no manual Zendesk Trigger setup is needed.',
  },
  auth: zendeskAuth,
  props: {
    user_role: Property.StaticDropdown({
      displayName: 'User Role (Optional)',
      description: 'Filter users by role. Leave empty to trigger for all user types.',
      required: false,
      options: {
        disabled: false,
        placeholder: 'Select a user role (optional)',
        options: [
          { label: 'All Roles', value: 'all' },
          { label: 'End User', value: 'end-user' },
          { label: 'Agent', value: 'agent' },
          { label: 'Admin', value: 'admin' },
        ],
      },
    }),
  },
  type: TriggerStrategy.WEBHOOK,
  sampleData: {
    id: 12345,
    url: 'https://example.zendesk.com/api/v2/users/12345.json',
    name: 'John Doe',
    email: '[email protected]',
    created_at: '2023-03-25T02:39:41Z',
    updated_at: '2023-03-25T02:39:41Z',
    time_zone: 'America/New_York',
    phone: '+1-555-123-4567',
    locale_id: 1,
    locale: 'en-US',
    organization_id: 67890,
    role: 'end-user',
    verified: true,
    external_id: 'user-001',
    tags: ['new_customer'],
    active: true,
    shared: false,
    shared_agent: false,
    two_factor_auth_enabled: false,
    moderator: false,
    ticket_restriction: 'requested',
    only_private_comments: false,
    restricted_agent: false,
    suspended: false,
    report_csv: false,
    user_fields: {
      department: 'Sales',
      company_size: 'Medium',
    },
  },
  async onEnable(context) {
    const authentication = context.auth;
    
    try {
      const response = await httpClient.sendRequest<{
        webhook: { id: string };
      }>({
        url: `${getZendeskBaseUrl(authentication)}/webhooks`,
        method: HttpMethod.POST,
        headers: {
          'Content-Type': 'application/json',
        },
        authentication: getZendeskAuthentication(authentication),
        body: {
          webhook: {
            name: `Activepieces New User Webhook - ${Date.now()}`,
            endpoint: context.webhookUrl,
            http_method: 'POST',
            request_format: 'json',
            status: 'active',
            subscriptions: ['zen:event-type:user.created'],
          },
        },
      });

      await context.store.put<string>(WEBHOOK_TRIGGER_KEY, response.body.webhook.id);
    } catch (error) {
      throw new Error(`Failed to register webhook: ${(error as Error).message}`);
    }
  },

  async onDisable(context) {
    const authentication = context.auth;
    const webhookId = await context.store.get<string>(WEBHOOK_TRIGGER_KEY);

    if (webhookId) {
      try {
        await httpClient.sendRequest({
          url: `${getZendeskBaseUrl(authentication)}/webhooks/${webhookId}`,
          method: HttpMethod.DELETE,
          authentication: getZendeskAuthentication(authentication),
        });
      } catch (error) {
        console.warn(`Warning: Failed to delete webhook ${webhookId}:`, (error as Error).message);
      } finally {
        await context.store.delete(WEBHOOK_TRIGGER_KEY);
      }
    }
  },

  async run(context) {
    const payload = context.payload.body as {
      type?: string;
      user?: ZendeskUser;
      detail?: ZendeskUser;
      'zen:body'?: { user?: ZendeskUser };
    };

    const user = payload.user || payload['zen:body']?.user || payload.detail;
    if (!user) {
      return [];
    }

    const userRole = context.propsValue.user_role;
    if (userRole && userRole !== 'all') {
      if (user.role !== userRole) {
        return [];
      }
    }

    return [user];
  },
});