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.6 KB
Raw Blame
import { getHubspotAccessToken, hubspotAuth } from '../auth';

import { Property, createAction } from '@activepieces/pieces-framework';

import { MarkdownVariant } from '@activepieces/pieces-framework';
import { OBJECT_TYPE } from '../common/constants';
import {
	getDefaultPropertiesForObject,
	pipelineDropdown,
	pipelineStageDropdown,
	standardObjectDynamicProperties,
	standardObjectPropertiesDropdown,
} from '../common/props';

import { Client } from '@hubspot/api-client';
import { crmObjectOutputSchema } from '../output-schemas';

export const createDealAction = createAction({
	auth: hubspotAuth,
	name: 'create-deal',
	classification: 'WRITE',
	displayName: 'Create Deal',
	description: 'Creates a deal in HubSpot.',
	audience: 'both',
	aiMetadata: { description: 'Creates a new deal in HubSpot, requiring a deal name plus a pipeline and stage, and returns the created deal. Use to open a new opportunity; use Update Deal to modify an existing one. Not idempotent: each call creates a separate deal, so guard against duplicates.', idempotent: false },
	outputSchema: crmObjectOutputSchema,
	propertyGroups: [
		{
			key: 'deal',
			display: 'section',
			label: 'Deal',
			icon: 'tag',
			props: ['dealname'],
		},
		{
			key: 'pipeline',
			display: 'section',
			label: 'Pipeline',
			icon: 'sliders',
			props: ['pipelineId', 'pipelineStageId'],
		},
		{
			key: 'details',
			display: 'section',
			label: 'Details',
			icon: 'text',
			props: ['objectProperties'],
		},
	],
	props: {
		dealname: Property.ShortText({
			displayName: 'Deal Name',
			required: true,
		}),
		pipelineId: pipelineDropdown({
			objectType: OBJECT_TYPE.DEAL,
			displayName: 'Deal Pipeline',
			required: true,
		}),
		pipelineStageId: pipelineStageDropdown({
			objectType: OBJECT_TYPE.DEAL,
			displayName: 'Deal Stage',
			required: true,
		}),
		objectProperties: standardObjectDynamicProperties(OBJECT_TYPE.DEAL, [
			'dealname',
			'pipeline',
			'dealstage',
		]),
		markdown: Property.MarkDown({
			variant: MarkdownVariant.INFO,
			value: `Returned by default: dealtype, dealname, amount, description, closedate, createdate, num_associated_contacts, hs_forecast_amount, hs_forecast_probability, hs_manual_forecast_category, hs_next_step, hs_object_id, hs_lastmodifieddate, hubspot_owner_id, hubspot_team_id.

Pick more under **Advanced**.`,
		}),
		additionalPropertiesToRetrieve: standardObjectPropertiesDropdown({
			objectType: OBJECT_TYPE.DEAL,
			displayName: 'Additional Properties to Retrieve',
			required: false,
			advanced: true,
		}),
	},
	async run(context) {
		const { dealname, pipelineId, pipelineStageId } = context.propsValue;
		const objectProperties = context.propsValue.objectProperties ?? {};
		const additionalPropertiesToRetrieve = context.propsValue.additionalPropertiesToRetrieve ?? [];

		const dealProperties: Record<string, string> = {
			dealname,
			pipeline: pipelineId!,
			dealstage: pipelineStageId!,
		};

		// Add additional properties to the dealProperties object
		Object.entries(objectProperties).forEach(([key, value]) => {
			// Format values if they are arrays
			dealProperties[key] = Array.isArray(value) ? value.join(';') : value;
		});

		const client = new Client({ accessToken: getHubspotAccessToken(context.auth) });

		const createdDeal = await client.crm.deals.basicApi.create({
			properties: dealProperties,
		});
		// Retrieve default properties for the deal and merge with additional properties to retrieve
		const defaultDealProperties = getDefaultPropertiesForObject(OBJECT_TYPE.DEAL);

		const dealDetails = await client.crm.deals.basicApi.getById(createdDeal.id, [
			...defaultDealProperties,
			...additionalPropertiesToRetrieve,
		]);

		return dealDetails;
	},
});