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.7 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { whatConvertsAuth } from '../common/auth';
import { whatConvertsProps } from '../common/props';
import { whatConvertsClient } from '../common/client';

export const findLeadAction = createAction({
  auth: whatConvertsAuth,
  name: 'find_lead',
  classification: 'SEARCH',
  displayName: 'Find Lead',
  description: 'Find a lead by various criteria.',
  audience: 'both',
  aiMetadata: { description: 'Searches WhatConverts leads filtered by email address, phone number, and/or lead type, returning matching leads. Use when an agent needs to look up leads matching specific contact or type criteria. Requires at least one of email, phone, or lead type. Read-only and idempotent.', idempotent: true },
  props: {
    account_id: whatConvertsProps.account_id(),
    profile_id: whatConvertsProps.profile_id(),
    email_address: Property.ShortText({
      displayName: 'Email Address',
      description: 'Find a lead by their email address.',
      required: false,
    }),
    phone_number: Property.ShortText({
      displayName: 'Phone Number',
      description: 'Find a lead by their E.164 formatted phone number.',
      required: false,
    }),
    lead_type: Property.StaticDropdown({
      displayName: 'Lead Type',
      description: 'Filter leads by their type.',
      required: false,
      options: {
        options: [
          { label: 'Appointment', value: 'appointment' },
          { label: 'Chat', value: 'chat' },
          { label: 'Email', value: 'email' },
          { label: 'Event', value: 'event' },
          { label: 'Other', value: 'other' },
          { label: 'Phone Call', value: 'phone_call' },
          { label: 'Text Message', value: 'text_message' },
          { label: 'Transaction', value: 'transaction' },
          { label: 'Web Form', value: 'web_form' },
        ],
      },
    }),
    per_page: Property.Number({
      displayName: 'Leads Per Page',
      description: 'The number of leads to return (default 25, max 2500).',
      required: false,
    }),
  },

  async run(context) {
    const { auth, propsValue } = context;

    const searchCriteria = [
      propsValue.email_address,
      propsValue.phone_number,
      propsValue.lead_type,
    ];

    if (searchCriteria.every((criterion) => !criterion)) {
      throw new Error(
        'Please provide at least one search criteria (Email, Phone, or Lead Type).'
      );
    }

    return await whatConvertsClient.findLeads(auth, {
      profile_id: propsValue.profile_id,
      email_address: propsValue.email_address,
      phone_number: propsValue.phone_number,
      lead_type: propsValue.lead_type,
      per_page: propsValue.per_page,
    });
  },
});