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.0 KB
Raw Blame
import { HttpMethod } from '@activepieces/pieces-common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { sendinblueAuth } from '../auth';
import { brevoCommon } from '../common';
import { getImportProcessActionOutputSchema } from '../output-schemas';

export const getImportProcess = createAction({
	auth: sendinblueAuth,
	name: 'get_import_process',
	outputSchema: getImportProcessActionOutputSchema,
	classification: 'READ',
	displayName: 'Get Import Process',
	description: 'Check the status of a background process such as an import.',
	audience: 'ai',
	aiMetadata: {
		description:
			'Reads the current status of one Brevo background process by the id that Import Contacts returned. Status is one of queued, in_process, processing, completed, failed or cancelled; a completed import also carries links to CSV files listing invalid emails and duplicate records. This is a single status read, not a wait — call it again after a delay while the status is still queued, in_process or processing. Read-only and idempotent.',
		idempotent: true,
	},
	props: {
		process_id: Property.Number({
			displayName: 'Process ID',
			description: 'The process id returned by Import Contacts.',
			required: true,
		}),
	},
	async run(context) {
		const { process_id } = context.propsValue;

		const process = await brevoCommon.apiCall<ProcessResponse>({
			apiKey: context.auth.secret_text,
			method: HttpMethod.GET,
			resourceUri: `/processes/${process_id}`,
		});

		return {
			id: process.id ?? process_id,
			status: process.status,
			finished: isTerminalStatus(process.status),
			name: process.name,
			export_url: process.export_url,
			info: process.info,
		};
	},
});

function isTerminalStatus(status: string | undefined): boolean {
	return status !== undefined && TERMINAL_PROCESS_STATUSES.includes(status);
}

const TERMINAL_PROCESS_STATUSES: readonly string[] = ['completed', 'failed', 'cancelled'];

type ProcessResponse = {
	id?: number;
	status?: string;
	name?: string;
	export_url?: string;
	info?: { import?: Record<string, string | null> };
};