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.2 KB
Raw Blame
import {
  createAction,
  Property,
} from '@activepieces/pieces-framework';
import { subscribe } from '../api';
import { buildListDropdown } from '../props';
import { sendyAuth, SendyAuthType } from '../auth';
import * as z from 'zod/mini'
import { propsValidation } from '@activepieces/pieces-common';

export const subscribeMultipleAction = createAction({
  name: 'subscribe_multiple_lists',
  classification: 'WRITE',
  auth: sendyAuth,
  displayName: 'Subscribe Multiple Lists',
  description: 'Add a new subscriber to a multiple lists',
  audience: 'both',
  aiMetadata: { description: 'Adds the same subscriber (by email, plus optional name, country, IP, referrer) to several Sendy lists in one call, iterating over each selected list. Use when a contact should join multiple lists at once; for a single list use Subscribe instead. Honors each list\'s double opt-in unless the silent flag forces single opt-in. Effectively an upsert on the email, so re-running with the same input is safe.', idempotent: true },
  props: {
    lists: Property.MultiSelectDropdown({
      auth: sendyAuth,
      displayName: 'Lists',
      description: 'Select the lists to subscribe to',
      required: true,
      refreshers: ['auth'],
      options: async ({ auth }) =>
        await buildListDropdown(auth as SendyAuthType),
    }),
    email: Property.ShortText({
      displayName: 'Email',
      description: "The user's email",
      required: true,
    }),
    name: Property.ShortText({
      displayName: 'Name',
      description: "The user's name",
      required: false,
    }),
    country: Property.ShortText({
      displayName: 'Country',
      description: "The user's 2 letter country code",
      required: false,
    }),
    ipaddress: Property.ShortText({
      displayName: 'IP Address',
      description: "The user's IP address",
      required: false,
    }),
    referrer: Property.ShortText({
      displayName: 'Referrer',
      description: 'The URL where the user signed up from',
      required: false,
    }),
    gdpr: Property.Checkbox({
      displayName: 'GDPR compliant',
      description:
        "If you're signing up EU users in a GDPR compliant manner, set to true",
      required: false,
      defaultValue: true,
    }),
    silent: Property.Checkbox({
      displayName: 'Silent',
      description:
        "set to true if your list is 'Double opt-in' but you want to bypass that and signup the user to the list as 'Single Opt-in instead' ",
      required: false,
      defaultValue: false,
    }),
  },
  async run(context) {
    await propsValidation.validateZod(context.propsValue, {
      email: z.string().check(z.email()),
      referrer: z.optional(z.string().check(z.url())),
    });

    const returnValues: any[] = [];

    for (const list of context.propsValue.lists) {
      const rc = await subscribe(context.auth, {
        list: list,
        email: context.propsValue.email,
        name: context.propsValue.name,
        country: context.propsValue.country,
        ipaddress: context.propsValue.ipaddress,
        referrer: context.propsValue.referrer,
        gdpr: context.propsValue.gdpr,
        silent: context.propsValue.silent,
      });
      returnValues.push(rc);
    }
    return returnValues;
  },
});