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

4.7 KB
Raw Blame
import { HttpMethod } from '@activepieces/pieces-common';
import { Property } from '@activepieces/pieces-framework';
import { sendinblueAuth } from '../auth';
import { brevoCommon } from '.';

const PAGE_SIZE = 50;

async function fetchAllLists(apiKey: string): Promise<BrevoList[]> {
	const lists: BrevoList[] = [];
	let offset = 0;
	let hasMore = true;

	while (hasMore) {
		const response = await brevoCommon.apiCall<BrevoListsResponse>({
			apiKey,
			method: HttpMethod.GET,
			resourceUri: '/contacts/lists',
			query: { limit: PAGE_SIZE, offset },
		});

		const page = response.lists ?? [];
		lists.push(...page);
		offset += PAGE_SIZE;
		hasMore = page.length === PAGE_SIZE;
	}

	return lists;
}

async function fetchAllTemplates(apiKey: string): Promise<BrevoTemplate[]> {
	const templates: BrevoTemplate[] = [];
	let offset = 0;
	let hasMore = true;

	while (hasMore) {
		const response = await brevoCommon.apiCall<BrevoTemplatesResponse>({
			apiKey,
			method: HttpMethod.GET,
			resourceUri: '/smtp/templates',
			query: { limit: PAGE_SIZE, offset },
		});

		const page = response.templates ?? [];
		templates.push(...page);
		offset += PAGE_SIZE;
		hasMore = page.length === PAGE_SIZE;
	}

	return templates;
}

function connectFirst() {
	return {
		disabled: true,
		placeholder: 'Connect your Brevo account first.',
		options: [],
	};
}

function loadFailed(noun: string) {
	return {
		disabled: false,
		options: [],
		placeholder: `Could not load ${noun}. Check your connection.`,
	};
}

function noneFound(noun: string) {
	return {
		disabled: false,
		options: [],
		placeholder: `No ${noun} found. Create one in Brevo first.`,
	};
}

export const brevoProps = {
	listIds: ({ displayName, description }: ListIdsPropParams) =>
		Property.MultiSelectDropdown({
			displayName,
			description,
			required: false,
			auth: sendinblueAuth,
			refreshers: [],
			options: async ({ auth }) => {
				if (!auth) {
					return connectFirst();
				}

				try {
					const lists = await fetchAllLists(auth.secret_text);

					if (lists.length === 0) {
						return noneFound('lists');
					}

					return {
						disabled: false,
						options: lists.map((list) => ({
							label: list.name,
							value: String(list.id),
						})),
					};
				} catch {
					return loadFailed('lists');
				}
			},
		}),
	optionalBoolean: ({ displayName, description }: OptionalBooleanPropParams) =>
		Property.StaticDropdown<string, false>({
			displayName,
			description,
			required: false,
			options: {
				disabled: false,
				options: [
					{ label: 'Yes', value: 'yes' },
					{ label: 'No', value: 'no' },
					{ label: 'Leave unchanged', value: 'unchanged' },
				],
			},
		}),
	senderEmail: Property.Dropdown({
		displayName: 'Sender',
		description: 'The verified sender the email is sent from.',
		required: false,
		auth: sendinblueAuth,
		refreshers: [],
		options: async ({ auth }) => {
			if (!auth) {
				return connectFirst();
			}

			try {
				const response = await brevoCommon.apiCall<BrevoSendersResponse>({
					apiKey: auth.secret_text,
					method: HttpMethod.GET,
					resourceUri: '/senders',
				});

				const senders = response.senders ?? [];

				if (senders.length === 0) {
					return noneFound('senders');
				}

				return {
					disabled: false,
					options: senders.map((sender) => ({
						label: sender.name ? `${sender.name} <${sender.email}>` : sender.email,
						value: sender.email,
					})),
				};
			} catch {
				return loadFailed('senders');
			}
		},
	}),
	emailTemplateId: Property.Dropdown({
		displayName: 'Template',
		description:
			'Send a saved Brevo template instead of supplying subject and content.',
		required: false,
		auth: sendinblueAuth,
		refreshers: [],
		options: async ({ auth }) => {
			if (!auth) {
				return connectFirst();
			}

			try {
				const templates = await fetchAllTemplates(auth.secret_text);

				if (templates.length === 0) {
					return noneFound('templates');
				}

				return {
					disabled: false,
					options: templates.map((template) => ({
						label: template.name,
						value: template.id,
					})),
				};
			} catch {
				return loadFailed('templates');
			}
		},
	}),
};

export type BrevoList = {
	id: number;
	name: string;
};

export type BrevoListsResponse = {
	lists: BrevoList[];
	count: number;
};

export type BrevoTemplate = {
	id: number;
	name: string;
};

export type BrevoTemplatesResponse = {
	templates: BrevoTemplate[];
	count: number;
};

export type BrevoSender = {
	id: number;
	name?: string;
	email: string;
};

export type BrevoSendersResponse = {
	senders: BrevoSender[];
};

export type ListIdsPropParams = {
	displayName: string;
	description: string;
};

export type OptionalBooleanPropParams = {
	displayName: string;
	description: string;
};