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 { Property, createAction } from '@activepieces/pieces-framework';
import {
  AuthenticationType,
  HttpMethod,
  HttpRequest,
  httpClient,
} from '@activepieces/pieces-common';
import { xeroAuth } from '../..';
import { props } from '../common/props';
import { xeroInput } from '../common/client';
import { xeroOutputSchemas } from '../output-schemas';

export const xeroFindContact = createAction({
  auth: xeroAuth,
  name: 'xero_find_contact',
  classification: 'SEARCH',
  displayName: 'Find Contact',
  description: 'Finds a contact by name or account number (or SearchTerm).',
  audience: 'both',
  aiMetadata: {
    description:
      'Read-only lookup of Xero contacts in a tenant; pick when you need to locate a contact or its ID before invoicing or other operations. Choose the search mode via Search By: exact Name or Account Number, or a broad Search Term for fuzzy matching across fields. Returns matching contacts without modifying anything; supply the tenant ID and use Summary Only / Page for large result sets.',
    idempotent: true,
  },
  outputSchema: xeroOutputSchemas.contactEnvelope,
  props: {
    tenant_id: props.tenant_id,
    search_by: Property.StaticDropdown({
      displayName: 'Search By',
      required: true,
      options: {
        options: [
          { label: 'Name (exact match)', value: 'NAME' },
          { label: 'Account Number (exact match)', value: 'ACCOUNT_NUMBER' },
          { label: 'Search Term (broad search)', value: 'SEARCH_TERM' },
        ],
      },
      defaultValue: 'NAME',
    }),
    value: Property.ShortText({
      displayName: 'Value',
      description: 'Name, Account Number, or Search Term depending on Search By.',
      required: true,
    }),
    include_archived: Property.Checkbox({
      displayName: 'Include Archived',
      required: false,
      defaultValue: false,
    }),
    summary_only: Property.Checkbox({
      displayName: 'Summary Only (faster, lighter)',
      description: 'Recommended for broad searches (Search Term). Excludes heavy fields.',
      required: false,
      defaultValue: true,
    }),
    page: Property.Number({
      displayName: 'Page',
      description: 'Pagination page (optional).',
      required: false,
    }),
  },
  async run(context) {
    const {
      tenant_id,
      search_by,
      value,
      include_archived,
      summary_only,
      page,
    } = context.propsValue as any;

    const baseUrl = 'https://api.xero.com/api.xro/2.0/Contacts';

    const params: string[] = [];
    if (include_archived) params.push('includeArchived=true');
    if (summary_only) params.push('summaryOnly=true');
    if (page) params.push(`page=${encodeURIComponent(page)}`);

    if (search_by === 'SEARCH_TERM') {
      params.push(`SearchTerm=${encodeURIComponent(value)}`);
    } else if (search_by === 'NAME') {
      const where = `Name=${xeroInput.whereString({ value })}`;
      params.push(`where=${encodeURIComponent(where)}`);
    } else if (search_by === 'ACCOUNT_NUMBER') {
      const where = `AccountNumber=${xeroInput.whereString({ value })}`;
      params.push(`where=${encodeURIComponent(where)}`);
    }

    const url = params.length ? `${baseUrl}?${params.join('&')}` : baseUrl;

    const request: HttpRequest = {
      method: HttpMethod.GET,
      url,
      authentication: {
        type: AuthenticationType.BEARER_TOKEN,
        token: (context.auth as any).access_token,
      },
      headers: {
        'Xero-Tenant-Id': tenant_id,
      },
    };

    const result = await httpClient.sendRequest(request);
    if (result.status === 200) {
      return result.body;
    }
    return result;
  },
});