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.4 KB
Raw Blame
import { getHubspotAccessToken, hubspotAuth } from '../auth';
import { createAction, Property } from '@activepieces/pieces-framework';
import { staticListsDropdown } from '../common/props';
import { HttpMethod } from '@activepieces/pieces-common';
import { Client } from '@hubspot/api-client';
import { FilterOperatorEnum, HubSpotAddContactsToListResponse } from '../common/types';

export const removeContactFromListAction = createAction({
	auth: hubspotAuth,
	name: 'remove-contact-from-list',
	classification: 'WRITE',
	displayName: 'Remove Contact from List',
	description: 'Removes a contact from a static list by email.',
	audience: 'both',
	aiMetadata: {
		description:
			'Remove a contact, identified by email, from a specific static HubSpot list (this only works on static lists, not active/dynamic ones). Use to revoke list membership; it first resolves the email to a contact and errors if no such contact exists or if the contact was not a member of the list.',
		idempotent: false,
	},
	propertyGroups: [
		{
			key: 'list',
			display: 'section',
			label: 'List',
			icon: 'users',
			props: ['listId'],
		},
		{
			key: 'contact',
			display: 'section',
			label: 'Contact',
			icon: 'user',
			props: ['email'],
		},
	],
	props: {
		listId: staticListsDropdown,
		email: Property.ShortText({
			displayName: 'Contact Email',
			placeholder: '[email protected]',
			required: true,
		}),
	},
	async run(context) {
		const { listId, email } = context.propsValue;

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

		const contact = await client.crm.contacts.searchApi.doSearch({
			limit: 1,
			filterGroups: [
				{ filters: [{ propertyName: 'email', operator: FilterOperatorEnum.Eq, value: email }] },
			],
		});
		if (contact.results.length === 0) {
			throw new Error(
				`No contact with email '${email}' was found. Unable to remove unknown contact from list.`,
			);
		}
		const apiResponse = await client.apiRequest({
			path: `/contacts/v1/lists/${listId}/remove`,
			method: HttpMethod.POST,
			body: { vids: [contact.results[0].id] },
			defaultJson: true,
		});

		const parsedResponse =(await apiResponse.json()) as HubSpotAddContactsToListResponse;
		if(parsedResponse.updated.length === 0) {
			throw new Error(
				`Contact with email '${email}' wasn't a member of list with id '${listId}'.`,
			);
		}
        return parsedResponse;
	},
});