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

3.4 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 { userIdDropdown } from '../common/props';
import { deleteUserOutputSchema } from '../output-schemas';

export const deleteUserAction = createAction({
  auth: zendeskAuth,
  name: 'delete-user',
  outputSchema: deleteUserOutputSchema,
  classification: 'DESTRUCTIVE',
  displayName: 'Delete User',
  description: 'Remove a user and associated records from the account.',
  audience: 'human',
  aiMetadata: { description: 'Soft-deletes a user identified by user ID from the Zendesk account; deleted users are not recoverable in the UI (use a permanent/GDPR erasure endpoint for full removal). Use to offboard or remove an account. Requires the confirmation checkbox to be set, and the account owner or restricted users cannot be deleted. Idempotent in effect: once removed the user stays removed, though a repeat call on an already-deleted user returns a not-found error.', idempotent: true },
  props: {
    user_id: userIdDropdown,
    confirmation: Property.Checkbox({
      displayName: 'Confirm Deletion',
      description: 'I understand that deleted users are not recoverable and this action cannot be undone.',
      required: true,
    }),
  },
  async run({ propsValue, auth }) {
    const authentication = auth;
    const {
      user_id,
      confirmation,
    } = propsValue;

    if (!confirmation) {
      throw new Error('You must confirm that you understand this action cannot be undone.');
    }

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

      return {
        success: true,
        message: `User ${user_id} has been deleted`,
        data: response.body,
        warning: "Deleted users are not recoverable in Zendesk UI. For GDPR erasure, use 'Permanently Delete User' if required.",
        note: 'To comply with GDPR, you may need to use the "Permanently Delete User" endpoint for complete data removal.',
      };
    } catch (error) {
      const errorMessage = (error as Error).message;
      if (errorMessage.includes('400')) {
        throw new Error(
          'Invalid request parameters. Please check the user ID and try again.'
        );
      }
      
      if (errorMessage.includes('401') || errorMessage.includes('403')) {
        throw new Error(
          'Authentication failed or insufficient permissions. Please check your API credentials and permissions to manage users.'
        );
      }
      
      if (errorMessage.includes('404')) {
        throw new Error(
          `User with ID ${user_id} not found. Please verify the user ID.`
        );
      }
      
      if (errorMessage.includes('422')) {
        throw new Error(
          'Cannot delete this user. The user may be the account owner or have restrictions preventing deletion.'
        );
      }
      
      if (errorMessage.includes('429')) {
        throw new Error(
          'Rate limit exceeded. Please wait a moment before trying again.'
        );
      }

      throw new Error(`Failed to delete user: ${errorMessage}`);
    }
  },
});