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

1.5 KB
Raw Blame
import { zagomailAuth } from '../auth';
import { createAction } from '@activepieces/pieces-framework';
import { zagoMailApiService } from '../common/request';
import { listFields, listUId } from '../common/props';
import { isNil } from '@activepieces/pieces-framework';
import dayjs from 'dayjs';

export const createSubscriber = createAction({
	auth: zagomailAuth,
	name: 'createSubscriber',
	displayName: 'Create Subscriber',
	description: 'Creates a new subscriber in a list.',
	audience: 'both',
	aiMetadata: { description: 'Adds a new subscriber to a specific Zagomail list, identified by its list UID, populating the list custom fields you provide. Use to enroll a contact into a mailing list. Not idempotent: each call creates another subscriber record, so guard against duplicates if re-running.', idempotent: false },
	props: {
		listUId: listUId,
		fields: listFields(true),
	},
	async run({ propsValue, auth }) {
		const listUId = propsValue.listUId;
		const listFields = propsValue.fields ?? {};

		const payload: Record<string, any> = {};

		for (const [key, value] of Object.entries(listFields)) {
			if (isNil(value) || value === '') continue;

			const [field, type] = key.split(':::');

			let formattedValue = value;

			if (type === 'Date') {
				formattedValue = dayjs(value).format('YYYY-MM-DD');
			} else if (type === 'Datetime') {
				formattedValue = dayjs(value).format('YYYY-MM-DD HH:mm:ss');
			}

			payload[field] = formattedValue;
		}

		return await zagoMailApiService.createSubscriber(auth.secret_text, listUId, payload);
	},
});