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.0 KB
Raw Blame
import { getHubspotAccessToken, HubspotAuthValue, hubspotAuth } from '../auth';
import {
	createAction,
	DropdownOption,
	Property,
} from '@activepieces/pieces-framework';
import { Client } from '@hubspot/api-client';
import { uploadFileOutputSchema } from '../output-schemas';

export const uploadFileAction = createAction({
	auth: hubspotAuth,
	name: 'upload-file',
	classification: 'WRITE',
	displayName: 'Upload File',
	description: 'Uploads a file to HubSpot File Manager.',
	audience: 'both',
	aiMetadata: { description: 'Upload a file into a chosen folder in the HubSpot File Manager with a given name and access level. Each call uploads a new file rather than replacing an existing one, so it is not idempotent.', idempotent: false },
	outputSchema: uploadFileOutputSchema,
	propertyGroups: [
		{
			key: 'file',
			display: 'section',
			label: 'File',
			icon: 'paperclip',
			props: ['file', 'fileName'],
		},
		{
			key: 'destination',
			display: 'section',
			label: 'Save To',
			icon: 'inbox',
			props: ['folderId', 'accessLevel'],
		},
	],
	props: {
		folderId: Property.Dropdown({
			auth: hubspotAuth,
			displayName: 'Folder',
			refreshers: [],
			required: true,
			options: async ({ auth }) => {
				if (!auth) {
					return {
						disabled: true,
						options: [],
						placeholder: 'Please connect your account.',
					};
				}

				const authValue = auth as HubspotAuthValue;
				const client = new Client({ accessToken: getHubspotAccessToken(authValue) });

				const limit = 100;
				const options: DropdownOption<string>[] = [];
				let after: string | undefined;

				do {
					const response = await client.files.foldersApi.doSearch(
						undefined,
						after,
						undefined,
						limit,
					);

					for (const folder of response.results) {
						options.push({
							value: folder.id,
							label: folder.name ?? folder.id,
						});
					}
					after = response.paging?.next?.after;
				} while (after);

				return {
					disabled: false,
					options,
				};
			},
		}),
		fileName: Property.ShortText({
			displayName: 'File Name',
			placeholder: 'report.pdf',
			required: true,
		}),
		accessLevel: Property.StaticDropdown({
			displayName: 'Access Level',
			description: 'Indexable files can appear in search engine results.',
			required: true,
			options: {
				disabled: false,
				options: [
					{
						value: 'PUBLIC_INDEXABLE',
						label: 'Public and indexable',
					},
					{
						value: 'PUBLIC_NOT_INDEXABLE',
						label: 'Public but not indexable',
					},
					{ label: 'Private', value: 'PRIVATE' },
				],
			},
		}),
		file: Property.File({
			displayName: 'File',
			required: true,
		}),
	},
	async run(context) {
		const { accessLevel, fileName, folderId, file } = context.propsValue;
		const client = new Client({ accessToken: getHubspotAccessToken(context.auth) });

		const response = await client.files.filesApi.upload(
			{
				name: fileName,
				data: file.data,
			},
			folderId,
			undefined,
			fileName,
			undefined,
			JSON.stringify({
				access: accessLevel,
			}),
		);

        return response;
	},
});