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.3 KB
Raw Blame
import { closeMappers } from '../common/mappers';
import { closeOutputSchemas } from '../common/output-schemas';
import { Property, createAction } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { closeAuth } from '../auth';
import { CloseCRMContact } from '../common/types';
import { customFields, leadId } from '../common/props';
import { CloseApiError, closeApiCall } from '../common/client';

export const createContact = createAction({
	auth: closeAuth,
	name: 'create_contact',
	classification: 'WRITE',
	displayName: 'Create Contact',
	description: 'Creates a new contact.',
	audience: 'human',
	aiMetadata: {
		description: 'Creates a new contact attached to an existing lead in Close CRM, with optional phones, emails, URL, and custom fields. Use to add a person to a known lead. Requires the target lead ID and a contact name. Not idempotent; each call creates a new contact and repeating produces duplicates.',
		idempotent: false,
	},
	outputSchema: closeOutputSchemas.raw.contact,
	props: {
		lead_id: leadId(),
		name: Property.ShortText({
			displayName: 'Name',
			required: true,
		}),
		title: Property.ShortText({
			displayName: 'Title',
			required: false,
		}),
		officePhone: Property.ShortText({
			displayName: 'Office Phone',
			required: false,
		}),
		mobilePhone: Property.ShortText({
			displayName: 'Mobile Phone',
			required: false,
		}),
		homePhone: Property.ShortText({
			displayName: 'Home Phone',
			required: false,
		}),
		directPhone: Property.ShortText({
			displayName: 'Direct Phone',
			required: false,
		}),
		faxPhone: Property.ShortText({
			displayName: 'Fax Phone',
			required: false,
		}),
		otherPhone: Property.ShortText({
			displayName: 'Other Phone',
			required: false,
		}),
		officeEmail: Property.ShortText({
			displayName: 'Office Email',
			required: false,
		}),
		homeEmail: Property.ShortText({
			displayName: 'Home Email',
			required: false,
		}),
		directEmail: Property.ShortText({
			displayName: 'Direct Email',
			required: false,
		}),
		otherEmail: Property.ShortText({
			displayName: 'Other Email',
			required: false,
		}),
		url: Property.ShortText({
			displayName: 'URL',
			required: false,
		}),
		customFields: customFields('contact'),
	},
	async run(context) {
		const {
			lead_id,
			name,
			title,
			officeEmail,
			officePhone,
			otherEmail,
			otherPhone,
			mobilePhone,
			homeEmail,
			url,
			homePhone,
			directEmail,
			directPhone,
			faxPhone,
		} = context.propsValue;

		const customFields = context.propsValue.customFields ?? {};

		const transformedCustomFields = Object.fromEntries(
			Object.entries(customFields)
				.filter(([, v]) => v !== '' && v != null && !(Array.isArray(v) && v.length === 0))
				.map(([key, value]) => [`custom.${key}`, value]),
		);

		const payload: Partial<CloseCRMContact> = {
			lead_id: lead_id as string,
			title: title,
			name: name,
			...transformedCustomFields,
			phones: [],
			emails: [],
			urls: [],
		};

		// Add emails if present
		if (officeEmail) payload.emails?.push({ email: officeEmail.trim(), type: 'office' });
		if (otherEmail) payload.emails?.push({ email: otherEmail.trim(), type: 'other' });
		if (homeEmail) payload.emails?.push({ email: homeEmail.trim(), type: 'home' });
		if (directEmail) payload.emails?.push({ email: directEmail.trim(), type: 'direct' });

		// Add phones if present
		if (officePhone) payload.phones?.push({ phone: officePhone.trim(), type: 'office' });
		if (otherPhone) payload.phones?.push({ phone: otherPhone.trim(), type: 'other' });
		if (mobilePhone) payload.phones?.push({ phone: mobilePhone.trim(), type: 'mobile' });
		if (homePhone) payload.phones?.push({ phone: homePhone.trim(), type: 'home' });
		if (directPhone) payload.phones?.push({ phone: directPhone.trim(), type: 'direct' });
		if (faxPhone) payload.phones?.push({ phone: faxPhone.trim(), type: 'fax' });

		if (url) payload.urls?.push({ url, type: 'url' });

		try {
			const response = await closeApiCall({
				accessToken: context.auth.secret_text,
				method: HttpMethod.POST,
				resourceUri: '/contact/',
				body: payload,
			});

			return closeMappers.withCustomMap({ objectType: 'contact', record: response });
		} catch (error) {
			if (error instanceof CloseApiError && error.status === 404) {
				throw new Error(`Lead not found with ID: ${lead_id}. ${error.message}`);
			}
			throw error;
		}
	},
});