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.7 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod, httpClient } from '@activepieces/pieces-common';
import { ReachinboxAuth } from '../..';
import { reachinboxCommon } from '../common';

// Define the shape of the account object
interface EmailAccount {
  id: number;
  email: string;
}

export const removeEmail = createAction({
  auth: ReachinboxAuth,
  name: 'removeEmail',
  classification: 'DESTRUCTIVE',
  displayName: 'Remove Email',
  description: 'Remove an email account from the system.',
  audience: 'both',
  aiMetadata: {
    description:
      'Deletes a connected email account from ReachInbox, identified by its account id. Use to permanently disconnect a mailbox. Idempotent on the id: once removed, repeating the call leaves the account absent.',
    idempotent: true,
  },
  props: {
    accountId: Property.Dropdown({
  auth: ReachinboxAuth,
      displayName: 'Select Email Account to Remove',
      description: 'Choose an email account to remove.',
      required: true,
      refreshers: ['auth'],
      options: async ({ auth }) => {
        try {
          // Fetch email accounts
          const response = await httpClient.sendRequest({
            method: HttpMethod.GET,
            url: `${reachinboxCommon.baseUrl}account/all`,
            headers: {
              Authorization: `Bearer ${auth}`,
            },
          });

          const accounts: EmailAccount[] =
            response.body?.data?.emailsConnected || [];

          // Map the fetched accounts to dropdown options
          const options = accounts.map((account: EmailAccount) => ({
            label: `${account.email} (ID: ${account.id})`,
            value: account.id.toString(),
          }));

          return {
            options,
            disabled: options.length === 0,
          };
        } catch (error) {
          console.error('Error fetching email accounts:', error);
          return { options: [], disabled: true };
        }
      },
    }),
  },
  async run(context) {
    const { accountId } = context.propsValue;

    try {
      const response = await httpClient.sendRequest({
        method: HttpMethod.DELETE,
        url: `${reachinboxCommon.baseUrl}account/delete/${accountId}`,
        headers: {
          Authorization: `Bearer ${context.auth.secret_text}`,
        },
      });

      if (response.status === 200) {
        return {
          success: true,
          message: `Email account with ID ${accountId} was successfully removed.`,
        };
      } else {
        throw new Error(`Failed to remove email account with ID: ${accountId}`);
      }
    } catch (error) {
      console.error('Error removing email account:', error);
      throw new Error(`Error removing email account with ID: ${accountId}`);
    }
  },
});