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.3 KB
Raw Blame
import { HttpMethod } from '@activepieces/pieces-common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { sendpulseApiCall } from '../common/client';
import { sendpulseAuth } from '../common/auth';
import { mailingListDropdown } from '../common/props';

export const unsubscribeUserAction = createAction({
  auth: sendpulseAuth,
  name: 'unsubscribe-user',
  classification: 'DESTRUCTIVE',
  displayName: 'Unsubscribe User',
  description: 'Remove subscribers from mailing list',
  audience: 'both',
  aiMetadata: { description: 'Unsubscribes one or more email addresses (up to 100 per call) from a single specified SendPulse mailing list, leaving them in other lists. Use to opt contacts out of one list rather than purge them account-wide. Idempotent — repeating leaves the same contacts unsubscribed from that list.', idempotent: true },
  props: {
    mailingListId: mailingListDropdown,
    emails: Property.Array({
      displayName: 'Email Addresses',
      description: 'Email addresses to unsubscribe (max 100)',
      required: true,
    }),
  },

  async run(context) {
    const { mailingListId, emails } = context.propsValue;

    if (emails.length === 0) {
      throw new Error('At least one email address is required');
    }

    if (emails.length > 100) {
      throw new Error('Maximum 100 email addresses allowed per request');
    }

    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    const invalidEmails = emails.filter(email => !emailRegex.test(email as string));
    
    if (invalidEmails.length > 0) {
      throw new Error(`Invalid email format(s): ${invalidEmails.join(', ')}`);
    }

    const body = { emails };

    try {
      const result = await sendpulseApiCall<{ result: boolean }>({
        method: HttpMethod.DELETE,
        auth: context.auth.props,
        resourceUri: `/addressbooks/${mailingListId}/emails`,
        body,
      });

      if (result.result) {
        return {
          success: true,
          message: `${emails.length} subscriber(s) unsubscribed successfully`,
          unsubscribed: emails,
          mailingListId,
          count: emails.length,
        };
      } else {
        throw new Error('Unsubscription failed - API returned failure');
      }
    } catch (error: any) {
      throw new Error(`Failed to unsubscribe users: ${error.message || 'Unknown error'}`);
    }
  },
});